Examples for 'rlang::env_clone'


Clone or coalesce an environment

Aliases: env_clone env_coalesce

Keywords:

### ** 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)
<environment: 0x55ccfebb4928>
Parent: <environment: 0x55ccfe32e560>
Bindings:
• a: <dbl>
• b: <dbl>
env_print(env)
<environment: 0x55ccfeb7b2f0>
Parent: <environment: 0x55ccfe32e560>
Bindings:
• a: <dbl>
• b: <dbl>
# 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)
<environment: 0x55ccfebb4928>
Parent: <environment: 0x55ccfe32e560>
Bindings:
• a: <chr>
• b: <dbl>
• c: <dbl>
env_print(env)
<environment: 0x55ccfeb7b2f0>
Parent: <environment: 0x55ccfe32e560>
Bindings:
• a: <dbl>
• b: <dbl>
# `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)
<environment: 0x55ccfb1ae3e8>
Parent: <environment: 0x55ccfe32e560>
Bindings:
• a: <dbl>
• b: <chr>
• c: <chr>
# 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)
<environment: 0x55ccfb1ae3e8>
Parent: <environment: 0x55ccfe32e560>
Bindings:
• a: <chr>
• b: <chr>
• c: <chr>

[Package rlang version 1.1.4 Index]