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

Antoine Fabri @nto|ne@|@br| @end|ng |rom gm@||@com
Wed Nov 15 19:49:00 CET 2023


Dear r-devel,

I was surprised to see that saveRDS() and readRDS() work quite well with
environments, see below:

```
z <- 3 # in global env
y <- new.env()
y$a <- 1
x <- new.env(parent = y)
x$b <- 2
saveRDS(x, "x.RDS")

# in a new session
x <- readRDS("x.RDS")
y <- parent.env(x)
x$b
#> [1] 2
y$a
#> [1] 1
parent.env(y)
#> <environment: R_GlobalEnv>
z
#> Error: object 'z' not found
```
We know that environments by definition cannot be cloned without
compromises, and we see above that compromises were made: we did save the
first parent, but didn't save or at least restore the global env. I don't
see this documented in `?ReadRDS`, did I miss something ?

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.

Thanks,

Antoine

	[[alternative HTML version deleted]]



More information about the R-devel mailing list