Rev 68948 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/serialize.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2014 R Core Team% Distributed under GPL 2 or later\name{readRDS}\alias{readRDS}\alias{saveRDS}\title{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{\item{object}{\R object to serialize.}\item{file}{a \link{connection} or the name of the file where the \R objectis saved to or read from.}\item{ascii}{a logical. If \code{TRUE} or \code{NA}, an ASCIIrepresentation is written; otherwise (default), a binary one is used.See the comments in the help for \code{\link{save}}.}\item{version}{the workspace format version to use. \code{NULL}specifies the current default version (2). Versions prior to 2 are notsupported, so this will only be relevant when there are later versions.}\item{compress}{a logical specifying whether saving to a named file isto use \code{"gzip"} compression, or one of \code{"gzip"},\code{"bzip2"} or \code{"xz"} to indicate the type of compression tobe used. Ignored if \code{file} is a connection.}\item{refhook}{a hook function for handling reference objects.}}\details{These functions provide the means to save a single \R object to aconnection (typically a file) and to restore the object, quitepossibly under a different name. This differs from \code{\link{save}}and \code{\link{load}}, which save and restore one or more namedobjects into an environment. They are widely used by \R itself, forexample to store metadata for a package and to store the\code{\link{help.search}} databases: the \code{".rds"} file extensionis most often used.Functions \code{\link{serialize}} and \code{\link{unserialize}}provide a slightly lower-level interface to serialization: objectsserialized to a connection by \code{serialize} can be read back by\code{readRDS} and conversely.All of these interfaces use the same serialization format, which hasbeen used since \R 1.4.0 (but extended from time to time as newobject types have been added to \R). However, \code{save} writes asingle line header (typically \code{"RDXs\n"}) before theserialization of a single object (a pairlist of all the objects to besaved).Compression is handled by the connection opened when \code{file} is afile name, so is only possible when \code{file} is a connection ifhandled by the connection. So e.g.\sspace{}\code{\link{url}}connections will need to be wrapped in a call to \code{\link{gzcon}}.If a connection is supplied it will be opened (in binary mode) for theduration of the function if not already open: if it is already open itmust be in binary mode for \code{saveRDS(ascii = FALSE)} or to readnon-ASCII saves.}\value{For \code{readRDS}, an \R object.For \code{saveRDS}, \code{NULL} invisibly.}\seealso{\code{\link{serialize}}, \code{\link{save}} and \code{\link{load}}.The \sQuote{R Internals} manual for details of the format used.}\examples{## save a single object to filesaveRDS(women, "women.rds")## restore it under a different namewomen2 <- readRDS("women.rds")identical(women, women2)## or examine the object via a connection, which will be opened as needed.con <- gzfile("women.rds")readRDS(con)close(con)## Less convenient ways to restore the object## which demonstrate compatibility with unserialize()con <- gzfile("women.rds", "rb")identical(unserialize(con), women)close(con)con <- gzfile("women.rds", "rb")wm <- readBin(con, "raw", n = 1e4) # size is a guessclose(con)identical(unserialize(wm), women)## Format compatibility with serialize():con <- file("women2", "w")serialize(women, con) # ASCII, uncompressedclose(con)identical(women, readRDS("women2"))con <- bzfile("women3", "w")serialize(women, con) # binary, bzip2-compressedclose(con)identical(women, readRDS("women2"))\testonly{unlink(c("women.rds", "women2", "women3"))}}\keyword{file}\keyword{connection}