| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/tempfile.R |
| 3 |
\name{with_tempfile} |
| 4 |
\alias{with_tempfile} |
| 5 |
\alias{local_tempfile} |
| 6 |
\alias{with_tempdir} |
| 7 |
\alias{local_tempdir} |
| 8 |
\title{Temporary files and directories} |
| 9 |
\usage{ |
| 10 |
with_tempfile( |
| 11 |
new, |
| 12 |
code, |
| 13 |
envir = parent.frame(), |
| 14 |
.local_envir = parent.frame(), |
| 15 |
pattern = "file", |
| 16 |
tmpdir = tempdir(), |
| 17 |
fileext = "" |
| 18 |
) |
| 19 |
|
| 20 |
local_tempfile( |
| 21 |
new = NULL, |
| 22 |
lines = NULL, |
| 23 |
envir = parent.frame(), |
| 24 |
.local_envir = parent.frame(), |
| 25 |
pattern = "file", |
| 26 |
tmpdir = tempdir(), |
| 27 |
fileext = "" |
| 28 |
) |
| 29 |
|
| 30 |
with_tempdir( |
| 31 |
code, |
| 32 |
clean = TRUE, |
| 33 |
pattern = "file", |
| 34 |
tmpdir = tempdir(), |
| 35 |
fileext = "" |
| 36 |
) |
| 37 |
|
| 38 |
local_tempdir( |
| 39 |
pattern = "file", |
| 40 |
tmpdir = tempdir(), |
| 41 |
fileext = "", |
| 42 |
.local_envir = parent.frame(), |
| 43 |
clean = TRUE |
| 44 |
) |
| 45 |
} |
| 46 |
\arguments{ |
| 47 |
\item{new}{\verb{[character vector]}\cr (Deprecated for \code{local_tempfile()}) Names of temporary file handles to create.} |
| 48 |
|
| 49 |
\item{code}{\code{[any]}\cr Code to execute in the temporary environment} |
| 50 |
|
| 51 |
\item{envir}{\verb{[environment]}\cr Deprecated in favor of \code{.local_envir}.} |
| 52 |
|
| 53 |
\item{.local_envir}{\verb{[environment]}\cr The environment to use for scoping.} |
| 54 |
|
| 55 |
\item{pattern}{a non-empty character vector giving the initial part |
| 56 |
of the name.} |
| 57 |
|
| 58 |
\item{tmpdir}{a non-empty character vector giving the directory name.} |
| 59 |
|
| 60 |
\item{fileext}{a non-empty character vector giving the file extension.} |
| 61 |
|
| 62 |
\item{lines}{Optionally, supply a character vector of lines to be written to |
| 63 |
\code{path}. This is useful if you want to seed the file with some default |
| 64 |
content.} |
| 65 |
|
| 66 |
\item{clean}{\verb{[logical(1)]}\cr A logical indicating if the temporary |
| 67 |
directory should be deleted after use (\code{TRUE}, default) or left alone (\code{FALSE}).} |
| 68 |
} |
| 69 |
\value{ |
| 70 |
\code{[any]}\cr The results of the evaluation of the \code{code} |
| 71 |
argument. |
| 72 |
} |
| 73 |
\description{ |
| 74 |
Temporarily create a file or directory, which will automatically deleted |
| 75 |
once you're finished with it. |
| 76 |
} |
| 77 |
\examples{ |
| 78 |
# local_tempfile() is the easiest to use because it returns a path |
| 79 |
local({ |
| 80 |
path1 <<- local_tempfile(lines = c("x,y", "1,2")) |
| 81 |
readLines(path1) |
| 82 |
}) |
| 83 |
# the file is deleted automatically |
| 84 |
file.exists(path1) |
| 85 |
|
| 86 |
# with_tempfile() is a bit trickier; the first argument gives the name |
| 87 |
# of a variable that will contain the path: |
| 88 |
with_tempfile("path2", { |
| 89 |
print(path2) |
| 90 |
write.csv(iris, path2) |
| 91 |
file.size(path2) |
| 92 |
}) |
| 93 |
|
| 94 |
# Note that this variable is only available in the scope of with_tempfile |
| 95 |
try(path2) |
| 96 |
} |
| 97 |
\seealso{ |
| 98 |
\code{\link{withr}} for examples |
| 99 |
} |