I am woking on a project, that has two possible scenarios. I the first scenanrio, i need variable a
, b
, and c
in the environment. In the second scenario i need variables a
, b
, d
, and f
.
I am trying to figure out a way to "attach" these environment somehow, such that i firstly create an basis_environment
with the variable that are common in both scenarios - a
and b
basis_environment <- env(a = 4, b = 5)
and the depending on the scenario (ex.scenario1) i create another just the variables like:
final_environment <- env(basis_environment,
c = 7)
However, this results in a final_ennvironment that only has one element, namely c
.
How can i create an environment that has all three variables, such that two are created at an earlier stage and the last one is created later on, but the final environment includes all three of them?
You could achieve this in base R with something like:
basis_environment <- list2env(list(a = 4, b = 5))
# The following ensures a copy of the environment is made, not just a reference
final_environment <- list2env(as.list(basis_environment))
# Add elements directly to final_environment using the $ operator
final_environment$c <- 7
And we can see these two environments are different:
ls(envir = basis_environment)
#> [1] "a" "b"
ls(envir = final_environment)
#> [1] "a" "b" "c"
Created on 2020-11-22 by the reprex package (v0.3.0)