The same R package in multiple libraries
Asked Answered
A

3

8

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

Aerobe answered 8/1, 2012 at 21:31 Comment(0)
R
6

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.

Rapparee answered 8/1, 2012 at 23:15 Comment(1)
+1 for the Debian-specific answer and description of the logic you used in the design.Confirmation
D
5

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)
Dendy answered 8/1, 2012 at 22:2 Comment(0)
G
0

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

Geisha answered 8/1, 2012 at 22:31 Comment(4)
That's not correct, at least not if you use the pre-made R binaries for Ubuntu or Debian as we set R_LIBS et al so that local installs always go to /usr/local/lib/R/. That scheme has worked since 2003 or so for thousands of users.Rapparee
So what happens if I run update.packages()? Will it try to overwrite files inside /usr/lib/R, or will it install copies on /usr/local/lib/R ?Aerobe
Dirk can you clarify: on my system, I see packages in both /usr/lib/R/site-library/ and /usr/local/lib/R/site-library/. It seems, for example, that r-cran-rjava has installed to /usr/lib/R/site-library/rJava.Geisha
Jeroen: depends whether you started R as a normal or sudo user. In the first case, packages in $HOME/R will upgrade, but you won't have permissions for packages under /usr. In the second case, all packages will upgrade.Geisha

© 2022 - 2024 — McMap. All rights reserved.