In our data aggregation pipeline we have a bunch of conditioning variables that are used to define groups. To improve code readability and maintainability, we use a preconfigured list of symbols with tidy evaluation as per this illustrative snippet:
# this is the list of our condition variables
condition_vars <- rlang::exprs(var1, var2, var3)
# split the data into groups
data %>% group_by(!!!condition_vars) %>% summarize(...)
This works great but I can't figure out what would be the elegant way to use this in <tidy-select>
context, e.g. for something like nest
. The problem is that new nest()
wants something like nest(data = c(var1, var2, var3))
and not nest(var1, var2, var3)
, so nest(!!!condition_vars)
will give me a warning.
The best I could come up with is
df <- tibble(x = c(1, 1, 1, 2, 2, 3), y = 1:6, z = 6:1)
vars <- exprs(y, z)
nest(df, data = !!call2("c", !!!vars))
but surely there is a better way...
You can do nest(df, data = c(!!!vars))
.
But nowadays, if the expressions are simple column names, I would store them in a character vector. You can supply the character vectors with all_of()
in selection contexts. In action verbs like mutate()
or group_by()
, use across()
to create a selection context where you can use all_of()
(and other features like starts_with()
).
cols <- c("cyl", "am")
mtcars %>% group_by(across(all_of(cols))
mtcars %>% nest(data = all_of(cols))