These functions return raw expressions (whereas quo()
and
variants return quosures). They support quasiquotation
syntax.
expr()
returns its argument unevaluated. It is equivalent to
base::bquote()
.
enexpr()
takes an argument name and returns it unevaluated. It
is equivalent to base::substitute()
.
exprs()
captures multiple expressions and returns a list. In
particular, it can capture expressions in ...
. It supports name
unquoting with :=
(see quos()
). It is equivalent to
eval(substitute(alist(...)))
.
See is_expr()
for more about R expressions.
expr(expr)enexpr(arg)
exprs(..., .ignore_empty = "trailing")
An expression.
A symbol referring to an argument. The expression supplied to that argument will be captured unevaluated.
Arguments to extract.
Whether to ignore empty arguments. Can be one
of "trailing"
, "none"
, "all"
. If "trailing"
, only the
last argument is ignored if it is empty.
The raw expression supplied as argument. exprs()
returns
a list of expressions.
# NOT RUN {
# The advantage of expr() over quote() is that it unquotes on
# capture:
expr(list(1, !! 3 + 10))
# Unquoting can be especially useful for successive transformation
# of a captured expression:
(expr <- quote(foo(bar)))
(expr <- expr(inner(!! expr, arg1)))
(expr <- expr(outer(!! expr, !!! lapply(letters[1:3], as.symbol))))
# Unlike quo(), expr() produces expressions that can
# be evaluated with base::eval():
e <- quote(letters)
e <- expr(toupper(!!e))
eval(e)
# Be careful if you unquote a quosure: you need to take the RHS
# (and lose the scope information) to evaluate with eval():
f <- quo(letters)
e <- expr(toupper(!! get_expr(f)))
eval(e)
# On the other hand it's fine to unquote quosures if you evaluate
# with eval_tidy():
f <- quo(letters)
e <- expr(toupper(!! f))
eval_tidy(e)
# exprs() lets you unquote names with the definition operator:
nm <- "foo"
exprs(a = 1, !! nm := 2)
# }
Run the code above in your browser using DataLab