[R] save/load and package namespaces

Duncan Murdoch murdoch.duncan at gmail.com
Tue Nov 13 20:12:27 CET 2012


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




More information about the R-help mailing list