expr: Defuse an R expression

View source: R/nse-defuse.R

exprR Documentation

Defuse an R expression


expr() defuses an R expression with injection support.

It is equivalent to base::bquote().



An expression to defuse.

See Also

  • Defusing R expressions for an overview.

  • enquo() to defuse non-local expressions from function arguments.

  • Advanced defusal operators.

  • sym() and call2() for building expressions (symbols and calls respectively) programmatically.

  • base::eval() and eval_bare() for resuming evaluation of a defused expression.


# R normally returns the result of an expression
1 + 1

# `expr()` defuses the expression that you have supplied and
# returns it instead of its value
expr(1 + 1)


# It supports _injection_ with `!!` and `!!!`. This is a convenient
# way of modifying part of an expression by injecting other
# objects.
var <- "cyl"
expr(with(mtcars, mean(!!sym(var))))

vars <- c("cyl", "am")
expr(with(mtcars, c(!!!syms(vars))))

# Compare to the normal way of building expressions
call("with", call("mean", sym(var)))

call("with", call2("c", !!!syms(vars)))

rlang documentation built on April 29, 2023, 1:14 a.m.