Skip to contents

This function loads multiple .RData files either into a single list object or directly into the global environment. It provides options for verbosity, returning object names, and handling of non-existent files.

Usage

LoadMultiple(
  Files = NULL,
  Verbose = TRUE,
  OneObject = TRUE,
  ReturnNames = TRUE
)

Arguments

Files

A character vector specifying the paths of .RData files to be loaded.

Verbose

A logical flag indicating whether to print messages about the loading process. Only effective when OneObject is FALSE. Defaults to TRUE.

OneObject

A logical flag indicating whether to load all objects into a single list (TRUE) or directly into the global environment (FALSE). Defaults to TRUE.

ReturnNames

A logical flag indicating whether to return the names of the loaded objects. Only effective when OneObject is FALSE. Defaults to TRUE.

Value

If OneObject is TRUE, returns a named list of all objects loaded from the specified files. If OneObject is FALSE and ReturnNames is TRUE, returns a character vector of the names of the objects loaded into the global environment. Otherwise, returns NULL.

Author

Ahmed El-Gabbas

Examples

(Files <- system.file(
   "testdata", c("culcita_dat.RData", "gopherdat2.RData"), package = "lme4"))
#> [1] "/home/runner/work/_temp/Library/lme4/testdata/culcita_dat.RData"
#> [2] "/home/runner/work/_temp/Library/lme4/testdata/gopherdat2.RData" 

ls()
#> [1] "Files"

# ---------------------------------------------------
# Load multiple *.RData files to one list object
# `OneObject = TRUE`
# ---------------------------------------------------
MultiObj <- LoadMultiple(Files = Files, OneObject = TRUE)
ls()
#> [1] "Files"    "MultiObj"

str(MultiObj, 1)
#> List of 2
#>  $ culcita_dat:'data.frame':	80 obs. of  3 variables:
#>  $ gopherdat2 :'data.frame':	30 obs. of  7 variables:

# ---------------------------------------------------
# Load multiple *.RData files separately to the current environment
# `OneObject = FALSE`
# ---------------------------------------------------
ls()
#> [1] "Files"    "MultiObj"
rm("MultiObj")
ls()
#> [1] "Files"

LoadMultiple(Files = Files, OneObject = FALSE)
#> Object: culcita_dat was loaded successfully.
#> Object: Gdat was loaded successfully.
#> [1] "culcita_dat" "Gdat"       

str(Gdat, 1)
#> 'data.frame':	30 obs. of  7 variables:
#>  $ Site   : Factor w/ 10 levels "BS","CB","Cent",..: 1 1 1 2 2 2 3 3 3 4 ...
#>  $ year   : num  2004 2005 2006 2004 2005 ...
#>  $ shells : int  0 0 0 1 0 1 0 1 1 9 ...
#>  $ type   : Factor w/ 1 level "Fresh": 1 1 1 1 1 1 1 1 1 1 ...
#>  $ Area   : num  15.2 15.2 15.2 16 16 16 6.9 6.9 6.9 43.2 ...
#>  $ density: num  4.8 4.8 4.8 2.8 2.8 2.8 29.1 29.1 29.1 4.2 ...
#>  $ prev   : num  1 1 1 4.3 8 17.6 28.6 51.9 10.7 80.7 ...

str(culcita_dat, 1)
#> 'data.frame':	80 obs. of  3 variables:
#>  $ block    : Factor w/ 10 levels "1","2","3","4",..: 1 1 2 2 3 3 4 4 5 5 ...
#>  $ predation: num  0 1 1 1 1 1 1 1 1 1 ...
#>  $ ttt      : Factor w/ 4 levels "none","crabs",..: 1 1 1 1 1 1 1 1 1 1 ...

# ---------------------------------------------------
# Load multiple *.RData files, one object already exists
# ---------------------------------------------------
ls()
#> [1] "Files"
rm("culcita_dat")
#> Warning: object 'culcita_dat' not found
ls()
#> [1] "Files"

try(LoadMultiple(Files = Files, OneObject = FALSE))
#> Object: culcita_dat was loaded successfully.
#> Object: Gdat was loaded successfully.
#> [1] "culcita_dat" "Gdat"