ID EN
I/O & Files

load

R Base 3.6.2

Reload datasets written with the function save.

Syntax

R
load(file, envir = parent.frame(), verbose = FALSE)

Arguments

Parameter Description
file a (readable binary-mode) connection or a character string giving the name of the file to load (when tilde expansion is done).
envir the environment where the data should be loaded.
verbose should item names be printed during loading?

Return Value

A character vector of the names of objects created, invisibly.

Details

load can load R objects saved in the current or any earlier format. It can read a compressed file (see save) directly from a file or from a suitable connection (including a call to url). A not-open connection will be opened in mode "rb" and closed after use. Any connection other than a gzfile or gzcon connection will be wrapped in gzcon to allow compressed saves to be handled: note that this leaves the connection in an altered state (in particular, binary-only), and that it needs to be closed explicitly (it will not be garbage-collected). Only R objects saved in the current format (used since R 1.4.0) can be read from a connection. If no input is available on a connection a warning will be given, but any input not in the current format will result in a error. Loading from an earlier versio

Examples

Example
R
# NOT RUN {
## save all data
xx <- pi # to ensure there is some data
save(list = ls(all = TRUE), file= "all.rda")
rm(xx)

## restore the saved values to the current environment
local({
   load("all.rda")
   ls()
})

xx <- exp(1:3)
## restore the saved values to the user's workspace
load("all.rda") ## which is here *equivalent* to
## load("all.rda", .GlobalEnv)
## This however annihilates all objects in .GlobalEnv with the same names !
xx # no longer exp(1:3)
rm(xx)
attach("all.rda") # safer and will warn about masked objects w/ same name in .GlobalEnv
ls(pos = 2)
##  also typically need to cleanup the search path:
detach("file:all.rda")

## clean up (the example):
unlink("all.rda")
# }
# NOT RUN {
# }
# NOT RUN {
con <- url("http://some.where.net/R/data/example.rda")
## print the value to see what objects were created.
print(load(con))
close(con) # url() always opens the connection
# }

See Also

save download.file; further attach as wrapper for load(). For other interfaces to the underlying serialization format see unserialize and readRDS.