19.3 Quoting

Capture expressions without evaluating them

Developer User
Expression (Quasiquotation)
One expr() enexpr()
Many exprs() enexprs()
Symbol (Quasiquotation)
One ensym()
Many ensyms()
R Base (Quotation)
One quote() alist()
Many substitute() as.list(substitute(...()))
  • Non-base functions are from rlang
  • Developer - From you, direct, fixed, interactive
  • User - From the user, indirect, varying, programmatic

Also:

  • bquote() provides a limited form of quasiquotation
  • ~, the formula, is a quoting function (see Section 20.3.4)

expr() and exprs()

expr(x + y)
#> x + y
exprs(exp1 = x + y, exp2 = x * y)
#> $exp1
#> x + y
#> 
#> $exp2
#> x * y

enexpr()2 and enexprs()

f <- function(x) enexpr(x)
f(a + b + c)
#> a + b + c

f2 <- function(x, y) enexprs(exp1 = x, exp2 = y)
f2(x = a + b, y = c + d)
#> $exp1
#> a + b
#> 
#> $exp2
#> c + d

ensym() and ensyms()

  • Remember: Symbol represents the name of an object. Can only be length 1.
  • These are stricter than enexpr/s()
f <- function(x) ensym(x)
f(a)
#> a

f2 <- function(x, y) ensyms(sym1 = x, sym2 = y)
f2(x = a, y = "b")
#> $sym1
#> a
#> 
#> $sym2
#> b

  1. enexpr() = enrich expr()↩︎