Skip to content

expr() defuses an R expression with injection support.

It is equivalent to base::bquote().

Arguments

expr

An expression to defuse.

See also

Examples

# R normally returns the result of an expression
1 + 1
#> [1] 2

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

expr(toupper(letters))
#> toupper(letters)

# 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))))
#> with(mtcars, mean(cyl))

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

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

call("with", call2("c", !!!syms(vars)))
#> with(c(cyl, am))