[Rd] Rgui never processes ~/.Renviron

Henrik Bengtsson henr|k@bengt@@on @end|ng |rom gm@||@com
Thu Sep 3 08:38:07 CEST 2020


ISSUE:
It looks like Rgui.exe never processes ~/.Renviron - only ./.Renviron.

REPRODUCIBLE EXAMPLE:
On Windows, create the following ~/.Renviron and ~/.Rprofile files:

C:\Users\alice> Rscript -e "cat('FOO=123\n', file='~/.Renviron')"
C:\Users\alice> Rscript -e "cat('print(Sys.getenv(\'FOO\'))',
file='~/.Rprofile')"

and launch Rgui (from a folder other that ~):
C:\Users\alice> Rgui

and you'll see that FOO is reported as "" (empty), whereas with R or
Rscript, it is reported as "123".


TROUBLESHOOTING:
>From code inspection
(<https://github.com/wch/r-source/blob/1658c8491e9cdc6d2fe61603ed23ae56232b6727/src/main/Renviron.c#L298-L301>):

#ifdef Win32
  {
    char buf[1024]; /* MAX_PATH is less than this */
    /* R_USER is not necessarily set yet, so we have to work harder */
    s = getenv("R_USER");
    if(!s) s = getenv("HOME");
    if(!s) return;
    snprintf(buf, 1024, "%s/.Renviron", s);
    s = buf;
  }
#endif

I think it happens because neither R_USER nor HOME is set when the
Rgui startup process calls process_user_Renviron().

WORKAROUND:
Setting R_USER or HOME prior to calling Rgui will cause Rgui to
process ~/.Renviron, e.g.

C:\User\alice> set R_USER=%UserProfile%\Documents
C:\User\alice> Rgui

The background for finding this is R-help thread '[R] tempdir() does
not respect TMPDIR' on 2020-08-29
(https://stat.ethz.ch/pipermail/r-help/2020-August/468573.html).

/Henrik



More information about the R-devel mailing list