save writes an external representation of R objects to the
specified file. The objects can be read back from the file at a later
date by using the function
data in some cases).
save.image() is just a short-cut for ‘save my current
save(list = ls(all.names = TRUE), file =
".RData", envir = .GlobalEnv).
It is also what happens with
1 2 3 4 5 6 7 8
save(..., list = character(), file = stop("'file' must be specified"), ascii = FALSE, version = NULL, envir = parent.frame(), compress = isTRUE(!ascii), compression_level, eval.promises = TRUE, precheck = TRUE) save.image(file = ".RData", version = NULL, ascii = FALSE, compress = !ascii, safe = TRUE)
the names of the objects to be saved (as symbols or character strings).
A character vector containing the names of objects to be saved.
a (writable binary-mode) connection or the name of the
file where the data will be saved (when tilde expansion
is done). Must be a file name for
the workspace format version to use.
environment to search for objects to be saved.
logical or character string specifying whether saving
to a named file is to use compression.
integer: the level of compression to be
used. Defaults to
logical: should objects which are promises be forced before saving?
logical: should the existence of the objects be checked before starting to save (and in particular before opening the file/connection)? Does not apply to version 1 saves.
The names of the objects specified either as symbols (or character
... or as a character vector in
used to look up the objects from environment
envir. By default
promises are evaluated, but if
eval.promises = FALSE
promises are saved (together with their evaluation environments).
(Promises embedded in objects are always saved unevaluated.)
All R platforms use the XDR (bigendian) representation of C ints and doubles in binary save-d files, and these are portable across all R platforms.
ASCII saves used to be useful for moving data between platforms but are now mainly of historical interest. They can be more compact than binary saves where compression is not used, but are almost always slower to both read and write: binary saves compress much better than ASCII ones. Further, decimal ASCII saves may not restore double/complex values exactly, and what value is restored may depend on the R platform.
Default values for the
version arguments can be modified with the
"save.defaults" option (used both by
save.image), see also the ‘Examples’ section. If a
"save.image.defaults" option is set it is used in preference to
"save.defaults" for function
save.image (which allows
this to have different defaults). In addition,
compression_level can be part of the
A connection that is not already open will be opened in mode
"wb". Supplying a connection which is open and not in binary
mode gives an error.
Large files can be reduced considerably in size by compression. A
particular 46MB R object was saved as 35MB without compression in 2
seconds, 22MB with
gzip compression in 8 secs, 19MB with
bzip2 compression in 13 secs and 9.4MB with
compression in 40 secs. The load times were 1.3, 2.8, 5.5 and 5.7
seconds respectively. These results are indicative, but the relative
performances do depend on the actual file:
unusually well here.
It is possible to compress later (with
xz) a file saved with
compress = FALSE: the effect
is the same as saving with compression. Also, a saved file can be
uncompressed and re-compressed under a different compression scheme
resaveRdaFiles for a way to do so from within R).
file can be a connection can be exploited to make use of
an external parallel compression utility such as
(http://compression.ca/pbzip2/) via a
connection. For example, using 8 threads,
1 2 3 4 5 6 7 8
where the last requires
xz 5.1.1 or later built with support
for multiple threads (and parallel compression is only effective for
large objects: at level 6 it will compress in serialized chunks of 12MB).
... arguments only give the names of the objects
to be saved: they are searched for in the environment given by the
envir argument, and the actual objects given as arguments need
not be those found.
Saved R objects are binary files, even those saved with
ascii = TRUE, so ensure that they are transferred without
conversion of end-of-line markers and of 8-bit characters. The lines
are delimited by LF on all platforms.
Although the default version has not changed since R 1.4.0, this does not mean that saved files are necessarily backwards compatible. You will be able to load a saved image into an earlier version of R unless use is made of later additions (for example, raw vectors, external pointers and some S4 objects).
One such ‘later addition’ was long vectors, introduced in R 3.0.0 and loadable only on 64-bit platforms.
Loading files saved with
ASCII = NA requires a C99-compliant C
sscanf: this is a problem on Windows, first worked
around in R 3.1.2: they should be readable in earlier versions of R
on all other platforms.
The most common reason for failure is lack of write permission in the
current directory. For
save.image and for saving at the end of
a session this will shown by messages like
1 2 3 4 5
For other interfaces to the underlying serialization format, see
1 2 3 4 5 6 7 8 9 10 11