Hack-R Hack-R - 3 months ago 9
R Question

Rscript uses C:/ for temp files even though TMP and TMPDIR set to another drive

My .Renviron file specifies

TMP = 'F:\temp'
TMPDIR = 'F:\temp'


and in RStudio I get the following results:

Sys.getenv("TMP")



[1] "F:\temp"


Sys.getenv("TMPDIR")



[1] "F:\temp"


That part is the way it should be. I have 1 TB of free space on F:/ and less than 50 MB free on C:/.

However, when I run a script via Rscript as a scheduled task it always fails because it somehow reverts to trying to use the C:/ for temporary files and runs out of space.

How can I fix this?

In the current use case the major user of temp files is
fread.sas7bdat.parso
from the package
sas7bdat.parso
. I tried setting
tempfile=("F://temp")
in that function call, but it made no difference.

Answer

Set them system-wide via the System Properties --> Environment Variables so they are set before R is started. That is what Rstudio accomplished: it runs as a GUI/IDE before it launches the underlying R process.

Windows Illustration: enter image description here

Linux Illustration:

edd@max:~$ TMP=/opt TEMPDIR=/src Rscript -e \   # line broken for display only
                       'print(Sys.getenv("TMP")); print(Sys.getenv("TEMPDIR"))'
[1] "/opt"
[1] "/src"
edd@max:~$ 
Comments