简体   繁体   中英

package.install with custom lib cannot load packages

I'm trying to install R packages with dependencies to a custom lib location. Here's the minimal example script ( req.R ):

dir.create("r_packages")
install.packages("R.utils", lib="r_packages")
library("R.utils", character.only = TRUE, lib.loc="r_packages")

Running this with Rscript (eg in the r-base docker container ) shows, that the install is running fine , but loading the packages fails :

Output shortened. Full paste is here

$ Rscript req.R
also installing the dependencies ‘R.oo’, ‘R.methodsS3’

[...]

* installing *source* package ‘R.methodsS3’ ...
[...]
* DONE (R.methodsS3)
* installing *source* package ‘R.oo’ ...
[...]
* DONE (R.oo)
* installing *source* package ‘R.utils’ ...
[...]
* DONE (R.utils)

The downloaded source packages are in
        ‘/tmp/RtmpU4nBhU/downloaded_packages’
Error: package ‘R.oo’ required by ‘R.utils’ could not be found
Execution halted

Loading the packages one by one in reverse order of dependency works fine :

$ cat req.R
dir.create("r_packages")
#install.packages("R.utils", lib="r_packages")
library("R.methodsS3", character.only = TRUE, lib.loc="r_packages")
library("R.oo", character.only = TRUE, lib.loc="r_packages")
library("R.utils", character.only = TRUE, lib.loc="r_packages")


$ Rscript req.R
Warning message:
In dir.create("r_packages") : 'r_packages' already exists
R.methodsS3 v1.7.1 (2016-02-15) successfully loaded. See ?R.methodsS3 for help.
R.oo v1.22.0 (2018-04-21) successfully loaded. See ?R.oo for help.

Attaching package: ‘R.oo’

The following objects are masked from ‘package:methods’:

    getClasses, getMethods

The following objects are masked from ‘package:base’:

    attach, detach, gc, load, save

R.utils v2.8.0 successfully loaded. See ?R.utils for help.

Attaching package: ‘R.utils’

The following object is masked from ‘package:utils’:

    timestamp

The following objects are masked from ‘package:base’:

    cat, commandArgs, getOption, inherits, isOpen, parse, warnings

Anyone with a hint into the right direction? What am I doing wrong?

After some research I found, that setting the standard library path with .libPaths() solved the problem. Obviously, the library() function does not pass the lib.loc argument to succeeding calls.

This is the final code:

dir.create("r_packages")
.libPaths(c("r_packages"))
install.packages("R.utils", lib="r_packages")
library("R.utils", character.only = TRUE, lib.loc="r_packages")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM