Change temporary directory

I had a similar problem. In my case the solutions described above didn't work. Rcpp when compiling still used the tempdir(). It was driven by the fact that my default TEMPDIR was using my Windows user folder, I have Polish letters in my user name and R does not like it.

What I found is that TEMP, TMP and TMPDIR have to be set before running R: https://cran.r-project.org/web/packages/startup/vignettes/startup-intro.html

And this should be done in the system. I used following instructions: https://answers.microsoft.com/en-us/windows/forum/windows_7-files/change-location-of-temp-files-folder-to-another/19f13330-dde1-404c-aa27-a76c0b450818

But instead of changing TEMP and TMP variables I created a TMPDIR variable in Windows. And it worked for me. After restart R points to the new tempdir() as guided by Windows TMPDIR!


Create a file called .Renviron in the directory given by Sys.getenv('R_USER') and save it with the line TMP = '<your-desired-tempdir>'.

write("TMP = '<your-desired-tempdir>'", file=file.path(Sys.getenv('R_USER'), '.Renviron'))

In windows, for me what worked is creating a file named Renviron.site and filling it with

TMPDIR=E:/rtemp 
TMP=E:/rtemp 
TEMP=E:/rtemp

Where E:/rtemp was the path to the directory where I wanted the temporary files. So you create a new text file, fill it with the above, and change its name (and extension) to Renviron.site.

Put it inside the R installation directory, in the directory etc (e.g. C:\Program Files\R\R-3.3.2\etc)

Obviously, you need to restart R studio for the change to work! (I use R studio but it should work in R also).

For me, this change allowed me to run a script of species distribution modeling which was creating very large temporary files on the system partition, consuming all the space and killing the process in the end. I have moved the temp files to a usb SSD disk (partition E:), and voila, it worked.

PS - the answer was in one of the links you mentioned.


For Linux, I'm using Ubuntu 18.04.1 LTS. You can try the following line:

write("TMP = YOUR_PATH_VARIABLE", file=file.path('~/.Renviron'))

Explanation: This line will write the TMP variable, which has been assigned to your own temp path, to the '.Renviron' file. And this '.Renviron' file will be created in your home directory. If this doesn't work, restart your R or R studio. The reason is the temporary directory was created before the current R session. So you have to restart another R session to implement this new TEMP_PATH configuration.

Tags:

R

Tempdir