[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  readRDS: Serialization Interface for Single Objects

##  Description

Functions to write a single  R  object to a file, and to restore it.

##  Usage

    
    
    saveRDS(object, file = "", ascii = FALSE, version = NULL,
            compress = TRUE, refhook = NULL)
    
    readRDS(file, refhook = NULL)

##  Arguments

object

R  object to serialize.

file

a [ connection ](/link/connection?package=base&version=3.6.2) or the name of
the file where the  R  object is saved to or read from.

ascii

a logical. If ` TRUE ` or ` NA ` , an ASCII representation is written;
otherwise (default), a binary one is used. See the comments in the help for `
[ save ](/link/save?package=base&version=3.6.2) ` .

version

the workspace format version to use. ` NULL ` specifies the current default
version (3). The only other supported value is 2, the default from  R  1.4.0
to  R  3.5.0.

compress

a logical specifying whether saving to a named file is to use ` "gzip" `
compression, or one of ` "gzip" ` , ` "bzip2" ` or ` "xz" ` to indicate the
type of compression to be used. Ignored if ` file ` is a connection.

refhook

a hook function for handling reference objects.

##  Value

For ` readRDS ` , an  R  object.

For ` saveRDS ` , ` NULL ` invisibly.

##  Details

These functions provide the means to save a single  R  object to a connection
(typically a file) and to restore the object, quite possibly under a different
name. This differs from ` [ save ](/link/save?package=base&version=3.6.2) `
and ` [ load ](/link/load?package=base&version=3.6.2) ` , which save and
restore one or more named objects into an environment. They are widely used by
R  itself, for example to store metadata for a package and to store the ` [
help.search ](/link/help.search?package=base&version=3.6.2) ` databases: the `
".rds" ` file extension is most often used.

Functions ` [ serialize ](/link/serialize?package=base&version=3.6.2) ` and `
[ unserialize ](/link/unserialize?package=base&version=3.6.2) ` provide a
slightly lower-level interface to serialization: objects serialized to a
connection by ` serialize ` can be read back by ` readRDS ` and conversely.

All of these interfaces use the same serialization format, which has been used
since  R  1.4.0 (but extended from time to time as new object types have been
added to  R  ). However, ` save ` writes a single line header (typically `
"RDXs\n" ` ) before the serialization of a single object (a pairlist of all
the objects to be saved).

Compression is handled by the connection opened when ` file ` is a file name,
so is only possible when ` file ` is a connection if handled by the
connection. So e.g. ` [ url ](/link/url?package=base&version=3.6.2) `
connections will need to be wrapped in a call to ` [ gzcon
](/link/gzcon?package=base&version=3.6.2) ` .

If a connection is supplied it will be opened (in binary mode) for the
duration of the function if not already open: if it is already open it must be
in binary mode for ` saveRDS(ascii = FALSE) ` or to read non-ASCII saves.

##  See Also

` [ serialize ](/link/serialize?package=base&version=3.6.2) ` , ` [ save
](/link/save?package=base&version=3.6.2) ` and ` [ load
](/link/load?package=base&version=3.6.2) ` .

The ‘R Internals’ manual for details of the format used.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2FreadRDS&utm_source=r-docs&utm_medium=docs&utm_term=readRDS&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    fil <- tempfile("women", fileext = ".rds")
    ## save a single object to file
    saveRDS(women, fil)
    ## restore it under a different name
    women2 <- readRDS(fil)
    identical(women, women2)
    ## or examine the