9

My goal is to define a single path which R will use for installing and searching for libraries. I read that this can be done by changing the Rprofile.site file in the R installation path. I tried two commands there:

.libPaths("D:/RLibrary")
.Library.site <- file.path("D:/RLibrary")

of which I do not fully understand the difference even after reading the help files.

However after starting R, libraries are still looked for in two locations.

.libPaths()
[1] "D:/RLibrary"                        "C:/Program Files/R/R-3.3.1/library"

Why is this, and how do I change the library path to my desired path only?

2
  • Changing to your desired path only is probably not a good idea, because some of R's default packages seem to work for me only when in the default folder. But your "D:/RLibrary" folder should now be the first place that it looks for packages, which should take care of things for you.
    – mkt
    Commented Aug 18, 2016 at 14:25
  • Maybe try packrat? that will create a local project directory... which gets around a lot of the nasty library confusion issues.
    – Shape
    Commented Aug 18, 2016 at 14:27

1 Answer 1

19

I would suggest you don't want a single directory for packages, since a number of base packages come with R. Instead you want a single directory where a user will install packages.

Create a .Renviron file and add the environment variable R_LIBS pointing to the directory you want your packages to end up in. On my machine, I have

# Linux 
R_LIBS=/data/Rpackages/

Or if you have Windows something like

# Windows
R_LIBS=C:/R/library

Your .libPaths() would now look something like

R> .libPaths()
[1] "/data/Rpackages"   "/usr/lib/R/site-library"

This means that when I install a package it goes to /data/ncsg3/Rpackages


If you really want to only have a single directory, you can set the R_LIBS_SITE variable to omit the default directories.

4
  • I am confused. What is the difference to the Rprofile.site?
    – tomka
    Commented Aug 18, 2016 at 14:31
  • See csgillespie.github.io/efficientR/set-up.html - Renviron is for environment variables. Commented Aug 18, 2016 at 14:32
  • Note that for this to work, $R_LIBS needs to exist beforehand. Running mkdir -p $R_LIBS (on Linux, anyway) should do that.
    – M. Rodo
    Commented Jul 4, 2023 at 15:23
  • @csgillespie can you add more directories to R_LIBS= using a colon? I know this can be done in .libPaths() Commented Oct 11, 2023 at 19:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.