This is a simple question but the answer is apparently not so simple... Is it possible to combine environments in R?
E1 = new.env()
E2 = new.env()
E1$x = 25
E2$y = 7
Ok, now I want an environment (say, E3
) that has both x
and y
defined.
c(E1, E2)
#doesn't work
E3 = new.env(E1, E2)
#doesn't work
I have found other similar questions but they don't seem to work for me.
Use case: Maybe there's a reason this isn't easy...the reason I want to do this is thus: I use some functions to load up data. Previously, I've just loaded it into the global environment, but I now have many different functions loading different types of data (which I call variously as needed), and so I wanted to keep the loaded data a bit more compartmentalized. If I call 2 different loading functions E1=loadData1()
and E2=loadData2()
, and I now want to call a function that uses variables from both of these functions, I'd like to be able to say with(E1 & E2, someFunction())
. Hence, merging my loaded environments seems appropriate.
So, what's the right way to merge them? And, as an aside, do you have a different suggestion for how to better accomplish what I'm doing, if merging environments is not the right way?