Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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)

If you install packages using apt-get, you will be the sudo user, so libraries will install in the system-wide location (usually /usr/lib/R).

When you use CRAN you may also be sudo or a normal user. In the first case packages will install system-wide (over-writing files installed using apt-get); in the second case, packages will install to the user's $HOME/R.

Where a package exists in both /usr/lib/R and $HOME/R, the first location in .libPaths() will be used on loading (which is usually $HOME/R).

EDIT: as Dirk pointed out, system-wide installs should go to /usr/local/lib/R/, not /usr/lib/R. However, my general point stands: installation location depends on user permissions and order of loading depends on .libPaths().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top