| tempfile | R Documentation |
tempfile returns a vector of character strings which can be used asnames for temporary files.
tempfile(pattern = "file", tmpdir = tempdir(), fileext = "")tempdir(check = FALSE)
pattern | a non-empty character vector giving the initial partof the name. |
tmpdir | a non-empty character vector giving the directory name |
fileext | a non-empty character vector giving the file extension |
check |
|
The length of the result is the maximum of the lengths of the threearguments; values of shorter arguments are recycled.
The names are very likely to be unique among calls totempfilein anR session and across simultaneousR sessions (unlesstmpdir is specified). The filenames are guaranteed not to becurrently in use.
The file name is made by concatenating the path given bytmpdir, thepattern string, a random string in hex anda suffix offileext.
By default,tmpdir will be the directory given bytempdir(). This will be a subdirectory of the per-sessiontemporary directory found by the following rule when theR session isstarted. The environment variablesTMPDIR,TMP andTEMP are checked in turn and the first found which points to awritable directory is used:if none succeeds ‘/tmp’ is used. The path should not contain spaces.Note that setting any of these environment variables in theR sessionhas no effect ontempdir(): the per-session temporary directoryis created before the interpreter is started.
Fortempfile a character vector giving the names of possible(temporary) files. Note that no files are generated bytempfile.
Fortempdir, the path of the per-session temporary directory.
On Windows, both will use a backslash as the path separator.
On a Unix-alike, the value will be an absolute path (unlesstmpdir is set to a relative path), but it need not be canonical(seenormalizePath) and on macOS it often is not.
R processes forked by functions such asmclapply andmakeForkCluster in packageparallel share aper-session temporary directory. Further, the ‘guaranteed notto be currently in use’ applies only at the time of asking, and twochildren could ask simultaneously. This is circumvented by ensuringthattempfile calls in different children try different names.
The final component oftempdir() is created by the POSIX systemcallmkdtemp, or if this is not available (e.g. onWindows) a version derived from the source code of GNUglibc.
It will be of the form ‘RtmpXXXXXX’ where the last 6 charactersare replaced in a platform-specific way. POSIX only requires that thereplacements be ASCII, which allows. (so the value may appearto have a file extension) and regexp metacharacters such as+. Most commonly the replacements are from the regexppattern[A-Za-z0-9], but.has been seen.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
unlink for deleting files.
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## Show how 'check' is working on some platforms:if(exists("I'm brave") && `I'm brave` && identical(.Platform$OS.type, "unix") && grepl("^/tmp/", tempdir())) { cat("Current tempdir(): ", tempdir(), "\n") cat("Removing it :", file.remove(tempdir()), "; dir.exists(tempdir()):", dir.exists(tempdir()), "\n") cat("and now tempdir(check = TRUE) :", tempdir(check = TRUE),"\n")}Add the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.