save_rds_archive: Archive existing .RDS files

View source: R/save_rds_archive.R

save_rds_archiveR Documentation

Archive existing .RDS files

Description

This wrapper around base R saveRDS() checks if the file you attempt to save already exists. If it does, the existing file is renamed / archived (with a time stamp), and the "updated" file will be saved under the specified name. This means that existing code which depends on the file name remaining constant (e.g., readRDS() calls in other scripts) will continue to work while an archived copy of the - otherwise overwritten - file will be kept.

Please note: If the file does not already exist (i.e., if there is nothing to overwrite or archive), regular saveRDS behavior will be invoked. In such a case, all arguments except object and file will be ignored!

Usage

save_rds_archive(
  object,
  file = "",
  archive = TRUE,
  last_modified = FALSE,
  with_time = FALSE,
  archive_dir_path = NULL,
  ...
)

Arguments

object

Object to be saved

file

Name of the file (path) where the R object is saved to. Note that this wrapper function does currently not support connections.

archive

Logical - should the file be archived if it already exists (default), or should it be overwritten (regular saveRDS behavior)?

last_modified

Logical - should the file name of the archived file be appended with the "last modified" date/time of the original RDS instead of the current date/time? Defaults to FALSE.

with_time

Logical - should the file be archived with just a date suffix (default) or with a date and time suffix? Applies to both archiving and modification date. Set to TRUE if you want to keep several versions of files archived on a single day. See details.

archive_dir_path

Character - if desired, path to a dedicated archive (sub-)directory (relative to the directory of file!) where the archived file will be saved. Will be created if it does not yet exist. Defaults to NULL.

...

Additional arguments passed along to saveRDS

Details

CAUTION: Note that existing archived versions of files will still be overwritten if they have the same archived file name, i.e., archived files will not be archived. This usually happens when you use only the date suffix and call this function multiple times per day: Only the most recent archived version will be kept. If you want to keep multiple archived versions of a single file, set with_time to TRUE. This will append a time stamp to the archived file name up to the current second (virtually ruling out the possibility of duplicated file names).

Value

NULL (invisibly)

Author(s)

Lukas Feick

See Also

saveRDS

Examples

## Not run: 
x <- 5
y <- 10
z <- 20

## save to RDS
saveRDS(x, "temp.RDS")
saveRDS(y, "temp.RDS")

## "temp.RDS" is silently overwritten with y
## previous version is lost
readRDS("temp.RDS")
#> [1] 10

save_rds_archive(z, "temp.RDS")

## current version is updated
readRDS("temp.RDS")
#> [1] 20

## previous version is archived
readRDS("temp_ARCHIVED_on_2020-03-30.RDS")
#> [1] 10


## End(Not run)


STATWORX/helfRlein documentation built on Feb. 12, 2024, 2:21 a.m.