# When a tidyeval function captures an argument, it is wrapped in a
# formula and interpolated. tidy_quote() is a simple wrapper around
# tidy_capture() and as such is the fundamental tidyeval
# function. It allows you to quote an expression and interpolate
# unquoted parts:
tidy_quote(foo(bar))
tidy_quote(1 + 2)
tidy_quote(paste0(!! letters[1:2], "foo"))
# The !! operator is a syntactic shortcut for unquoting with UQ().
# However you need to be a bit careful with operator
# precedence. All arithmetic and comparison operators bind more
# tightly than `!`:
tidy_quote(1 + !! (1 + 2 + 3) + 10)
# For this reason you should always wrap the unquoted expression
# with parentheses when operators are involved:
tidy_quote(1 + (!! 1 + 2 + 3) + 10)
# Or you can use the explicit unquote function:
tidy_quote(1 + UQ(1 + 2 + 3) + 10)
# Use !!! or UQS() if you want to add multiple arguments to a
# function It must evaluate to a list
args <- list(1:10, na.rm = TRUE)
tidy_quote(mean( UQS(args) ))
# You can combine the two
var <- quote(xyz)
extra_args <- list(trim = 0.9, na.rm = TRUE)
tidy_quote(mean(UQ(var) , UQS(extra_args)))
# Unquoting is especially useful for transforming a captured
# expression:
f <- ~foo(bar)
f <- tidy_quote(inner(!! f, arg1))
f <- tidy_quote(outer(!! f, !!! lapply(letters[1:3], as.symbol)))
f
# Note that it's fine to unquote formulas as long as you evaluate
# with tidy_eval():
f <- ~letters
f <- tidy_quote(toupper(!! f))
tidy_eval(f)
# Formulas carry scope information about the inner expression
# inlined in the outer expression upon unquoting. To see this,
# let's create a formula that quotes a symbol that only exists in a
# local scope (a child environment of the current environment):
f1 <- local({ foo <- "foo"; ~foo })
# You can evaluate that expression with tidy_eval():
tidy_eval(f1)
# And you can also inline it in another expression before
# evaluation:
f2 <- local({ bar <- "bar"; ~toupper(bar)})
f3 <- tidy_quote(paste(!!f1, !!f2, "!"))
f3
# tidy_eval() treats one-sided formulas like promises to be evaluated:
tidy_eval(f3)
# The formula-promise representation is necessary to preserve scope
# information and make sure objects are looked up in the right
# place. However, there are situations where it can get in the way.
# This is the case when you deal with non-tidy NSE functions that do
# not understand formulas. You can inline the RHS of a formula in a
# call thanks to the UQE() operator:
nse_function <- function(arg) substitute(arg)
var <- local(~foo(bar))
tidy_quote(nse_function(UQ(var)))
tidy_quote(nse_function(UQE(var)))
# This is equivalent to unquoting and taking the RHS:
tidy_quote(nse_function(!! f_rhs(var)))
# One of the most important old-style NSE function is the dollar
# operator. You need to use UQE() for subsetting with dollar:
var <- ~cyl
tidy_quote(mtcars$UQE(var))
# `!!`() is also treated as a shortcut. It is meant for situations
# where the bang operator would not parse, such as subsetting with
# $. Since that's its main purpose, we've made it a shortcut for
# UQE() rather than UQ():
var <- ~cyl
tidy_quote(mtcars$`!!`(var))
# Sometimes you would like to unquote an object containing a
# formula but include it as is rather than treating it as a
# promise. You can use UQF() for this purpose:
var <- ~letters[1:2]
f <- tidy_quote(list(!!var, UQF(var)))
f
tidy_eval(f)
# Note that two-sided formulas are never treated as fpromises:
tidy_eval(tidy_quote(a ~ b))
Run the code above in your browser using DataCamp Workspace