From a792283169d7858ff4415a7f52bb2641a02a5d21 Mon Sep 17 00:00:00 2001 From: Will Beasley Date: Sun, 2 Feb 2020 10:55:08 -0600 Subject: [PATCH] `add = TRUE` for `on.exit()` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > Always set add = TRUE when using on.exit(). If you don’t, each call to on.exit() will overwrite the previous exit handler. Even when only registering a single handler, it’s good practice to set add = TRUE so that you won’t get any unpleasant surprises if you later add more exit handlers. https://adv-r.hadley.nz/functions.html#on-exit --- R/SWORD_files.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/SWORD_files.R b/R/SWORD_files.R index d5c634f..90dfea6 100644 --- a/R/SWORD_files.R +++ b/R/SWORD_files.R @@ -13,7 +13,7 @@ create_zip.character <- function(x, ...) { } create_zip.data.frame <- function(x, ...) { tmpdf <- tempfile(fileext = ".zip") - on.exit(file.remove(tmpdf)) + on.exit(file.remove(tmpdf), add = TRUE) tmp <- tempfile(fileext = ".zip") save(x, file = tmpdf) stopifnot(!utils::zip(tmp, tmpdf)) @@ -21,7 +21,7 @@ create_zip.data.frame <- function(x, ...) { } create_zip.list <- function(x, ...) { tmpdf <- sapply(seq_along(x), tempfile(fileext = ".zip")) - on.exit(file.remove(tmpdf)) + on.exit(file.remove(tmpdf), add = TRUE) mapply(x, tmpdf, function(x, f) save(x, file = f), SIMPLIFY = TRUE) tmp <- tempfile(fileext = ".zip") stopifnot(!utils::zip(tmp, tmpdf))