Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How/When is the HOME environment variable set within R

Tags:

r

Asked on superuser and got crickets, so trying here. This one seems to straddle the border of SU/SO.

In troubleshooting some kind of R configuration issue that was causing a pandoc conversion failure when trying to Knit a .Rmd on a colleague's Windows 10 machine, I noticed that the first path in .libPaths() was pointing to a path on a network directory rather than the c:/Users/[username]/R/win-library/... directory.

Running Sys.getenv() in R showed that HOMEDRIVE and HOMEPATH were (as expected) c: and \Users\[username], yet there was a HOME environment variable listed that was pointing to the network path that we'd found in .libPaths()

Running SET in a cmd shell did not list this HOME environment variable at all, so it seems to be something that R found somewhere else...

Where does R get this HOME environment variable?

FWIW: I fixed the config issue by setting a Windows User Environment variable HOME=%HOMEDRIVE%%HOMEPATH%; R then set all the other environment variables appropriately from there.

like image 700
mac Avatar asked Dec 03 '18 17:12

mac


1 Answers

R startup is somewhat complicated, but it is pretty well documented. The usual starting place is help("Startup"). The answer to your question is not documented there, but you will find this clue in the See also section:

For the definition of the ‘home’ directory on Windows see the ‘rw-FAQ’ Q2.14. It can be found from a running R by Sys.getenv("R_USER")

and indeed the cited FAQ at https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What-are-HOME-and-working-directories_003f gives us the answer:

The home directory is set as follows: If environment variable R_USER is set, its value is used. Otherwise if environment variable HOME is set, its value is used. After those two user-controllable settings, R tries to find system-defined home directories. It first tries to use the Windows "personal" directory (typically C:\Users\username\Documents). If that fails, if both environment variables HOMEDRIVE and HOMEPATH are set (and they normally are), the value is ${HOMEDRIVE}${HOMEPATH}. If all of these fail, the current working directory is used.

like image 123
Ista Avatar answered Oct 21 '22 18:10

Ista