Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage multiple package locations (folders) in R?

Before I upgrade to R-2.14, I want to take the opportunity to rationalise the folder structure of my installed packages.

At the moment I use the R default, i.e. all new installed packages goes to R_LIBS_USER. However, I really distinguish between two classes of package:

  • Packages I use repeatedly to do my work, e.g. plyr, data.table, etc.
  • Packages I install just to experiment with (often to replicate a question or answer on StackOverflow)

Since install.packages offers one the option to specify a lib argument, this is clearly possible.

Is there an easy way to manage package locations, e.g. by creating some sensible settings / wrapper function in .RProfile or RProfile.Site?

like image 832
Andrie Avatar asked Nov 03 '11 10:11

Andrie


People also ask

How do I change the default library path for R packages?

To set environment variable R_LIBS_USER in Windows, go to the Control Panel (System Properties -> Advanced system properties -> Environment Variables -> User Variables) to a desired value (the path to your library folder), e.g.

How do I change where R packages are installed?

R uses a single package library for each installed version of R on your machine. Fortunately it is easy to modify the path where R installs your packages. To do this, you simply call the function . libPaths() and specify the library location.

Where is the R packages folder?

R packages are installed in a directory called library. The R function . libPaths() can be used to get the path to the library.


2 Answers

There are numerous options for that. The first thing I did was adapt my Rprofile.site to contain the following line, making my default library path a directory not included in my R installation.

 .libPaths(c("D:/R/Library",.libPaths())) 

This makes D:/R/Library my default path without losing the other paths. You can add two paths to that one, say D:/R/Library/Work and D:/R/Library/Test. The one that's put in the first position is the default one used if you don't specify lib in install.packages().

Then you can assign two variables in your .Rprofile.site. These ones are assigned in the base namespace, and hence always accessible and not removed by ls(). Something like

 .libwork <- 'D:/R/Library/Work'  .libtest <- 'D:/R/Library/Test' 

which allows you to install packages like:

 install.packages('aPackage',lib=.libwork) 

There are other options too I guess, but this is how I would roll.

like image 167
Joris Meys Avatar answered Sep 23 '22 22:09

Joris Meys


Hadley's excellent package devtools provides a function dev_mode.
http://www.inside-r.org/packages/cran/devtools/docs/dev_mode

Here you can find an example usage: https://gist.github.com/1150934

Basically,

dev_mode(TRUE, path = "anywhere-you-want-to-install") install.packages("anything-that-you-want-to-install") 

is a powerful way.

like image 34
kohske Avatar answered Sep 20 '22 22:09

kohske