Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

with_tempfile

Temporary files


Description

Temporarily create a tempfile, which is automatically removed afterwards.

Usage

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

local_tempfile(
  new = 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

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

# check how big iris would be if written as csv vs RDS
tf <- with_tempfile("tf", {write.csv(iris, tf); file.size(tf)})
tf <- with_tempfile("tf", {saveRDS(iris, tf); file.size(tf)})

withr

Run Code 'With' Temporarily Modified Global State

v2.4.2
MIT + file LICENSE
Authors
Jim Hester [aut, cre], Kirill Müller [aut], Kevin Ushey [aut], Hadley Wickham [aut], Winston Chang [aut], Jennifer Bryan [ctb], Richard Cotton [ctb], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.