[R] save(), load(), saveRDS(), and readRDS()

Greg Snow 538280 @end|ng |rom gm@||@com
Mon Oct 2 21:12:29 CEST 2023

One more function to consider using and teaching is the attach
function.  If you use `attach` with a the name of a file that was
created using `save` then it creates a new, empty environment, `load`s
the contents of the file into the environment, and attached the
environment to the search path (by default in position 2).  This means
that the objects are all available to use, but will not overwrite any
objects of the same name in your workspace.  The command `ls(2)`
quickly shows the names of the objects that were read in.  You can use
simple assignment to copy and optionally rename any of the objects
into your workspace, or just leave them in the attached workspace
(just recognize what will happen if you have multiple objects with the
same name).  Once you have copied or used the objects of interest, you
can simply `detach` the environment.

If you are going to teach the use of `attach` I would suggest
emphasizing the 2nd paragraph under the heading "Good practice" on the
help page for attach.

On Thu, Sep 28, 2023 at 9:48 AM Shu Fai Cheung <shufai.cheung using gmail.com> wrote:
> Hi All,
> There is a thread about the use of save(), load(), saveRDS(), and
> loadRDS(). It led me to think about a question regarding them.
> In my personal work, I prefer using saveRDS() and loadRDS() as I don't like
> the risk of overwriting anything in the global environment. I also like the
> freedom to name an object when reading it from a file.
> However, for teaching, I have to teach save() and load() because, in my
> discipline, it is common for researchers to share their datasets on the
> internet using the format saved by save(), and so students need to know how
> to use load() and what will happen when using it. Actually, I can't recall
> encountering datasets shared by the .rds format. I have been wondering why
> save() was usually used in that case.
> That discussion led me to read the help pages again and I noticed the
> following warning, from the help page of saveRDS():
> "Files produced by saveRDS (or serialize to a file connection) are not
> suitable as an interchange format between machines, for example to download
> from a website. The files produced by save
> <> have a header identifying
> the file type and so are better protected against erroneous use."
> When will the problem mentioned in the warning occur? That is, when will a
> file saved by saveRDS() not be read correctly? Saved in Linux and then read
> in Windows? Is it possible to create a reproducible error?
> Regards,
> Shu Fai
>         [[alternative HTML version deleted]]
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

Gregory (Greg) L. Snow Ph.D.
538280 using gmail.com

More information about the R-help mailing list