为了使自定义函数能够灵活地接收每个形式参数的一个或多个调用参数,我目前依赖“...”:
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)
但是“...”模糊了函数的逻辑,并且它不能用于具有 2 个或多个需要多个调用参数的正式参数的自定义函数。
有没有办法编写上述函数来利用形式参数(例如,“组”)而不是“...”,它可以接收单个向量名称或向量名称的向量作为其参数?就像是:
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)
请注意,此代码可以工作,但需要用户记住在函数体中使用 quos():
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)
我很想在函数体中依赖 enquos() 。