To make a custom function flexible to receiving one or more calling arguments per formal argument I currently rely on "...":
library(dplyr)
foo <- function(data, ..., dv){
groups <- enquos(...)
dv <- enquo(dv)
data %>%
group_by(!!!groups) %>%
summarise(group_mean = mean(!!dv))
}
mtcars %>% foo(am, dv = mpg)
mtcars %>% foo(vs, am, dv = mpg)
But "..." obscures the logic of the function, and it could not be used in a custom function with 2 or more formal arguments requiring multiple calling arguments.
Is there a way to write the above function to utilize a formal argument (e.g., "groups") rather than "..." that can receive a single vector name or a vector of vector names as its argument(s)? Something like:
foo <- function(data, groups, dv){
groups <- enquos(groups)
dv <- enquo(dv)
data %>%
group_by(!!!groups) %>%
summarise(group_mean = mean(!!dv))
}
# Failing code
mtcars %>% foo(groups = c(vs, am), dv = mpg)
Note that this code would work, but require user to remember to use quos() in function body:
foo <- function(data, groups, dv){
dv <- enquo(dv)
data %>%
group_by(!!!groups) %>%
summarise(group_mean = mean(!!dv))
}
mtcars %>% foo(groups = quos(vs, am), dv = mpg)
I'd love to rely on enquos() in body of function instead.
We could place the
...
at the endIf we want to pass a
vector
of 'group', then one option isgroup_by_at
One option if we want to pass unquoted expression with
c
would be to convert it to expression and then evaluate itOr as @Joe mentioned in the comments,
enquo
should also work withgroup_by_at