Skip to contents

This function saves an R object to a specified file path with a potentially new name. It is useful for renaming objects during the save process. The function supports saving objects in RData, qs2, feather, and rds formats. The format is determined by the extension of the file path.

Usage

SaveAs(
  InObj,
  OutObj = NULL,
  OutPath,
  nthreads = 5,
  feather_compression = "zstd",
  ...
)

Arguments

InObj

The input object to be saved. This can be an actual R object or a character string representing the name of an object.

OutObj

A character string specifying the new name for the saved RData object. This name is used when the object is loaded back into R. Default is NULL. This is required when saving RData files.

OutPath

A character string specifying the file path (ends with either *.RData, *.qs2, feather, and rds) where the object should be saved. This includes the directory and the file name.

nthreads

A character string specifying the number of threads to use when compressing data. See qs2::qs_save.

feather_compression

A character string specifying the compression algorithm to use when saving the object in the feather format. The default is "zstd". See arrow::write_feather.

...

Additional arguments to be passed to the respective save functions. base::save for RData files; qs2::qs_save for qs2 files; arrow::write_feather for feather files; and base::saveRDS for rds files.

Value

The function does not return a value but saves an object to the specified file path.

Author

Ahmed El-Gabbas

Examples

TMP_Folder <- file.path(tempdir(), stringi::stri_rand_strings(1, 5))
fs::dir_create(TMP_Folder)
list.files(TMP_Folder)
#> character(0)

# save iris data in `iris2.RData` with `iris2` object name
SaveAs(iris, "iris2", file.path(TMP_Folder, "iris2.RData"))
list.files(TMP_Folder, pattern = "^.+.RData")
#> [1] "iris2.RData"

(load(file.path(TMP_Folder, "iris2.RData")))
#> [1] "iris2"

tibble::tibble(iris2)
#> # A tibble: 150 × 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  1          5.1         3.5          1.4         0.2 setosa 
#>  2          4.9         3            1.4         0.2 setosa 
#>  3          4.7         3.2          1.3         0.2 setosa 
#>  4          4.6         3.1          1.5         0.2 setosa 
#>  5          5           3.6          1.4         0.2 setosa 
#>  6          5.4         3.9          1.7         0.4 setosa 
#>  7          4.6         3.4          1.4         0.3 setosa 
#>  8          5           3.4          1.5         0.2 setosa 
#>  9          4.4         2.9          1.4         0.2 setosa 
#> 10          4.9         3.1          1.5         0.1 setosa 
#> # ℹ 140 more rows