On 13/11/2012 1:45 PM, Jamie Olson wrote:
Correct me if I'm wrong, but it also seems that more generally, everything works as long as the environment is 'below' .GlobalEnv. For example,

x = function(){
   y = 4
   function()y
}
yfun = x()
save(yfun,file = "yfun.RData")

load("yfun.RData")
yfun()

This works fine even when there are more inherited environments. I imagine this is because in saving yfun, it also saves the environment and any parent environments until some point? Is it checking to see if the environment inherits from .GlobalEnv or something like that?

The source is here: https://svn.r-project.org/R/trunk/src/main/serialize.c. It's not the simplest code, but if you look, you can see that the empty, base and global environments are handled specially (by just writing a marker, not their contents). Package and namespace environments are handled by writing out a string describing them. Other environments are saved by saving their parent, their content, their hash table, and their attributes.

So yfun would be saved, along with its environment, but the parent of that environment is globalenv(), so just the marker is saved.

Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to