HenrikBengtsson / startup

:wrench: R package: startup - Friendly R Startup Configuration
https://henrikbengtsson.github.io/startup/
160 stars 5 forks source link

Assert .Renviron files were actually processed #93

Closed HenrikBengtsson closed 3 years ago

HenrikBengtsson commented 3 years ago

When running in debug mode, check that the variables declared in the .Renviron file are actually set and available. If not, produce an informative warning about the discrepancy.

This is related to a finding about This that was reported on R-help.

HenrikBengtsson commented 3 years ago

Background: R-help thread '[R] tempdir() does not respect TMPDIR' on 2020-08-29.

Using startup::startup(debug=TRUE) confirms that when launching the Windows Rgui by double-clicking on an .RData file, or equivalently explicitly launching it as:

C:\Users\hb>Rgui --workspace=C:\Users\hb\Documents\test\.RData

will result in R not processing ~/.Renviron (unless the .Rdata file is in that directory too):

R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
...
0.000s: System information:
0.000s: - R_HOME: 'C:/PROGRA~1/R/R-40~1.2' (existing folder)
0.000s: - R call: C:\Program Files\R\R-4.0.2\bin\x64\RGui.exe --workspace=C:\Users\hb\Documents\test\.RData
0.000s: - Current directory: 'C:/Users/hb/Documents/test'
0.000s: - User's home directory: '~' => 'C:\Users\hb\Documents' (existing folder)
0.000s: - Search path: '.GlobalEnv', 'package:methods', 'Autoloads', 'package:base'
0.000s: - Loaded namespaces: 'startup', 'methods', 'base'
0.000s: The following has already been processed by R:
0.000s: - R_ENVIRON: ''
0.000s: - R_ENVIRON_USER: ''
0.016s: - [WARNING: It appears that R never processed this file] '~/.Renviron' => 'C:\Users\hb\Documents/.Renviron' (3 lines; 48 bytes) setting 3 environment variables ('FOO', 'YO', 'TMPDIR')
0.016s: - R_DEFAULT_PACKAGES: '' (= 'base,methods,datasets,utils,grDevices,graphics,stats')
0.016s: - R_LIBS: ''
0.016s: - R_LIBS_SITE: ''
0.016s: - R_LIBS_USER: 'C:\Users\hb\Documents/R/win-library/4.0'
0.016s: - R_PROFILE: ''
0.016s: - R_PROFILE_USER: ''
0.016s: - 'C:/PROGRA~1/R/R-40~1.2/etc/Rprofile.site' (1 code lines; 589 bytes)
0.016s: - '~/.Rprofile' => 'C:\Users\hb\Documents/.Rprofile' (5 code lines; 355 bytes)
...
HenrikBengtsson commented 3 years ago

This Rgui problem is even simpler and is independent of using an .RData file or not. It's simply the fact that if Rgui never processes the ~/.Renviron file unless it is launched from there.

Some more trial-and-error troubleshooting suggest that this is because when Rgui calls C function process_user_Renviron() neither R_USER or HOME is set, which is required on Windows to find ~ in ~/.Renviron;

https://github.com/wch/r-source/blob/1658c8491e9cdc6d2fe61603ed23ae56232b6727/src/main/Renviron.c#L298-L301

One workaround is to set either of those before launching Rgui, e.g.

C:\Users\hb>set R_USER=%UserProfile%\Documents
C:\Users\hb>Rgui

or

C:\Users\hb>set HOME=%UserProfile%\Documents
C:\Users\hb>Rgui

Obviously, another solution is to avoid this problem all along by setting:

C:\Users\hb>set R_ENVIRON_USER=%UserProfile%\Documents
C:\Users\hb>Rgui
HenrikBengtsson commented 3 years ago

Bug reported to R-devel thread 'Rgui never processes ~/.Renviron' on 2020-09-03.