| 1 |
#' Temporary files and directories |
| 2 |
#' |
| 3 |
#' Temporarily create a file or directory, which will automatically deleted |
| 4 |
#' once you're finished with it. |
| 5 |
#' |
| 6 |
#' @template with |
| 7 |
#' @param new `[character vector]`\cr (Deprecated for `local_tempfile()`) Names of temporary file handles to create. |
| 8 |
#' @param envir `[environment]`\cr Deprecated in favor of `.local_envir`. |
| 9 |
#' @param clean `[logical(1)]`\cr A logical indicating if the temporary |
| 10 |
#' directory should be deleted after use (`TRUE`, default) or left alone (`FALSE`). |
| 11 |
#' @inheritParams with_collate |
| 12 |
#' @inheritParams base::tempfile |
| 13 |
#' @examples |
| 14 |
#' # local_tempfile() is the easiest to use because it returns a path |
| 15 |
#' local({ |
| 16 |
#' path1 <<- local_tempfile(lines = c("x,y", "1,2")) |
| 17 |
#' readLines(path1) |
| 18 |
#' }) |
| 19 |
#' # the file is deleted automatically |
| 20 |
#' file.exists(path1) |
| 21 |
#' |
| 22 |
#' # with_tempfile() is a bit trickier; the first argument gives the name |
| 23 |
#' # of a variable that will contain the path: |
| 24 |
#' with_tempfile("path2", { |
| 25 |
#' print(path2) |
| 26 |
#' write.csv(iris, path2) |
| 27 |
#' file.size(path2) |
| 28 |
#' }) |
| 29 |
#' |
| 30 |
#' # Note that this variable is only available in the scope of with_tempfile |
| 31 |
#' try(path2) |
| 32 |
#' @export |
| 33 |
with_tempfile <- function( |
| 34 |
new, |
| 35 |
code, |
| 36 |
envir = parent.frame(), |
| 37 |
.local_envir = parent.frame(), |
| 38 |
pattern = "file", |
| 39 |
tmpdir = tempdir(), |
| 40 |
fileext = "" |
| 41 |
) { |
| 42 |
if (!missing(envir)) { |
| 43 |
.Deprecated( |
| 44 |
msg = "`envir` argument of with_tempfile() is deprecated.\n Use `with_tempfile(.local_envir=)` instead." |
| 45 |
) |
| 46 |
.local_envir <- envir |
| 47 |
} |
| 48 |
env <- new.env(parent = .local_envir) |
| 49 |
for (f in new) { |
| 50 |
assign( |
| 51 |
f, |
| 52 |
tempfile(pattern = pattern, tmpdir = tmpdir, fileext = fileext), |
| 53 |
envir = env |
| 54 |
) |
| 55 |
} |
| 56 |
on.exit(unlink(mget(new, envir = env), recursive = TRUE)) |
| 57 |
eval(substitute(code), envir = env) |
| 58 |
} |
| 59 |
|
| 60 |
#' @rdname with_tempfile |
| 61 |
#' @param lines Optionally, supply a character vector of lines to be written to |
| 62 |
#' `path`. This is useful if you want to seed the file with some default |
| 63 |
#' content. |
| 64 |
#' @export |
| 65 |
local_tempfile <- function( |
| 66 |
new = NULL, |
| 67 |
lines = NULL, |
| 68 |
envir = parent.frame(), |
| 69 |
.local_envir = parent.frame(), |
| 70 |
pattern = "file", |
| 71 |
tmpdir = tempdir(), |
| 72 |
fileext = "" |
| 73 |
) { |
| 74 |
if (!missing(envir)) { |
| 75 |
.Deprecated( |
| 76 |
msg = "`envir` argument of local_tempfile() is deprecated.\n Use `local_tempfile(.local_envir=)` instead." |
| 77 |
) |
| 78 |
.local_envir <- envir |
| 79 |
} |
| 80 |
if (is.null(new)) { |
| 81 |
path <- tempfile(pattern = pattern, tmpdir = tmpdir, fileext = fileext) |
| 82 |
if (!is.null(lines)) { |
| 83 |
con <- file(path, open = "wb", encoding = "native.enc") |
| 84 |
defer(close(con)) |
| 85 |
|
| 86 |
writeLines(enc2utf8(lines), con, useBytes = TRUE) |
| 87 |
} |
| 88 |
|
| 89 |
defer(unlink(path, recursive = TRUE), envir = .local_envir) |
| 90 |
return(path) |
| 91 |
} |
| 92 |
|
| 93 |
.Deprecated( |
| 94 |
msg = "`new` argument of local_tempfile() is deprecated.\n Use `path <- local_tempfile()` instead." |
| 95 |
) |
| 96 |
|
| 97 |
for (f in new) { |
| 98 |
assign( |
| 99 |
f, |
| 100 |
tempfile(pattern = pattern, tmpdir = tmpdir, fileext = fileext), |
| 101 |
envir = .local_envir |
| 102 |
) |
| 103 |
} |
| 104 |
defer( |
| 105 |
unlink(mget(new, envir = .local_envir), recursive = TRUE), |
| 106 |
envir = .local_envir |
| 107 |
) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
#' @rdname with_tempfile |
| 112 |
#' @export |
| 113 |
with_tempdir <- function( |
| 114 |
code, |
| 115 |
clean = TRUE, |
| 116 |
pattern = "file", |
| 117 |
tmpdir = tempdir(), |
| 118 |
fileext = "" |
| 119 |
) { |
| 120 |
if (length(clean) > 1 || !is.logical(clean)) { |
| 121 |
stop("`clean` must be a single TRUE or FALSE", call. = FALSE) |
| 122 |
} |
| 123 |
|
| 124 |
tmp <- tempfile(pattern = pattern, tmpdir = tmpdir, fileext = fileext) |
| 125 |
|
| 126 |
dir.create(tmp) |
| 127 |
if (clean) { |
| 128 |
on.exit(unlink(tmp, recursive = TRUE), add = TRUE) |
| 129 |
} |
| 130 |
withr::with_dir(tmp, code) |
| 131 |
} |
| 132 |
|
| 133 |
#' @rdname with_tempfile |
| 134 |
#' @export |
| 135 |
local_tempdir <- function( |
| 136 |
pattern = "file", |
| 137 |
tmpdir = tempdir(), |
| 138 |
fileext = "", |
| 139 |
.local_envir = parent.frame(), |
| 140 |
clean = TRUE |
| 141 |
) { |
| 142 |
if (length(clean) > 1 || !is.logical(clean)) { |
| 143 |
stop("`clean` must be a single TRUE or FALSE", call. = FALSE) |
| 144 |
} |
| 145 |
|
| 146 |
path <- tempfile(pattern = pattern, tmpdir = tmpdir, fileext = fileext) |
| 147 |
|
| 148 |
dir.create(path, recursive = TRUE) |
| 149 |
|
| 150 |
if (isTRUE(clean)) { |
| 151 |
defer(unlink(path, recursive = TRUE), envir = .local_envir) |
| 152 |
} |
| 153 |
|
| 154 |
path |
| 155 |
} |