EVOLUTION-MANAGER
Edit File: tempfile.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Create Names for Temporary Files</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for tempfile {base}"><tr><td>tempfile {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Create Names for Temporary Files</h2> <h3>Description</h3> <p><code>tempfile</code> returns a vector of character strings which can be used as names for temporary files. </p> <h3>Usage</h3> <pre> tempfile(pattern = "file", tmpdir = tempdir(), fileext = "") tempdir(check = FALSE) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>pattern</code></td> <td> <p>a non-empty character vector giving the initial part of the name.</p> </td></tr> <tr valign="top"><td><code>tmpdir</code></td> <td> <p>a non-empty character vector giving the directory name</p> </td></tr> <tr valign="top"><td><code>fileext</code></td> <td> <p>a non-empty character vector giving the file extension</p> </td></tr> <tr valign="top"><td><code>check</code></td> <td> <p><code><a href="logical.html">logical</a></code> indicating if <code>tmpdir()</code> should be checked and recreated if no longer valid.</p> </td></tr> </table> <h3>Details</h3> <p>The length of the result is the maximum of the lengths of the three arguments; values of shorter arguments are recycled. </p> <p>The names are very likely to be unique among calls to <code>tempfile</code> in an <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> session and across simultaneous <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> sessions (unless <code>tmpdir</code> is specified). The filenames are guaranteed not to be currently in use. </p> <p>The file name is made by concatenating the path given by <code>tmpdir</code>, the <code>pattern</code> string, a random string in hex and a suffix of <code>fileext</code>. </p> <p>By default, <code>tmpdir</code> will be the directory given by <code>tempdir()</code>. This will be a subdirectory of the per-session temporary directory found by the following rule when the <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> session is started. The environment variables <span class="env">TMPDIR</span>, <span class="env">TMP</span> and <span class="env">TEMP</span> are checked in turn and the first found which points to a writable directory is used: if none succeeds ‘<span class="file">/tmp</span>’ is used. The path should not contain spaces. Note that setting any of these environment variables in the <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> session has no effect on <code>tempdir()</code>: the per-session temporary directory is created before the interpreter is started. </p> <h3>Value</h3> <p>For <code>tempfile</code> a character vector giving the names of possible (temporary) files. Note that no files are generated by <code>tempfile</code>. </p> <p>For <code>tempdir</code>, the path of the per-session temporary directory. </p> <p>On Windows, both will use a backslash as the path separator. </p> <p>On a Unix-alike, the value will be an absolute path (unless <code>tmpdir</code> is set to a relative path), but it need not be canonical (see <code><a href="normalizePath.html">normalizePath</a></code>) and on macOS it often is not. </p> <h3>Note on parallel use</h3> <p><span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> processes forked by functions such as <code><a href="../../parallel/html/mclapply.html">mclapply</a></code> and <code><a href="../../parallel/html/makeCluster.html">makeForkCluster</a></code> in package <span class="pkg">parallel</span> 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 <code>tempfile</code> calls in different children try different names. </p> <h3>Source</h3> <p>The final component of <code>tempdir()</code> is created by the POSIX system call <code>mkdtmp</code>, or if this is not available (e.g. on Windows) a version derived from the source code of GNU <code>glibc</code>. </p> <p>It will be of the form ‘<span class="file">RtmpXXXXXX</span>’ where the last 6 characters are replaced in a platform-specific way. POSIX only requires that the replacements be ASCII, which allows <code>.</code> (so the value may appear to have a file extension) and <a href="regex.html">regexp</a> metacharacters such as <code>+</code>. Most commonly the replacements are from the <a href="regex.html">regexp</a> pattern <code>[A-Za-z0-9]</code>, but <code>.</code> <em>has</em> been seen. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="unlink.html">unlink</a></code> for deleting files.</p> <h3>Examples</h3> <pre> 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") } </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>