readRDS
Serialization Interface for Single Objects
Description
Load or save a single object from or to a file or connection (respectively).
The file normally has extension name "rds" or "rdx".
Usage
readRDS(file, refhook = NULL)
saveRDS(object, file = "", ascii = FALSE, version = NULL,
compress = TRUE, refhook = NULL)
Arguments
file |
a character string specifying the name of a file or a connection that a
single object is saved to or read from. The string can be NULL
for saveRDS or a raw vector for readRDS. An empty string
is not allowed.
|
refhook |
a hook function to handle reference objects.
|
object |
any object to be saved to file.
|
ascii |
a logical value. If TRUE, the file format is ASCII. If
FALSE (the default), the format is binary.
|
version |
the serialization format version to use. This may be NULL or 2
or 3. If it is NULL, the version defaults to 3. This default
can be changed by setting the environment variable
R_DEFAULT_SERIALIZE_VERSION to "2".
|
compress |
a logical value or character string to specify the compression mode
if file is a file name. If TRUE (the default), the
corresponding file is compressed in gzip compression, the string "bzip"
specifies bzip2 compression, and the string "xz" specifies xz compression.
Non-missing compress is ignored with a warning
if file is a file connection.
|
Details
readRDS unserializes an object value from a file
or connection.
saveRDS serializes an object value
to a file or connection.
Value
readRDS | returns an object that is unserialized
from a file or connection. |
saveRDS | returns an invisible NULL. |
Note
readRDS and saveRDS are not like load and
save, which save and restore one or more named objects
to or from a file or connection (respectively).
See Also
Examples
# Create an object:
first5primes <- c(2, 3, 5, 7, 11)
# Save the object to a file:
tfile <- tempfile()
saveRDS(first5primes, tfile)
# Restore with a different name:
p5 <- readRDS(tfile)
identical(p5, first5primes)
## [1] TRUE
# A complicated object is saved to a connection
lm.freeny <- lm(y ~ ., data=Sdatasets::freeny)
tfreeny <- tempfile()
freeny.con <- bzfile(tfreeny, "wb")
saveRDS(lm.freeny, freeny.con)
close(freeny.con)
# Restore with different name.
lm.freeny1 <- readRDS(tfreeny)
identical(lm.freeny1, lm.freeny)
## [1] TRUE