tempfile {base} | R Documentation

## Create Names for Temporary Files

### Description

`tempfile` returns a vector of character strings which can be used as names for temporary files.

### Usage

    tempfile(pattern = "file", tmpdir = tempdir(), fileext = "")
    tempdir()

### Arguments

`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

### Details

The length of the result is the maximum of the lengths of the three arguments; values of shorter arguments are recycled.

The names are very likely to be unique among calls to `tempfile` in an R session and across simultaneous R sessions (unless `tmpdir` is specified). The filenames are guaranteed not to be currently in use.

The file name is made by concatenating the path given by `tmpdir`, the `pattern` string, a random string in hex and a suffix of `fileext`.

By default, `tmpdir` will be the directory given by `tempdir()`. This will be a subdirectory of the per-session temporary directory found by the following rule when the R session is started. The environment variables TMPDIR, TMP and TEMP are checked in turn and the first found which points to a writable directory is used: if none succeeds the value of R_USER (see `Rconsole`) is used. If the path to the directory contains a space in any of the components, the path returned will use the shortnames version of the path. Note that setting any of these environment variables in the R session has no effect on `tempdir()`: the per-session temporary directory is created before the interpreter is started.

### Value

For `tempfile` a character vector giving the names of possible (temporary) files. Note that no files are generated by `tempfile`.

For `tempdir`, the path of the per-session temporary directory.

Both will use backslash as the path separator.

### Note on parallel

R processes forked by functions such as `mclapply` and `makeForkCluster` in package parallel share a per-session temporary directory. Further, the ‘guaranteed not to be currently in use’ applies only at the time of asking, and two children could ask simultaneously. This is circumvented by ensuring that `tempfile` calls in different children try different names.

### Source

The final component of `tempdir()` is created by the POSIX system call `mkdtmp`, or if this is not available (e.g. on Windows) a version derived from the source code of GNU `glibc`.

It will be of the form ‘RtmpXXXXXX’ where the last 6 characters are replaced in a platform-specific way. POSIX only requires that the replacements be ASCII, which allows `.` (so the value may appear to have a file extension) and regexp metacharacters such as `+`. Most commonly the replacements are from the regexp pattern `[A-Za-z0-9]`, but `.` has been seen.

### References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

### See Also

`unlink` for deleting files.

### Examples

    tempfile(c("ab", "a b c"))   # give file name with spaces in!

    tempfile("plot", fileext = c(".ps", ".pdf"))

    tempdir() # works on all platforms with a platform-dependent result

[Package base version 3.4.3 Index]