Examples for 'rlang::get_env'


Get or set the environment of an object

Aliases: get_env set_env env_poke_parent

Keywords:

### ** Examples

# Environment of closure functions:
fn <- function() "foo"
get_env(fn)
<environment: 0x55ccfe07ab50>
# Or of quosures or formulas:
get_env(~foo)
<environment: 0x55ccfe07ab50>
get_env(quo(foo))
<environment: 0x55ccfe07ab50>
# Provide a default in case the object doesn't bundle an environment.
# Let's create an unevaluated formula:
f <- quote(~foo)

# The following line would fail if run because unevaluated formulas
# don't bundle an environment (they didn't have the chance to
# record one yet):
# get_env(f)

# It is often useful to provide a default when you're writing
# functions accepting formulas as input:
default <- env()
identical(get_env(f, default), default)
[1] TRUE
# set_env() can be used to set the enclosure of functions and
# formulas. Let's create a function with a particular environment:
env <- child_env("base")
fn <- set_env(function() NULL, env)

# That function now has `env` as enclosure:
identical(get_env(fn), env)
[1] TRUE
identical(get_env(fn), current_env())
[1] FALSE
# set_env() does not work by side effect. Setting a new environment
# for fn has no effect on the original function:
other_env <- child_env(NULL)
set_env(fn, other_env)
<srcref: file "" chars 31:15 to 31:29>
<environment: 0x55ccff340b28>
identical(get_env(fn), other_env)
[1] FALSE
# Since set_env() returns a new function with a different
# environment, you'll need to reassign the result:
fn <- set_env(fn, other_env)
identical(get_env(fn), other_env)
[1] TRUE

[Package rlang version 1.1.4 Index]