rlang (version 0.0.0.9000)

env_set: Set an environment.

Description

env_set() does not work by side effect. The input is copied before being assigned an environment, and left unchanged. However, env_set_parent() operates on the inner environment and does have a side effect.

Usage

env_set(env, new_env)
"env_set"(env, new_env)
"env_set"(env, new_env)
"env_set"(env, new_env)
env_set_parent(env, new_env)

Arguments

env
An environment or an object with a S3 method for env_set().
new_env
An environment to replace env with. Can be an object with an S method for env().

Examples

Run this code
# Create a function with a given environment:
env <- new_env(base_env())
fn <- with_env(env, function() NULL)
identical(env(fn), env)

# env_set() does not work by side effect. Setting a new environment
# for fn has no effect on the original function:
other_env <- new_env()
env_set(fn, other_env)
identical(env(fn), other_env)

# env_set() returns a new function with a different environment, so
# you need to assign the returned function to the `fn` name:
fn <- env_set(fn, other_env)
identical(env(fn), other_env)

Run the code above in your browser using DataCamp Workspace