Saturday, 7 September 2013

Using a parent environment to access child

Using a parent environment to access child

I'm attempting to create a hierarchy of environments, where I have one
main environment which contains a variety of other environments which
could then contain other environments (and so on).
I create the environment and its child like this:
parentEnv <- new.env()
childEnv <- new.env(parentEnv)
We can see that the parent and child were created:
> childEnv
<environment: 0x000000000e811208>
> parentEnv
<environment: 0x000000000d9e2440>
However, I then check the child's parent and it tells me its the R_Global.
(This isn't actually surprising as I was able to access it directly). Does
new.env(parent) not do what I think it does?
> parent.env(childEnv)
<environment: R_GlobalEnv>
That's fine, I then set my child's parent as parent.env(childEnv) <-
parentEnv (though the R docs says this is 'dangerous' and could be become
deprecated, I wanted to try it anyway).
parent.env(childEnv) <- parentEnv
> childEnv
<environment: 0x000000000e811208>
> parent.env(childEnv)
<environment: 0x000000000d9e2440>
We now see that the child's parent is parentEnv! Everything should be
great, right...?
> parentEnv$childEnv
NULL
> with(parentEnv, childEnv)
<environment: 0x000000000e811208>
I can't access it with $. Though I can using the 'with' notation. What's
going on here? Am not understand how environments work in R?

No comments:

Post a Comment