Skip to content

Commit

Permalink
add = TRUE for on.exit()
Browse files Browse the repository at this point in the history
> 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
  • Loading branch information
wibeasley committed Feb 2, 2020
1 parent 8db7abf commit a792283
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/SWORD_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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))
return(tmp)
}
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))
Expand Down

0 comments on commit a792283

Please sign in to comment.