rlang (version 0.0.0.9000)

env_assign: Assign objects to an environment.

Description

These functions create bindings in the specified environment. The bindings are supplied as pairs of names and values, either directly (env_assign()), in dots (env_define()), or from a dictionary (env_bind()). See is_dictionary() for the definition of a dictionary.

Usage

env_assign(env = caller_env(), nm, x)
env_bind(env = caller_env(), data = list())
env_define(env = caller_env(), ...)

Arguments

env
An environment or an object with a S3 method for env(). If missing, the environment of the current evaluation frame is returned.
nm
The name of the binding.
x
The value of the binding.
data
A vector with unique names which defines bindings (pairs of name and value). See is_dictionary().
...
Pairs of unique names and R objects used to define new bindings.

Value

The input object env, with its associated environment modified in place.

Details

These functions operate by side effect. For example, if you assign bindings to a closure function, the environment of the function is modified in place.

Examples

Run this code
# Create a function that uses undefined bindings:
fn <- function() list(a, b, c, d, e)
env(fn) <- new_env(base_env())

# This would throw a scoping error if run:
# fn()

data <- stats::setNames(letters, letters)
env_bind(fn, data)

# fn() now sees the objects
fn()

# Redefine new bindings:
fn <- env_assign(fn, "a", "1")
fn <- env_define(fn, b = "2", c = "3")
fn()

Run the code above in your browser using DataCamp Workspace