Skip to contents

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.

Usage

LoadAs(File, nthreads = 5, ...)

Arguments

File

character; the file path of the file to be loaded.

nthreads

Number of threads to use when reading qs2 files. Default 5; see qs2::qs_read.

...

Additional arguments to be passed to the respective load functions. base::load for RData files; qs2::qs_read for qs2 files; arrow::read_feather for feather files; and base::readRDS for rds files.

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.

Author

Ahmed El-Gabbas

Examples


File <- system.file("testdata", "culcita_dat.RData", package = "lme4")

# ---------------------------------------------------------
# loading RData using base library
# ---------------------------------------------------------
(load(File))
#> [1] "culcita_dat"

ls()
#> [1] "File"        "culcita_dat"

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 <- LoadAs(File = File)

ls()
#> [1] "File"        "NewObj"      "culcita_dat"

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 <- LoadAs(TempFile)

# overwrite the file with different order of objects
save(mtcars, mtcars2, mtcars3, file = TempFile)
mtcars_all2 <- LoadAs(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"