with_tempfile: Temporary files and directories

View source: R/tempfile.R

with_tempfileR Documentation

Temporary files and directories

Description

Temporarily create a file or directory, which will automatically deleted once you're finished with it.

Usage

with_tempfile(
  new,
  code,
  envir = parent.frame(),
  .local_envir = parent.frame(),
  pattern = "file",
  tmpdir = tempdir(),
  fileext = ""
)

local_tempfile(
  new = NULL,
  lines = NULL,
  envir = parent.frame(),
  .local_envir = parent.frame(),
  pattern = "file",
  tmpdir = tempdir(),
  fileext = ""
)

with_tempdir(
  code,
  clean = TRUE,
  pattern = "file",
  tmpdir = tempdir(),
  fileext = ""
)

local_tempdir(
  pattern = "file",
  tmpdir = tempdir(),
  fileext = "",
  .local_envir = parent.frame(),
  clean = TRUE
)

Arguments

new

⁠[character vector]⁠
(Deprecated for local_tempfile()) Names of temporary file handles to create.

code

[any]
Code to execute in the temporary environment

envir

⁠[environment]⁠
Deprecated in favor of .local_envir.

.local_envir

⁠[environment]⁠
The environment to use for scoping.

pattern

a non-empty character vector giving the initial part of the name.

tmpdir

a non-empty character vector giving the directory name.

fileext

a non-empty character vector giving the file extension.

lines

Optionally, supply a character vector of lines to be written to path. This is useful if you want to seed the file with some default content.

clean

⁠[logical(1)]⁠
A logical indicating if the temporary directory should be deleted after use (TRUE, default) or left alone (FALSE).

Value

[any]
The results of the evaluation of the code argument.

See Also

withr for examples

Examples

# local_tempfile() is the easiest to use because it returns a path
local({
  path1 <<- local_tempfile(lines = c("x,y", "1,2"))
  readLines(path1)
})
# the file is deleted automatically
file.exists(path1)

# with_tempfile() is a bit trickier; the first argument gives the name
# of a variable that will contain the path:
with_tempfile("path2", {
  print(path2)
  write.csv(iris, path2)
  file.size(path2)
})

# Note that this variable is only available in the scope of with_tempfile
try(path2)

withr documentation built on Oct. 28, 2024, 5:07 p.m.