Home > Back-end >  prefixing !!! with the package name, rlang
prefixing !!! with the package name, rlang

Time:12-24

Is it possible to use the !!! like so:

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

instead of:

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

EDITED:

It is not possible.

So now the question becomes what is the alternative to using !!!?

CodePudding user response:

Instead of using the rlang, we may use all_of with across

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

CodePudding user response:

There are soft-deprecated functions in rlang called UQ() and UQS() (unquote and unquote-splice). But they should not be used with rlang:: in front either.

In short, just use !!!.

But here is an example of what you are asking about.

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

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

If you do this, it will yell at you.

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

Here is an extract from 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.

  • Related