R_LIBS_USER ignored by R

Viewed 1620

I am using bash on Linux Centos. I set up the R_LIBS_USER from my $HOME/.bashrc. Also tried to set this up on $HOME/.Renviron

# in .bashrc
export R_LIBS_USER=$HOME/lib/R/site-library

cat .Renviron
R_LIBS_USER=$HOME/lib/R/site-library

I made sure R_LIBS_USER is set up properly. Echo show the proper value. However running R from this terminal that gives the proper value of R_LIBS_USER does not pick up this value.

> .libPaths()
[1] "/global/software/r/3.5.0/lib64/R/library"

The .libPaths() only shows the default library path in stead of my personal one. $HOME/lib/R/site-library is not in the output of .libPaths(). When I tried to load libraries inside $HOME/lib/site-library I got package not found error. I can add the personal path from inside R, then I can load libraries inside my personal R lib directory.

> .libPaths(c("/myhome/lib/R/site-library", .libPaths()))
> .libPaths()
[1] "/myhome/lib/R/site-library"                           
[2] "/global/software/r/3.5.0/lib64/R/library"

I have searched for two days to look for a solution. Other people had similar problems and also had no solutions. I used to be able to pick up the personal library path, but don't know what I changed that abolished this normal behavior.

2 Answers

Finally figured it out, .Renviron file does not do expansion of variable $HOME, not like those in .bashrc. R_LIBS_USER variable will be set to $HOME/lib/R/site-library. This is an invalid path when $HOME is not expanded. R will not tell you that this is wrong. Just silently move on to its normal business (I think this is a design flaw in R, no exception handling).

.libPaths("/nonexistent/path") # has no effect on the result of .libPaths()
 # except that it will maintain the default R library and remove any exisiting
 # personal library. And will not tell you anything wrong with the nonexistent
 # directory

Before the .libPath("/nonexistent/path") call, I had two elements in the vector: one /default/R/library, ether other /my/persoanl/R/lib after the assignment only the former is left. R is full of surprises.

Close and valid solution ... but you can try one more thing here to improve ... since you then would not need to maintain things in multiple places ie you can reuse your exported environment variables in .Renviron

You should be able to achieve that if you replace

R_LIBS_USER=$HOME/lib/R/site-library

with

R_LIBS_USER="${HOME}/lib/R/site-library"

the "${...}/..." (so { brackets around the variable and whole path or expression enclosed in double or single quotes) will/should make the path expansion from .Renviron succeed.

Related