enquo_arg: Enquo function argument(s) to quosure

Description Usage Arguments Value Author(s) See Also Examples

Description

This function can be used inside a function to enquo() an argument to a quosure. By this means, you can work with non-standard evaluation functions (e.g., ggplot2, dplyr) with ease.
You can feed character, symbol, name or formula to the function. But if a value instead of an expression is given, quasi-quotation will not take effect, and thus, NULL will be returned.

Usage

1
2
3
4
5
enquo_arg(..., datamask = NULL, named = FALSE,
  flatten_mono = match.call()[1L] == "enquo_arg()")

enquo_args(..., datamask = NULL, named = FALSE,
  flatten_mono = match.call()[1L] == "enquo_arg()")

Arguments

...

argument list, either one or a list of character ("x"), name (x) or formula (~x).

datamask

environment for eval_tidy() to parse. Default NULL. You are encouraged to pass the mask ennvironment using as_data_mask().

named

logical, whether name the enquo-ed dots. Default FALSE.

flatten_mono

logical, whether break the structure and only retain the first element of the output list when there is only one argument passed in. If has no effect when there are multiple arguments.

  • if enquo_arg is called, default TRUE

  • if enquo_args is called, default FALSE

Value

A quosure (... contains only one argument) or a list of quosure (... contains multiple arguments), depending on the length of argument list and flatten_mono.

Author(s)

Yiying Wang, wangy@aetna.com

See Also

Quasi-quotation in rlang

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
## Not run: 
enquo_arg(a)  # returns a single quosure
# <quosure>
# expr: ^a
# env:  empty

enquo_args(a)  # returns a list of single quosure
# [[1]]
# <quosure>
# expr: ^a
# env:  empty

enquo_arg(a, b)  # retuns a quosure list, equal to enquo_args(a, b)
# [[1]]
# <quosure>
# expr: ^a
# env:  empty
# [[2]]
# <quosure>
# expr: ^b
# env:  empty

enquo_arg(6)  # returns NULL

# ------ A real-world use case -------
library(dplyr)
fun <- function(data, by, on){
    by <- enquo_arg(by)
    on <- enquo_arg(on)
    data %>% group_by(!! by) %>%
        summarise(avg=mean(!! on, na.rm=TRUE), sd=sd(!! on, na.rm=TRUE))
}
fun(mtcars, am, mpg)  # yields
##  A tibble: 2 x 3
#      am   avg    sd
#   <dbl> <dbl> <dbl>
# 1     0  17.1  3.83
# 2     1  24.4  6.17

# can also be written as
fun(mtcars, 'am', 'mpg')  # or
fun(mtcars, ~am, ~mpg)

# Without enquo_arg, fun will encounter an "object not found" error
# due to non-standard evaluation.

## End(Not run)

madlogos/aseskit documentation built on June 26, 2019, 12:17 a.m.