#' 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())
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.