Press "Enter" to skip to content

When do you need a temporary directory and files in R, and how to use them?

When temporary files in R become handy

Every time you need to download a file from anywhere to extract data from it and then throw it away, you have to decide where to save this file. There could be many options, such as keeping it in the working or home directories. It looks simple, but you must check whether a file with a similar name already exists. Then you need to hold a list of all temporary files to remove them later.

How to use tempdir() and tempfile()

R provides you with a particular infrastructure for temp files. When you start a new session, R creates a temporary directory. The location of the directory depends on your system and configuration. Function tempdir() returns the path to the current temporary directory.

On GNU/Linux system, its output could look like this:

tempdir()
[1] "/tmp/RtmpQlfeCO"

R cleans up this directory on normal exit, so don’t put your nonreproducible data there.

Now you know the place where to hold temporary stuff.

The next question is how to name temporary files. You can hardcode names in your script, but again, using a specialized R tool is more convenient.

Function tempfile() generates file names that are likely to be unique among calls to ‘tempfile’ in an R session. The function takes three arguments: the file name pattern, the directory name and the file extension. By default, you get a filename with a pattern file without extension located in the per-session temporary directory.

tempfile()
[1] "/tmp/RtmpQlfeCO/file1f0d1fd5e3ba"

tempfile(pattern = "", fileext = ".csv")
[1] "/tmp/RtmpQlfeCO/1f0d27e09a1c.csv"

You can use file names from tempfile() to save your temporary files. When you don’t need them anymore, you can delete them with unlink() R function.

Originally Alexander Matrunich wrote this post for Rstat.Consulting.

Leave a Reply

Your email address will not be published. Required fields are marked *

Защита от спама *