在R中的函数内部取消引用参数

在R中的函数内部取消引用参数,r,dplyr,quotes,quasiquotes,R,Dplyr,Quotes,Quasiquotes,我无法理解为什么函数中的bang-bang运算符没有取消引用我的grp参数。任何帮助都将不胜感激 library(dplyr) test_func <- function(dat, grp){ dat %>% group_by(!!grp) %>% summarise(N = n()) } test_func(dat = iris, grp = "Species") 库(dplyr) 测试函数% 分组依据(!!grp)%>% 总结

我无法理解为什么函数中的bang-bang运算符没有取消引用我的
grp
参数。任何帮助都将不胜感激

library(dplyr)

test_func <- function(dat, grp){
  dat %>%
    group_by(!!grp) %>%
    summarise(N =  n())
}

test_func(dat = iris, grp = "Species")
库(dplyr)
测试函数%
分组依据(!!grp)%>%
总结(N=N())
}
测试功能(dat=iris,grp=“物种”)
它不是按物种分组,而是生成整个数据的摘要:

如果我们传递一个字符串,那么将其转换为
sym
bol并计算(
!!

test_func <- function(dat, grp){
 dat %>%
    group_by(!! rlang::ensym(grp)) %>%
    summarise(N =  n(), .groups = 'drop')
 }
test_func(dat = iris, grp = "Species")
# A tibble: 3 x 2
#  Species        N
#* <fct>      <int>
#1 setosa        50
#2 versicolor    50
#3 virginica     50
test_func <- function(dat, grp){
    dat %>%
       group_by(across(all_of(grp))) %>%
       summarise(N =  n(), .groups = 'drop')
 }