Save multiple objects to their respective .RData
files
Source: R/General_SaveMultiple.R
SaveMultiple.Rd
This function saves specified variables from the global environment to
separate .RData
files. It allows for optional file prefixing and
overwriting of existing files.
Usage
SaveMultiple(
Vars = NULL,
OutFolder = getwd(),
Overwrite = FALSE,
Prefix = "",
Verbose = FALSE
)
Arguments
- Vars
A character vector specifying the names of the variables to be saved. If
NULL
or any specified variable does not exist in the global environment, the function will stop with an error.- OutFolder
A string specifying the path to the output folder where the
.RData
files will be saved. Defaults to the current working directory.- Overwrite
A logical value indicating whether existing
.RData
files should be overwritten. IfFALSE
(Default) and files exist, the function will stop with an error message.- Prefix
A string to be prefixed to each output file name. Useful for organizing saved files or avoiding name conflicts. Defaults to an empty string.
- Verbose
A logical value indicating whether to print a message upon successful saving of files. Defaults to
FALSE
.
Examples
if (FALSE) { # \dontrun{
TMP_Folder <- file.path(tempdir(), stringi::stri_rand_strings(1, 5))
fs::dir_create(TMP_Folder)
# ----------------------------------------------
# Save x1 and x2 to disk
# ----------------------------------------------
x1 = 10; x2 = 20
SaveMultiple(Vars = c("x1", "x2"), OutFolder = TMP_Folder)
list.files(path = TMP_Folder, pattern = "^.+.RData")
(x1Contents <- IASDT.R::LoadAs(file.path(TMP_Folder, "x1.RData")))
(x2Contents <- IASDT.R::LoadAs(file.path(TMP_Folder, "x2.RData")))
# ----------------------------------------------
# Use Prefix
# ----------------------------------------------
SaveMultiple(Vars = c("x1", "x2"), OutFolder = TMP_Folder, Prefix = "A_")
list.files(path = TMP_Folder, pattern = "^.+.RData")
# ----------------------------------------------
# File exists, no save
# ----------------------------------------------
try(SaveMultiple(Vars = c("x1", "x2"), OutFolder = TMP_Folder))
# ----------------------------------------------
# overwrite existing file
# ----------------------------------------------
x1 = 100; x2 = 200; x3 = 300
SaveMultiple(Vars = c("x1", "x2", "x3"),
OutFolder = TMP_Folder, Overwrite = TRUE)
(x1Contents <- IASDT.R::LoadAs(file.path(TMP_Folder, "x1.RData")))
(x2Contents <- IASDT.R::LoadAs(file.path(TMP_Folder, "x2.RData")))
(x3Contents <- IASDT.R::LoadAs(file.path(TMP_Folder, "x3.RData")))
} # }