This function loads an RData file specified by the file parameter. If the
RData file contains a single object, that object is returned directly. If
the file contains multiple objects, they are returned as a list with each
object accessible by its name. This allows for flexible handling of loaded
data without needing to know the names of the objects stored within the RData
file ahead of time. The function also supports loading feather, qs2 and
rds files.
Arguments
- file
Character. the file path or URL of the file to be loaded. If
fileis a URL, the function will download the file from the URL to a temporary file and load it.- n_threads
Number of threads to use when reading
qs2files. Default 5; see qs2::qs_read.- timeout
integer; time in seconds before the download times out. Default 300 seconds; see download.file.
- ...
Additional arguments to be passed to the respective load functions. base::load for
RDatafiles; qs2::qs_read forqs2files; arrow::read_feather forfeatherfiles; and base::readRDS forrdsfiles.
Value
Depending on the contents of the RData file, this function returns
either a single R object or a named list of R objects. The names of the
list elements (if a list is returned) correspond to the names of the
objects stored within the RData file.
Examples
file <- system.file("testdata", "culcita_dat.RData", package = "lme4")
# ---------------------------------------------------------
# loading RData using base library
# ---------------------------------------------------------
(load(file))
#> [1] "culcita_dat"
ls()
#> [1] "culcita_dat" "file"
tibble::tibble(culcita_dat)
#> # A tibble: 80 × 3
#> block predation ttt
#> <fct> <dbl> <fct>
#> 1 1 0 none
#> 2 1 1 none
#> 3 2 1 none
#> 4 2 1 none
#> 5 3 1 none
#> 6 3 1 none
#> 7 4 1 none
#> 8 4 1 none
#> 9 5 1 none
#> 10 5 1 none
#> # ℹ 70 more rows
# ---------------------------------------------------------
# Loading as custom object name
# ---------------------------------------------------------
NewObj <- load_as(file = file)
ls()
#> [1] "NewObj" "culcita_dat" "file"
print(tibble::tibble(NewObj))
#> # A tibble: 80 × 3
#> block predation ttt
#> <fct> <dbl> <fct>
#> 1 1 0 none
#> 2 1 1 none
#> 3 2 1 none
#> 4 2 1 none
#> 5 3 1 none
#> 6 3 1 none
#> 7 4 1 none
#> 8 4 1 none
#> 9 5 1 none
#> 10 5 1 none
#> # ℹ 70 more rows
# ---------------------------------------------------------
# Loading multiple objects stored in single RData file
# ---------------------------------------------------------
# store three objects to single RData file
mtcars2 <- mtcars3 <- mtcars
TempFile <- tempfile(pattern = "mtcars_", fileext = ".RData")
save(mtcars2, mtcars3, mtcars, file = TempFile)
mtcars_all <- load_as(TempFile)
# overwrite the file with different order of objects
save(mtcars, mtcars2, mtcars3, file = TempFile)
mtcars_all2 <- load_as(TempFile)
# single list object with 3 items, keeping original object names and order
names(mtcars_all)
#> [1] "mtcars2" "mtcars3" "mtcars"
names(mtcars_all2)
#> [1] "mtcars" "mtcars2" "mtcars3"