rlang (version 0.2.1)

env_bind_exprs: Bind lazy or active bindings

Description

Bind lazy or active bindings

Usage

env_bind_exprs(.env, ..., .eval_env = caller_env())

env_bind_fns(.env, ...)

Arguments

.env

An environment or an object bundling an environment, e.g. a formula, quosure or closure. This argument is passed to get_env().

...

Pairs of names and expressions, values or functions. These dots support tidy dots features.

.eval_env

The environment where the expressions will be evaluated when the symbols are forced.

Life cycle

These functions are experimental. Expect API changes.

Examples

Run this code
# NOT RUN {
# env_bind_exprs() assigns expressions lazily:
env <- env()
env_bind_exprs(env, name = cat("forced!\n"))
env$name
env$name

# You can unquote expressions. Note that quosures are not
# supported, only raw expressions:
expr <- quote(message("forced!"))
env_bind_exprs(env, name = !! expr)
env$name

# You can create active bindings with env_bind_fns()
# Let's create some bindings in the lexical enclosure of `fn`:
counter <- 0

# And now a function that increments the counter and returns a
# string with the count:
fn <- function() {
  counter <<- counter + 1
  paste("my counter:", counter)
}

# Now we create an active binding in a child of the current
# environment:
env <- env()
env_bind_fns(env, symbol = fn)

# `fn` is executed each time `symbol` is evaluated or retrieved:
env$symbol
env$symbol
eval_bare(quote(symbol), env)
eval_bare(quote(symbol), env)
# }

Run the code above in your browser using DataCamp Workspace