Saving and Loading R Objects

R allows you to save and load R data objects to external files.

The simplest way to save an object is with the save function. For example, we could use the following command to save the object top.5.salaries to the file ~/top.5.salaries.RData:

> save(top.5.salaries,file="~/top.5.salaries.RData")

In R, file paths are always specified with forward slashes (“/”), even on Microsoft Windows. So, to save this file to the directory C:\Documents and Settings\me\My Documents\top.5.salaries.rda, you would use the following command:

> save(top.5.salaries,
+   file="C:/Documents and Settings/me/My Documents/top.5.salaries.RData")

Note that the file argument must be explicitly named. (Nine out of 10 times, I forget to do so.) Now you can easily load this object back into R with the load function:

> load("~/top.5.salaries.RData")

Incidentally, files saved in R will work across platforms. (For example, the data files for this book were produced on Mac OS X but work on Windows and Linux.) You can save multiple objects to the same file by simply listing them in the same save command. If you want to save every object in the workspace, you can use the save.image function. (When you quit R, you will be asked if you want to save your current workspace. If you say yes, the workspace will be saved the same way as this function.)

The save function is very flexible and can be used in many different ways. You can save multiple objects, save to files or connections, and save in a variety of formats:

save(..., list =, file =, ascii =, version =, envir =,
     compress =, eval.promises =, precheck = )

You can omit any argument except the filename. The defaults for save are very sensible: objects will be saved in a compressed binary format, and existing files won’t be overwritten.

Here is a detailed description of the arguments to save.

ArgumentDescription
...A set of symbols that name the objects to be saved. (This is a variable-length argument.)
listAlternatively, you may specify the objects to be saved in a character vector.
fileSpecifies where to save the file. Both connections and filenames can be used.
asciiA logical value that indicates whether to write a human-readable representation of the data (ascii=TRUE) or a binary representation (ascii=FALSE). Default is ascii=FALSE.
versionA numeric value that indicates the file version. For R 0.99.0 through 1.3.1, use version=1. For R 1.4.0 through (at least) 2.8.1, use version=2. Default is version=2.
envirSpecifies the environment in which to find the objects to be saved. Default is the environment in which save was called (to be precise, parent.frame()).
compressA logical value that indicates whether to compress the file when saving it. (The effect is the same as running gzip on an uncompressed file.) Default is compress=TRUE for binary files (ascii=FALSE) and compress=FALSE for human-readable files (ascii=TRUE).
eval.promisesA logical value that indicates whether promise objects should be forced before saving. Default is eval.promises=TRUE.
precheckA logical value that indicates whether the save function should check if the object exists before saving (and raise an error if it is). Default is precheck=TRUE.