tempfile {base} | R Documentation

## Create Names for Temporary Files

### Description

Returns a vector of character strings which can be used as names for temporary files.

### Usage

    tempfile(pattern = "file")

### Arguments

`pattern` | a non-empty character vector giving the initial part of the name.

### Details

If `pattern` has length greater than one then the result is of the same length giving a temporary file name for each component of `pattern`.

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

The file name is made of the pattern, the process number in hex and a random suffix in hex. The filenames will be in the directory given by the first found of the environment variables `TMP`, `TEMP`, or `"/tmp"`.

### Value

A character vector giving the names of possible (temporary) files.

Note that no files are generated by `tempfile`.

### See Also

`unlink` for deleting files.

### Examples

    tempfile(c("ab", "a b c"))   # give file name with spaces in!
    ## One possibility of getting ALL environment variables;
    ## compare with `getenv':
    fun <- function() {
      FILE <- tempfile("fun")
      on.exit(unlink(FILE))
      system(paste("printenv >", FILE))
      x <- strsplit(scan(FILE, what = ""), "=")
      v <- n <- character(LEN <- length(x))
      for (i in 1:LEN) {
        n[i] <- x[[i]][1]
        v[i] <- paste(x[[i]][-1], collapse = "=")
      }
      structure(v, names = n)
    }

    fun()

[Package Contents]