Skip to contents

This function takes a named list and saves each element of the list as a separate .RData file. The names of the list elements are used as the base for the filenames, optionally prefixed. Files are saved in the specified directory, with an option to overwrite existing files.

Usage

List2RData(List, Prefix = "", Dir = getwd(), Overwrite = FALSE)

Arguments

List

A named list object to be split into separate .RData files.

Prefix

A character string to prefix to each filename. If empty (default), no prefix is added.

Dir

The directory where the .RData files will be saved. Defaults to the current working directory.

Overwrite

A logical indicating whether to overwrite existing files. Defaults to FALSE, in which case files that already exist will not be overwritten, and a message will be printed for each such file.

Value

The function is called for its side effect of saving files and does not return a value.

Author

Ahmed El-Gabbas

Examples

# split iris data by species name
iris2 <- iris %>%
  tibble::tibble() %>%
  split(~Species)

str(iris2, 1)
#> List of 3
#>  $ setosa    : tibble [50 × 5] (S3: tbl_df/tbl/data.frame)
#>  $ versicolor: tibble [50 × 5] (S3: tbl_df/tbl/data.frame)
#>  $ virginica : tibble [50 × 5] (S3: tbl_df/tbl/data.frame)

(TMP_Folder <- file.path(tempdir(), stringi::stri_rand_strings(1, 5)))
#> [1] "/tmp/Rtmp1Wy2ry/Me8gr"
list.files(TMP_Folder)
#> character(0)

List2RData(List = iris2, Dir = TMP_Folder)
list.files(TMP_Folder)
#> [1] "setosa.RData"     "versicolor.RData" "virginica.RData"