save {base} | R Documentation

## Save R Objects

### Description

`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 `load` (or `data` in some cases).

`save.image()` is just a short-cut for “save my current environment”, i.e., `save(list = ls(all=TRUE), file = ".RData")`. It is what also happens with `q("yes")`.

### Usage

    save(..., list = character(0),
         file = stop("'file' must be specified"),
         ascii = FALSE, version = NULL, envir = parent.frame(),
         compress = !ascii)

    save.image(file = ".RData", version = NULL, ascii = FALSE,
               compress = FALSE, safe = TRUE)

### Arguments

`...` | the names of the objects to be saved.
`list` | A character vector containing the names of objects to be saved.
`file` | a connection or the name of the file where the data will be saved. Must be a file name for workspace format version 1.
`ascii` | if `TRUE`, an ASCII representation of the data is written. The default value of `ascii` is `FALSE` which leads to a more compact binary file being written.
`version` | the workspace format version to use. `NULL` specifies the current default format. The version used from R 0.99.0 to R 1.3.1 was version 1. The default format as from R 1.4.0 is version 2.
`envir` | environment to search for objects to be saved.
`compress` | logical specifying whether saving to a named file is to use compression. Ignored when `file` is a connection and for workspace format version 1.
`safe` | logical. If `TRUE`, a temporary file is used for creating the saved workspace. The temporary file is renamed to `file` if the save succeeds. This preserves an existing workspace `file` if the save fails, but at the cost of using extra disk space during the save.

### Details

The names of the objects specified either as symbols in `...` or as a character vector in `list` are used to look up the objects from environment `envir`. The objects are not evaluated, so promises are saved (together with their evaluation environments).

All R platforms use the XDR representation of binary objects 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 only of historical interest.)

Default values for the `ascii`, `compress`, `safe` and `version` arguments can be modified with the `save.defaults` option (used both by `save` and `save.image`), see also the example section below. If a `save.image.defaults` option is set it overrides `save.defaults` for function `save.image` (which allows this to have different defaults).

It is possible to compress later (with `gzip`) a file saved with `compress = FALSE`: the effect is the same as saving with `compress = TRUE`.

### Warnings

The `...` 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 conpatible. 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 or external pointers).

### See Also

`dput`, `dump`, `load`, `data`.

### Examples

    x <- 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")

[Package base version 2.3.1 Index]