env_clone {rlang}R Documentation

Clone or coalesce an environment

Description

Both these functions preserve active bindings and promises (the latter are only preserved on R >= 4.0.0).

Usage

env_clone(env, parent = env_parent(env))

env_coalesce(env, from)

Arguments

env

An environment.

parent

The parent of the cloned environment.

from

Environment to copy bindings from.

Examples

# A clone initially contains the same bindings as the original
# environment
env <- env(a = 1, b = 2)
clone <- env_clone(env)

env_print(clone)
env_print(env)

# But it can acquire new bindings or change existing ones without
# impacting the original environment
env_bind(clone, a = "foo", c = 3)

env_print(clone)
env_print(env)


# `env_coalesce()` copies bindings from one environment to another
lhs <- env(a = 1)
rhs <- env(a = "a", b = "b", c = "c")
env_coalesce(lhs, rhs)
env_print(lhs)

# To copy all the bindings from `rhs` into `lhs`, first delete the
# conflicting bindings from `rhs`
env_unbind(lhs, env_names(rhs))
env_coalesce(lhs, rhs)
env_print(lhs)

[Package rlang version 1.1.3 Index]