Skip to content

tempdir() does not respect TMPDIR

6 messages · Henrik Bengtsson, Jinsong Zhao

#
Hi there,

When I started R by double clicking on Rgui icon (I am on Windows), the 
tempdir() returned the tmpdir in the directory I set in .Renviron. If I 
started R by double clicking on a *.RData file, the tempdir() return the 
tmpdir in the directory setting by Windows system. I don't know whether 
it's designed.

 > sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
...

Best,
Jinsong
#
It is too late to set TMPDIR in .Renviron.  It is one of the
environment variables that has to be set prior to launching R.  From
help("tempfile", package = "base"):

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.**

/Henrik
On Sat, Aug 29, 2020 at 6:40 AM Jinsong Zhao <jszhao at yeah.net> wrote:
#
I read the help page, I don't understand it very well, since I set the 
environmental variable TMPDIR in .Renviron. What confused me is when 
double clicking the *.RData to launch R, the tempdir() does not respect 
the environmental variable TMPDIR, but launch R by double clicking Rgui 
icon does.

Best,
Jinsong
On 2020/8/30 0:36, Henrik Bengtsson wrote:
#
Sorry, I should retract my claim that it's too late to set TMPDIR in
.Renviron.  It does indeed work on Linux and R 4.0.2, e.g.

$ cd
$ mkdir test
$ cd test
$ echo "TMPDIR=$PWD" > ./.Renviron
$ cat ./.Renviron
TMPDIR=/home/hb/test
Rscript --no-init-file -e "tempdir()"
[1] "/home/hb/test/RtmpyH47tc"

Hmm... either this has changed "recently" or I've got it wrong all the
time.  Eitherway, I need to revise the vignette in my 'startup'
package.

Sorry for the misleading comment.

So, back to your comment about it does *not* work, that is,
~/.Renviron is not read, when you double-click on an .RData file.  I
just tried with R 4.0.2 in a Windows 10 VM and I think I can reproduce
what you're describing.

The problem seems to be that when one launches Rgui via
double-clicking .RData, the Rgui will only read ./.Renviron, that is,
the .Renviron file that is located in the same folder as the .RData
file.  It will never load ~/.Renviron (e.g.
C:/Users\alice/Documents/.Renviron) unless the .RData file is in that
folder too.

This looks odd to me but it could be that I made another mistake in my
conclusions above.  I let someone else with a less mushy brain take
over from here.

/Henrik
On Sat, Aug 29, 2020 at 4:31 PM Jinsong Zhao <jszhao at yeah.net> wrote:
#
Thanks a lot for the confirmation and explanation.
On 2020/8/31 3:45, Henrik Bengtsson wrote:
This works just becuase .Renviron is in the same directory R launched. 
It confirmed what you stated that launched Rgui via double-clicking 
.RData. Generally, we put .Renviron in R_USER (on Windows).
I should guess to that you said. When I launched R from console (cmd on 
Windows) by command like:

C:\Users\zjs>"c:\Program Files\R\R-4.0.2\bin\R.exe"
or
C:\Users\zjs>"c:\Program Files\R\R-4.0.2\bin\x64\Rgui.exe"

The tempdir() does not return the value I set in .Renviron. It means 
when R launching, it search .Renvrion in the current directory. If it 
does not get the .Renviron, it does not search it in R_USER. Am I right?

So where I set TMPDIR, R would use it whenever it launched? The purpose 
that I tried to set TMPDIR is RStudio may refuse to work when user name 
contained non-latin characters.

Thanks again.

Best,
Jinsong
#
The solution to this question is to set environmental variable R_ENVIRON 
that point to the .Renviron file. I should read the ?Startup more carefully.

Best,
Jinsong
On 2020/8/31 3:45, Henrik Bengtsson wrote: