Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly set up the library directory/path in R

Tags:

r

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?

like image 835
tomka Avatar asked Aug 18 '16 14:08

tomka


People also ask

How do I find the library path in R?

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().

How do I change the install library path in Rstudio?

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"


1 Answers

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.

like image 158
csgillespie Avatar answered Oct 02 '22 18:10

csgillespie