Rev 50048 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/save.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2009 R Core Development Team% Distributed under GPL 2 or later\name{save}\alias{save}\alias{save.image}\title{Save R Objects}\description{\code{save} writes an external representation of \R objects to thespecified file. The objects can be read back from the file at a laterdate by using the function \code{load} (or \code{data} in some cases).\code{save.image()} is just a short-cut for \sQuote{save my currentworkspace},i.e., \code{save(list = ls(all=TRUE), file = ".RData")}. It is also whathappens with \code{\link{q}("yes")}.}\usage{save(\dots, list = character(0L),file = stop("'file' must be specified"),ascii = FALSE, version = NULL, envir = parent.frame(),compress = !ascii, compression_level,eval.promises = TRUE, precheck = TRUE)save.image(file = ".RData", version = NULL, ascii = FALSE,compress = !ascii, safe = TRUE)}\arguments{\item{\dots}{the names of the objects to be saved (as symbols orcharacter strings).}\item{list}{A character vector containing the names of objects to besaved.}\item{file}{a connection or the name of the file where the data will besaved. Must be a file name for workspace format version 1.}\item{ascii}{if \code{TRUE}, an ASCII representation of the data iswritten. The default value of \code{ascii} is \code{FALSE} whichleads to a more compact binary file being written.}\item{version}{the workspace format version to use. \code{NULL}specifies the current default format. The version used from \R0.99.0 to \R 1.3.1 was version 1. The default format as from \R1.4.0 is version 2.}\item{envir}{environment to search for objects to be saved.}\item{compress}{logical or character string specifying whether savingto a named file is to use compression. \code{TRUE} corresponds to\command{gzip} compression, and (from \R 2.10.0) character strings\code{"gzip"}, \code{"bzip2"} or \code{"xz"} specify thetype of compression. Ignored when \code{file} is a connectionand for workspace format version 1.}\item{compression_level}{integer: the level of compression to beused. Defaults to \code{6} for \command{gzip} compression and to\code{9} for \command{bzip2} or \command{xz} compression.}\item{eval.promises}{logical: should objects which are promises beforced before saving?}\item{precheck}{logical: should the existence of the objects bechecked before starting to save (and in particular before openingthe file/connection)? Does not apply to version 1 saves.}\item{safe}{logical. If \code{TRUE}, a temporary file is used forcreating the saved workspace. The temporary file is renamed to\code{file} if the save succeeds. This preserves an existingworkspace \code{file} if the save fails, but at the cost of usingextra disk space during the save.}}\details{The names of the objects specified either as symbols (or characterstrings) in \code{\dots} or as a character vector in \code{list} areused to look up the objects from environment \code{envir}. By default\link{promises} are evaluated, but if \code{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 anddoubles in binary save-d files, and these are portable across all \Rplatforms. (ASCII saves used to be useful for moving data betweenplatforms but are now mainly of historical interest.)Default values for the \code{ascii}, \code{compress}, \code{safe} and\code{version} arguments can be modified with the \code{save.defaults}option (used both by \code{save} and \code{save.image}), see also theexample section below. If a \code{save.image.defaults} option is setit overrides \code{save.defaults} for function \code{save.image}(which allows this to have different defaults).A connection that is not already open will be opened in mode\code{"wb"}. Only ASCII saves can be written to text-mode connections.}\section{Compression}{Large files can be reduced considerably in size by compression. Aparticular 46MB dataset was saved as 35MB without compression in 2seconds, 22MB with \command{gzip} compression in 8 secs, 19MB with\command{bzip2} compression in 13 secs and 9.4MB with \command{xz}compression in 40 secs. The load times were 1.3, 2.8, 5.5 and 5.7seconds respectively. These results are indicative, but the relativeperformances do depend on the actual file and \command{xz} didunusually well here.It is possible to compress later (with \command{gzip}, \command{bzip2}or \command{xz}) a file saved with \code{compress = FALSE}: the effectis the same as saving with compression. Also, a saved file can beuncompressed and re-compressed under a different compression scheme(and see \code{\link{resaveRdaFiles}} for a way to do so from within \R).}\note{The most common reason for failure is lack of write permission in thecurrent directory. For \code{save.image} and for saving at the end ofa session this will shown by messages like\preformatted{Error in gzfile(file, "wb") : unable to open connectionIn addition: Warning message:In gzfile(file, "wb") :cannot open compressed file '.RDataTmp',probable reason 'Permission denied'}The defaults were changed to use compressed saves for \code{save} in2.3.0 and for \code{save.image} in 2.4.0. Any recent version of \Rcan read compressed save files, and a compressed file can beuncompressed (by \code{gzip -d}) for use with very old versions of \R.}\section{Warnings}{The \code{\dots} arguments only give the \emph{names} of the objectsto be saved: they are searched for in the environment given by the\code{envir} argument, and the actual objects given as arguments neednot be those found.Saved \R objects are binary files, even those saved with\code{ascii = TRUE}, so ensure that they are transferred withoutconversion of end of line markers and of 8-bit characters. The linesare delimited by LF on all platforms.Although the default version has not changed since \R 1.4.0, thisdoes not mean that saved files are necessarily backwards compatible.You will be able to load a saved image into an earlier version of \Runless use is made of later additions (for example, raw vectors orexternal pointers).}\seealso{\code{\link{dput}}, \code{\link{dump}}, \code{\link{load}},\code{\link{data}}.}\examples{x <- stats::runif(20)y <- list(a = 1, b = TRUE, c = "oops")save(x, y, file = "xy.Rdata")save.image()unlink("xy.Rdata")unlink(".RData")# set save defaults using option:options(save.defaults=list(ascii=TRUE, safe=FALSE))save.image()unlink(".RData")}\keyword{file}