8

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?

Community
  • 1
  • 1
tomka
  • 2,516
  • 7
  • 31
  • 45
  • 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 Aug 18 '16 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 Aug 18 '16 at 14:27

1 Answers1

16

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.

csgillespie
  • 59,189
  • 14
  • 150
  • 185