Skip to content
Prev 27869 / 63424 Next

merging environments

On 3/7/2008 2:02 PM, Ben Bolker wrote:
One way is to set one as the parent of the other.  If they both already 
have non-empty parents, you're out of luck.
Luckily lists and data.frames don't have parents, so you can make a new 
environment, put the elements of data into it, and set the old 
environment as its parent.  That's sort of like what you did, but 
slightly different, and with fewer bad side effects:

bothenvs <- function(fun,data) {
      newenv <- new.env(hash=TRUE, parent=environment(fun))
      mapply(assign,names(data),data,
                       MoreArgs=list(envir=newenv))
      newenv
}

It would sure be nice if as.environment() took a list as an arg and 
turned it into an environment, but no such luck.

Duncan Murdoch