前缀!包名 rlang

prefixing !!! with the package name, rlang

是否可以像这样使用!!!:

dplyr::count(df, rlang::`!!!`(rlang::syms(variables)))

而不是:

dplyr::count(df, !!!syms(variables))

已编辑:

不可能。

所以现在问题变成了使用 !!! 的替代方法是什么?

不使用 rlang,我们可以使用 all_ofacross

library(dplyr)
df %>%
   count(across(all_of(variables)))

rlang 中有软弃用函数,称为 UQ()UQS()(取消引用和取消引用拼接)。但是它们也不应该与前面的 rlang:: 一起使用。

总之就是用!!!.

但是这里有一个你所问的例子。

df <- mtcars
variables <- c("cyl", "gear")

dplyr::count(df, rlang::UQS(syms(variables)))

如果你这样做,它会冲你大吼大叫。

Warning message:
Prefixing `UQS()` with the rlang namespace is deprecated as of rlang 0.3.0.
Please use the non-prefixed form or `!!!` instead.

  # Bad:
  rlang::expr(mean(rlang::UQS(args)))

  # Ok:
  rlang::expr(mean(UQS(args)))

  # Good:
  rlang::expr(mean(!!!args))

这是来自 help("nse-force") 的摘录。

Calling UQ() and UQS() with the rlang namespace qualifier is deprecated as of rlang 0.3.0. Just use the unqualified forms instead:

# Bad
rlang::expr(mean(rlang::UQ(var) * 100))

# Ok
rlang::expr(mean(UQ(var) * 100))

# Good
rlang::expr(mean(!!var * 100))

Supporting namespace qualifiers complicates the implementation of unquotation and is misleading as to the nature of unquoting operators (which are syntactic operators that operate at quotation-time rather than function calls at evaluation-time).

UQ() and UQS() were soft-deprecated in rlang 0.2.0 in order to make the syntax of quasiquotation more consistent. The prefix forms are now `!!`() and `!!!`() which is consistent with other R operators (e.g. `+`(a, b) is the prefix form of a + b).

Note that the prefix forms are not as relevant as before because !! now has the right operator precedence, i.e. the same as unary - or +. It is thus safe to mingle it with other operators, e.g. !!a + !!b does the right thing. In addition the parser now strips one level of parentheses around unquoted expressions. This way (!!"foo")(...) expands to foo(...). These changes make the prefix forms less useful.

Finally, the named functional forms UQ() and UQS() were misleading because they suggested that existing knowledge about functions is applicable to quasiquotation. This was reinforced by the visible definitions of these functions exported by rlang and by the tidy eval parser interpreting rlang::UQ() as !!. In reality unquoting is not a function call, it is a syntactic operation. The operator form makes it clearer that unquoting is special.