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

Hi all, I recently got a new computer (windows) and I am trying to stay on top of my file organization. Upon installing R/Rstudio and installing my first packages: install.packages("dplyr") and saw the default library installation path was into:

"c:/users/USER/documents/r/..."

Which wasn't ideal. I'd simply like to change the default package installation directory. My initial search made me think that an .Rprofile could set the proper .libPaths() but more digging lead me to believe .Renviron variables may be the way to go.

Still, some resources recommend changing R_LIBS_USER: Link

Others recommend changing R_LIBS or R_LIBS_SITE: Link

I also looked at the CRAN documents for managing libraries and couldn't really determine benefits one way or the other.

Three main questions:

  1. What are the main differences between the environment variables?
  2. Is it wrong to install all packages in the same location, or should base packages be kept in a separate directory?
  3. Will I have to move directories around when new R versions are installed?

Thanks in advance,

Greg

3 Likes

This is not an answer to your question (sorry!) but more of a follow up question... Can you explain a bit more why you don’t want to use the default library location? Understanding the constraints you’re working with might help people give you better answers.

1 Like

Understanding the constraints you’re working with might help people give you better answers.

I don't have any explicit constraints. I wanted to redirect the library install location to another folder like "c:/users/USER/software/r/libraries/..." simply because it makes more organizational sense to me than my documents folder.

I just became confused with the many different ways to change the default package location.

From the help for ?.libPaths

By default R_LIBS is unset, and R_LIBS_USER is set to
subdirectory R/win-library/x.y of the home directory,
for R x.y.z.

However the directories are only included in your library path if they exist (which they will not by default).

So I would recommend you create the directory for each minor version of R, e.g. dir.create("~/R/win-library/3.5") and install packages there. The base and recommended packages will be installed with R.

You will only need to install new packages when the minor version changes; e.g. when R 3.6.0 is released. For patch versions of R you can use the same library.

Using this approach means you don't have to worry about setting any of the environment variables yourself or dealing with any of the R startup files.

4 Likes

Thanks! This method works in relation to my windows home directory, but is there a way to set the minor version folders one level further from home e.g.:

"~/software/R/win-library/3.5"
instead of
"~/R/win-library/3.5"

From what I've gathered (which may be totally wrong):

  • R_LIBS_USER
    • Intended to list libraries for a user, which doesn't affect other users.
    • Environment variable which is read at start-up.
    • This would work for you.
  • R_LIBS_SITE
    • Intended to list libraries shared by multiple users at a site (e.g., the same computer).
    • Environment variable which is read at start-up.
    • Good if there's an admin who takes care of installs and upgrades, but not for your situation.
  • .libPaths()
    • Shows libraries in the search path or adds them.
    • Executed in an R session.
    • This would also work for you.
  • R_LIBS:
    • Environment variable which is read at start-up.
    • I have no idea of the intent behind this one.

If you'd like to use R_LIBS_USER, add an .Renviron file to your home directory that has this line:

R_LIBS_USER=c:/users/USER/software/r/libraries/3.5

If you'd like to use .libPaths() instead, add an .Rprofile file to your home directory that has this line:

.libPaths("c:/users/USER/software/r/libraries/3.5")

Either of those should work for as long as the relevant file is in your home directory.

6 Likes

Thanks for all the help everyone!

It seems that the R HOME directory is different from the windows HOME directory. Using the FAQ Q2.14 I decided to create an R_USER environment variable in my system settings located in the windows control panel:

Control Panel > System and Security > System > Advanced System settings > Environment Variables
I set the R_USER = C:\Users\Greg\Software

When I closed and reopened r and installed packages they were installed to:
C:\Users\Greg\Software\R\win-library\3.5

The output from .libPaths() is
[1] "C:/Users/Greg/Software/R/win-library/3.5" "C:/Program Files/R/R-3.5.1/library"

This is exactly what I wanted. If you recognize anything wrong with this approach please let me know, otherwise I hope this helps others who ran into this problem on windows!

4 Likes

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

usethis::edit_r_environ() is very useful for this! It will open in your editor the file.

This is super useful. I will test this method moving forward for a more dynamic switch. I did not know that specifiers could be used.