Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing temporary directory in R [duplicate]

Tags:

r

temp

rstudio

I'm using a Windows virtual machine and I have both R and RStudio installed on it and I'm trying to change the directory where R writes the temporary files.

When I start R, I try changing the temporary directory and then close R. When I restart R and try tempdir() it still shows the old directory, as if nothing has happened. However, if after attempting to change the temporary directory in R I start RStudio instead of restarting R, when I try tempdir() in RStudio it shows the new/updated directory I set in R. How is this possible? What is happening? Why does only RStudio respond to my changing of the temporary directory but not R, even though that's where I'm changing the directory?

like image 522
g_puffo Avatar asked Jul 14 '16 02:07

g_puffo


2 Answers

How exactly are you setting the temporary directory? The R documentation in ?tempdir says this:

By default, tmpdir will be the directory given by tempdir(). This will be a subdirectory of the per-session temporary directory found by the following rule when the R session is started. The environment variables TMPDIR, TMP and TEMP are checked in turn and the first found which points to a writable directory is used: if none succeeds ‘/tmp’ is used. The path should not contain spaces. Note that setting any of these environment variables in the R session has no effect on tempdir(): the per-session temporary directory is created before the interpreter is started.

Presumedly, if you have the TMPDIR environment variable set, R (and hence RStudio) should just do the right thing. Note that you'll have to set before R / RStudio is launched; odds are you can accomplish this by setting it within ~/.Renviron or ~/.Rprofile.

See also: Change temporary directory

like image 174
Kevin Ushey Avatar answered Oct 21 '22 03:10

Kevin Ushey


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) and restart RStudio or R.

like image 30
Adrian Stoica Avatar answered Oct 21 '22 03:10

Adrian Stoica