tempfile returns a vector of character strings which can be used as names for temporary files.
tempfile(pattern = "file", tmpdir = tempdir(), fileext = "") tempdir()
- a non-empty character vector giving the initial part of the name.
- a non-empty character vector giving the directory name
- a non-empty character vector giving the file extension
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
pattern string, a random string in hex and a suffix of
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: 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.
tempdir, the path of the per-session temporary directory.
Note on parallel
R processes forked by functions such as
mclapply in package parallel (or multicore) 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.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
unlink for deleting files.
Documentation reproduced from R 3.0.2. License: GPL-2.