# NOT RUN {
# Quasiquotation functions act like base::quote()
quote(foo(bar))
expr(foo(bar))
quo(foo(bar))
# In addition, they support unquoting:
expr(foo(UQ(1 + 2)))
expr(foo(!! 1 + 2))
quo(foo(!! 1 + 2))
# The !! operator is a handy 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 `!`:
quo(1 + !! (1 + 2 + 3) + 10)
# For this reason you should always wrap the unquoted expression
# with parentheses when operators are involved:
quo(1 + (!! 1 + 2 + 3) + 10)
# Or you can use the explicit unquote function:
quo(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)
quo(mean( UQS(args) ))
# You can combine the two
var <- quote(xyz)
extra_args <- list(trim = 0.9, na.rm = TRUE)
quo(mean(UQ(var) , UQS(extra_args)))
# Unquoting is especially useful for transforming successively a
# captured expression:
quo <- quo(foo(bar))
quo <- quo(inner(!! quo, arg1))
quo <- quo(outer(!! quo, !!! syms(letters[1:3])))
quo
# Since we are building the expression in the same environment, you
# can also start with raw expressions and create a quosure in the
# very last step to record the dynamic environment:
expr <- expr(foo(bar))
expr <- expr(inner(!! expr, arg1))
quo <- quo(outer(!! expr, !!! syms(letters[1:3])))
quo
# }
Run the code above in your browser using DataLab