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?
You can see all the library paths installed for your user by issuing the . libPaths() command in the R terminal. If you have multiple libraries installed, packages will be loaded in the order specified by . libPaths().
To do this two steps needed: Create the file named Renviron (without dot) in the folder \Program\etc\ (Program is the directory where R is installed--for example, for me it was C:\Program Files\R\R-4.0. 0\etc) Insert a line in Renviron with new path: R_LIBS_USER = "C:/R/Library"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With