Skip to contents

Create a temporary directory that can safely be removed.

Usage

create_tempdir(prefix = "tempdir")

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

A character string with the absolute normalised path to the created temporary directory, returned invisibly, see the section 'Notes on Paths' below.

An error is thrown when attempting to create subdirectories (see 'Details') and if creating the directory fails.

Details

The new directory is created inside the directory returned by 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 appears to have a file extension, see the section Source in help("tempdir").

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.

Notes on Paths

The file separator is a backslash (\) on Windows but a forward slash (/) on other operating systems: use file.path() or fs::path() to construct paths in a platform-independent way and .Platform$file.sep to get the file separator used on the current platform.

Furthermore, the backslash is also used as an escape character in R, such that backslashes need to be escaped in R code by doubling them. Use cat(x) to omit the escape characters to see how x would be printed.

The working directory can be indicated by a single dot (".") and the parent directory can be indicated by two dots ("..").

relative paths start from the working directory. In contrast, absolute paths start from the root directory and thus give the complete path, such that the path still points to the same directory if the working directory changes.

Paths can be normalised, e.g., through normalizePath() or fs::path_norm() (which is also called by fs::path_abs()). On case-insensitive file systems, this also adjusts the case to match names of directories that are already present.

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 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::local_dir() and usethis::local_project() to change the working directory to a temporary directory (use withr::deferred_run() to change back to the normal project in interactive use); 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/RtmpmaAUni"
# Create a directory inside the directory returned by 'tempdir()'
(tempdir_ex1 <- create_tempdir(prefix = "subtempdir"))
#> [1] "/tmp/RtmpmaAUni/subtempdir1aa9476bbef9"

# Using the same 'prefix' again creates another directory
(tempdir_ex2 <- create_tempdir(prefix = "subtempdir"))
#> [1] "/tmp/RtmpmaAUni/subtempdir1aa92f04e0d0"

# 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(tempdir_ex1, tempdir_ex2), recursive = TRUE)
rm(tempdir_ex1, tempdir_ex2)