R/backup.R

Defines functions backup

Documented in backup

#' Backup an object
#'
#' Take an object and if a a copy of it with the suffix "bak" is not present on
#' the environment, it creates one (when overwrite = FALSE).
#' @param object the object to back up.
#' @param overwrite force overwriting an existing backup?
#' @keywords backup
#' @export
#' @examples
#' test <- 1:10
#' backup(test)
#' test_bak
#' test <- 11:20
#' backup(test)
#' test_bak
#' backup(test, TRUE)
#' test_bak

backup <-
  function(object, overwrite = FALSE) {

    backup_name <- paste0(deparse(substitute(object)),
                        "_bak")

    if(exists(backup_name)) {
      if (overwrite) {
        warning(paste0("Overwriting ",
                       backup_name,
                       "."))
      } else {
        stop(paste("There already exists a backup for",
                   deparse(substitute(object)),
                   ". To overwrite it use 'overwrite = TRUE'."))
      }
    }

    assign(backup_name,
           object,
           pos = parent.frame())
  }
mtcruz/mtcruzr documentation built on Dec. 26, 2019, 11:04 p.m.