save, save.image, load

Save a particular set of objects using save, or save all objects in the current R session via save.image. Load an R data file into a session using load.
save(…, file)
  • – List all objects that should be saved.
  • file – Provide the file name, typically ending in .rda. Also, because the list of objects to save is arbitrarily long, the file argument must begin with the argument name, i.e. include file= in this argument’s declaration.
  • file – Provide the file name, typically ending in .rda or .RData.
  • file – The name of the file to be loaded.
In each case, the file name may also include a path to the file.

Example. The code below has three main components: (1) Create three objects. (2) Save two of those objects and then delete all objects from the current session. (3) Reload the file and verify the two objects reloaded.
> x <- 1:10
> y <- runif(5)
> f <- function(x){ x^2 }
> save(y, f, file="temp.rda")
> rm(list=ls()) # remove all objects
> load("temp.rda")
> ls()
[1] "f" "y"
Tip 1. Saving and reloading data, results, and other objects prevents the need to rerun code. Whenever writing code that takes any meaningful amount of time to run, save the result.
Tip 2. Organize code and saved files carefully. For instance, it is advisable to have a folder for code in a project (e.g. analysis) that contains scripts. This folder can then contain subfolders (e.g. data, results, figures) that can help keep a project and analysis organized.

Leave a Reply