This page describes the <data-masking>
argument modifier which
indicates that the argument uses data masking, a sub-type of
tidy evaluation. If you've never heard of tidy evaluation before,
start with the practical introduction in
https://r4ds.hadley.nz/functions.html#data-frame-functions then
then read more about the underlying theory in
https://rlang.r-lib.org/reference/topic-data-mask.html.
Key techniques
To allow the user to supply the column name in a function argument, embrace the argument, e.g.
filter(df, {{ var }})
.To work with a column name recorded as a string, use the
.data
pronoun, e.g.summarise(df, mean = mean(.data[[var]]))
.To suppress
R CMD check
NOTE
s about unknown variables use.data$var
instead ofvar
:You'll also need to import
.data
from rlang with (e.g.)@importFrom rlang .data
.
Dot-dot-dot (...)
...
automatically provides indirection, so you can use it as is
(i.e. without embracing) inside a function:
<- function(df, var, ...) {
grouped_mean %>%
df group_by(...) %>%
summarise(mean = mean({{ var }}))
}
You can also use :=
instead of =
to enable a glue-like syntax for
creating variables from user supplied data:
<- "l100km"
var_name %>% mutate("{var_name}" := 235 / mpg)
mtcars
<- function(df, var) {
summarise_mean %>%
df summarise("mean_of_{{var}}" := mean({{ var }}))
}%>% group_by(cyl) %>% summarise_mean(mpg) mtcars
Learn more in https://rlang.r-lib.org/reference/topic-data-mask-programming.html.