This is with the new dplyr, to be released Real Soon Now.
The dplyr programming vignette gives an example of calling group_by
with the grouping variables specified in an outer function:
my_summarise <- function(df, group_var) {
df %>%
group_by(!!group_var) %>%
summarise(a = mean(a))
}
This works when supplied with a single grouping variable. However, it fails with multiple variables.
Simplified example:
f <- function(x)
{
group_by(mtcars, !!x)
}
## works
g1 <- "cyl"
f(g1)
## doesn't work
#Error in mutate_impl(.data, dots) :
# Column `c("cyl", "gear")` must be length 32 (the number of rows) or one, not 2
g2 <- c("cyl", "gear")
f(g2)
How can I fix this, within the rlang framework?
Ideally I want f
's signature to remain the same, ie I specify the grouping variables as a single vector rather than via a ...
argument.