Create a temporary directory that can safely be removed.
Arguments
- prefix
character string with the initial part of the name of the new temporary directory, only containing characters that are valid in a path.
Value
The absolute normalized path to the created temporary directory, returned invisibly.
Details
The new directory is created inside tempdir(). Its
name starts with the string given by prefix and is followed
by a random string. This random string might contain a dot (.)
such that the directory might appear to have a file extension, see the
section Source in help("tempdir"). An error is thrown if creating the
directory fails.
It is not possible to create recursive subdirectories (e.g.,
tempdir/subtempdir/otherdir, see the Examples) because
removing such directories might remove files that are still
needed by other processes.
Although the temporary directory given by tempdir() will normally be
automatically removed when R quits (and operating
systems might
periodically
empty the temporary directory), the created subdirectories should be
removed once they are not needed anymore, see the section
Usage in practice below.
Usage in practice
Examples and tests should write to a temporary directory that is cleaned
up afterwards (otherwise R cmd check
will issue a Note about detritus in the temp directory
and CRAN will not accept your package, see section Source packages from the
CRAN policies).
Although tempdir() points to a temporary
directory, that directory should not be removed because other processes
in R and RStudio also use
it. Instead, create a temporary subdirectory in tempdir() and afterwards
clean up by removing that subdirectory:
my_tempdir <- create_tempdir(prefix = "subtempdir")
< do stuff >
unlink(my_tempdir, recursive = TRUE)It is good practice to create the complete temporary directory with all required files and folders before running any test for the presence or absence of particular files or folders: this ensures no spurious matches occur if examples or tests are removed or added.
Side effects
The directory indicated by the returned path is created if it does not yet exist.
Programming notes
The output of tempdir() during
R CMD checks on MacOS contains successive
forward slashes (e.g., /var/[...]/T//RtmpxC2Fyl/working_dir/RtmpdnqgUR)
which in earlier versions of is_path() (then in package
progutils) led to
spurious warnings about duplicated file separators.
See also
checkinput::is_path() to check if a path is valid, with a Note on paths
and extensive references about file paths and directories;
create_dir() to create a (non-temporary) directory if it does not yet exist;
local() and
withr::local_tempdir()
for automated deletion of temporary directories;
withr::withr::local_dir()
and usethis::local_project()
to change the working directory to a temporary directory;
tempfile() used in this function to create the paths for the temporary
directory;
Other functions to handle paths and directories:
create_dir(),
create_file_path(),
get_file_path()
Examples
tempdir(check = TRUE)
#> [1] "/tmp/Rtmp31i27G"
# Create a directory inside the directory returned by 'tempdir()'
(my_subtempdir_ex1 <- create_tempdir(prefix = "subtempdir"))
#> [1] "/tmp/Rtmp31i27G/subtempdir1a6246ae7578"
# Using the same 'prefix' again creates another directory
(my_subtempdir_ex2 <- create_tempdir(prefix = "subtempdir"))
#> [1] "/tmp/Rtmp31i27G/subtempdir1a625a659ed8"
# It is not possible to create recursive subdirectories
try(no_subtempdir <- create_tempdir(prefix = "subtempdir/otherdir"))
#> Error in create_tempdir(prefix = "subtempdir/otherdir") :
#> 'prefix' should not include file separators
try(no_subtempdir <- create_tempdir(prefix = "subtempdir\\otherdir"))
#> Error in create_tempdir(prefix = "subtempdir\\otherdir") :
#> 'prefix' should not include file separators
# Clean up
unlink(c(my_subtempdir_ex1, my_subtempdir_ex2), recursive = TRUE)
rm(my_subtempdir_ex1, my_subtempdir_ex2)