Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The same R package in multiple libraries

Tags:

r

cran

What is supposed to happen if a package is installed in multiple libraries? For example, in Debian/Ubuntu one can install debianized packages through apt-get, and then also install a more recent version of the same package straight from CRAN. When using library(), will the most recent package be loaded, or does it depend on the order of .libPaths()?

like image 367
Jeroen Ooms Avatar asked Jan 08 '12 21:01

Jeroen Ooms


People also ask

What happens if you install a package twice in R?

It shouldn't be an issue unless you install a package as an admin user, and again as a normal user. Then you will have a version in two different locations on your system. That could lead to issues when upgrading, or confusion as to which version is loaded.

How do I install multiple libraries in R?

You can install multiple packages by passing a vector of package names to the function, for example, install. packages(c("dplyr", "stringr")) . That function will install the requested packages, along with any of their non-optional dependencies.

How do I use different libraries in R?

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.

What is the difference between package and library in R?

In R, a package is a collection of R functions, data and compiled code. The location where the packages are stored is called the library. If there is a particular functionality that you require, you can download the package from the appropriate site and it will be stored in your library.


2 Answers

As already stated by others, .libPaths() search order matter which is why we set it such that local packages are searched first as the distro versions, especially with Debian stable or Ubuntu releases that are not updated, are more likely to be older.

There is a comment to this effect in the file /etc/R/Renviron setting it:

# edd Apr 2003  Allow local install in /usr/local, also add a directory for
#               Debian packaged CRAN packages, and finally the default dir 
# edd Jul 2007  Now use R_LIBS_SITE, not R_LIBS
R_LIBS_SITE=${R_LIBS_SITE-'/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library'}

So a user-set value of R_LIBS_SITE would get precedence, else the value shown here is used.

like image 68
Dirk Eddelbuettel Avatar answered Nov 08 '22 01:11

Dirk Eddelbuettel


My understanding is that it would depend on the order of .libPaths(). This is code from library

if (!missing(package)) {
        if (is.null(lib.loc)) 
            lib.loc <- .libPaths()
        lib.loc <- lib.loc[file.info(lib.loc)$isdir %in% TRUE]
    #  >>>> snipped code
        newpackage <- is.na(match(pkgname, search()))
        if (newpackage) {
            pkgpath <- find.package(package, lib.loc, quiet = TRUE, 
                verbose = verbose)
            if (length(pkgpath) == 0L) {
          # snipped

And this is from the help page for find.package

Details

find.package returns path to the locations where the given packages are found. 
If lib.loc is NULL, then attached packages are searched before the libraries. 
If a package is found more than once, the first match is used. 

And if there are more than one instance, then there should be a warning based on my reading of the find.package code (unless you set "verbose" to be FALSE):

if (length(paths) > 1L) {
            paths <- paths[1L]
            if (verbose) 
                warning(gettextf("package %s found more than once,\n
                                 using the one found in %s", 
                  sQuote(pkg), sQuote(paths)), domain = NA)
like image 5
IRTFM Avatar answered Nov 08 '22 00:11

IRTFM