1

为了使自定义函数能够灵活地接收每个形式参数的一个或多个调用参数,我目前依赖“...”:

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() 。

4

1 回答 1

1

我们可以...

foo <- function(data,  dv, ...){
   groups <- enquos(...)
   dv <- enquo(dv)
   data %>% 
     group_by(!!!groups) %>% 
     summarise(group_mean = mean(!!dv))
  }

如果我们想传递一个vector“组”,那么一个选项是group_by_at

foo <- function(data, groups, dv){
  dv <- enquo(dv)

  data %>% 
     group_by_at(vars(groups)) %>% 
     summarise(group_mean = mean(!!dv))
  }

mtcars %>% 
    foo(groups = c("vs", "am"), dv = mpg)
# A tibble: 4 x 3
# Groups:   vs [?]
#     vs    am group_mean
#  <dbl> <dbl>      <dbl>
#1     0     0       15.0
#2     0     1       19.8
#3     1     0       20.7
#4     1     1       28.4

如果我们想传递不带引号的表达式,一种选择c是将其转换为表达式,然后对其求值

foo <- function(data, groups, dv){

 groups <- as.list(rlang::enexpr(groups))[-1]
 dv <- enquo(dv)
   data %>% 
      group_by(!!! groups) %>% 
      summarise(group_mean = mean(!!dv))
 }

mtcars %>% 
      foo(groups = c(vs, am), dv = mpg)
# A tibble: 4 x 3
# Groups:   vs [?]
#     vs    am group_mean
#  <dbl> <dbl>      <dbl>
#1     0     0       15.0
#2     0     1       19.8
#3     1     0       20.7
#4     1     1       28.4

或者正如评论中提到的@Joe,enquo也应该与group_by_at

foo <- function(data, groups, dv){
   dv <- enquo(dv) 
   groups <- enquos(groups) 
   data %>% 
        group_by_at(vars(!!!groups)) %>% 
        summarise(group_mean = mean(!!dv))
   } 

mtcars %>% 
     foo(groups = c(vs, am), dv = mpg)
# A tibble: 4 x 3
# Groups:   vs [?]
#     vs    am group_mean
#  <dbl> <dbl>      <dbl>
#1     0     0       15.0
#2     0     1       19.8
#3     1     0       20.7
#4     1     1       28.4
于 2018-11-05T19:13:59.713 回答