Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

inject

Inject objects in an R expression


Description

inject() evaluates an expression with injection (unquotation) support. There are three main usages:

Usage

inject(expr, env = caller_env())

Arguments

expr

An argument to evaluate. This argument is immediately evaluated in env (the current environment by default) with injected objects and expressions.

env

The environment in which to evaluate expr. Defaults to the current environment. For expert use only.

Details

  • Splicing lists of arguments in a function call.

  • Inline objects or other expressions in an expression with !! and !!!. For instance to create functions or formulas programmatically.

  • Pass arguments to NSE functions that defuse their arguments without injection support (see for instance enquo0()). You can use {{ arg }} with functions documented to support quosures. Otherwise, use !!enexpr(arg).

Examples

# inject() simply evaluates its argument with injection
# support. These expressions are equivalent:
2 * 3
inject(2 * 3)
inject(!!2 * !!3)

# Injection with `!!` can be useful to insert objects or
# expressions within other expressions, like formulas:
lhs <- sym("foo")
rhs <- sym("bar")
inject(!!lhs ~ !!rhs + 10)

# Injection with `!!!` splices lists of arguments in function
# calls:
args <- list(na.rm = TRUE, finite = 0.2)
inject(mean(1:10, !!!args))

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.