Skip to content

save(), load(), saveRDS(), and readRDS()

1 message · Greg Snow

#
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 at gmail.com> wrote: