rlang (version 0.0.0.9000)

env_assign_lazily: Assign a promise to an environment.

Description

These functions let you create a promise in an environment. Such promises behave just like lazily evaluated arguments. They are evaluated whenever they are touched by code, but not when they are passed as arguments.

Usage

env_assign_lazily(env = env_caller(), nm, expr, eval_env = NULL)
env_assign_lazily_(env = env_caller(), nm, expr, eval_env = NULL)

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.
expr
An expression to capture for env_assign_lazily(), or a captured expression (either quoted or a formula) for the standard evaluation version env_assign_lazily_(). This expression is used to create a promise in env.
eval_env
The environment where the promise will be evaluated when the promise gets forced. If expr is a formula, its environment is used instead. If not a formula and eval_env is not supplied, the promise is evaluated in the environment where env_assign_lazily() (or the underscore version) was called.

See Also

env_assign()

Examples

Run this code
env <- env_new()
env_assign_lazily(env, "name", cat("forced!\n"))
env$name

# Use the standard evaluation version with quoted expressions:
f <- ~message("forced!")
env_assign_lazily_(env, "name2", f)
env$name2

Run the code above in your browser using DataCamp Workspace