[Rd] saveRDS()/readRDS() on environments

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Wed Nov 15 20:13:34 CET 2023


On Wed, 15 Nov 2023 19:49:00 +0100
Antoine Fabri <antoine.fabri using gmail.com> wrote:

> I would like to know what rules are followed, if I had to guess I
> would think that parents are saved until a "special environment" is
> reached (such as global env, empty env, package envs, namespaces,
> imports, datasets), but I'd like clarifications on this point.

This is what R Internals has to say:

>> Serialization in R needs to take into account that objects may
>> contain references to environments, which then have enclosing
>> environments and so on. (Environments recognized as package or name
>> space environments are saved by name.

>> Some objects are written as if they were SEXPTYPEs: such
>> pseudo-SEXPTYPEs cover R_NilValue, R_EmptyEnv, R_BaseEnv,
>> R_GlobalEnv, R_UnboundValue, R_MissingArg and R_BaseNamespace. 

>> Environments are treated in several ways: as we have seen, some are
>> written as specific pseudo-SEXPTYPEs. Package and namespace
>> environments are written with pseudo-SEXPTYPEs followed by the name.
>> ‘Normal’ environments are written out as ENVSXPs with an integer
>> indicating if the environment is locked followed by the enclosure,
>> frame, ‘tag’ (the hash table) and attributes. 

<http://mirrors.dotsrc.org/cran/doc/manuals/R-ints.html#Serialization-Formats>

(I would normally link to cran.r-project.org, but they are undergoing
maintenance until tomorrow.)

To summarise, ordinary, anonymous environments (including those
originating from a call frame that a closure may inherit!) are saved.
Package-related environments are referenced by name, and so is the
global environment.

-- 
Best regards,
Ivan



More information about the R-devel mailing list