Help regarding package installation: .Renviron, .Rprofile, R_LIBS, R_LIBS_SITE, and R_LIBS_USER Oh my!

For these things, I use (which works on all platforms and after upgrading R):

  1. Set R_LIBS_USER in your ~/.Renviron. One <name>=<value> per line.
  2. Don't hardcode the R version or architecture. Instead make use of so-called "specifiers", which include %p (expands to the architecture, e.g. x86_64-pc-linux-gnu) and %v (expands to major and minor R version, e.g. 3.5) - see ?R_LIBS_USER. Using R_LIBS_USER=~/R/%p-library/%v corresponds to the default R settings.

To know the location of ~/.Renviron, which is not always obvious on Windows, use normalizePath(), e.g.

 > normalizePath("~/.Renviron", mustWork = FALSE)
[1] "/home/hb/.Renviron"

That's on my Linux machine. On your Windows, I think you'll get something like:

 > normalizePath("~/.Renviron", mustWork = FALSE)
 [1] "C:/users/Greg/documents/.Renviron"

Now, if you edit/create that ~/.Renviron file to have a line:

R_LIBS_USER="C:/Users/Greg/Software/R/%p-library/%v

you will get:

> Sys.getenv('R_LIBS_USER')
[1] "C:/Users/Greg/Software/R/win-library/3.5"

and on R 3.6.0 devel, you'll get:

> Sys.getenv('R_LIBS_USER')
[1] "C:/Users/Greg/Software/R/win-library/3.6"

Importantly, if this folder does not exist, then R will silently ignore it, despite properly parsing and expanding R_LIBS_USER, e.g.

> Sys.getenv('R_LIBS_USER')
[1] "C:/Users/Greg/Software/R/win-library/3.5"
> dir.exists(Sys.getenv('R_LIBS_USER'))
[1] FALSE

and .libPaths()[1] will be incorrect. So, you need to create this folder once, e.g.

> Sys.getenv('R_LIBS_USER')
[1] "C:/Users/Greg/Software/R/win-library/3.5"
> dir.create(Sys.getenv('R_LIBS_USER'), recursive = TRUE)
> dir.exists(Sys.getenv('R_LIBS_USER'))
[1] TRUE

Then restart R, and you'll get:

> .libPaths()[1]
[1] "C:/Users/Greg/Software/R/win-library/3.5"
13 Likes