how do I make install.packages return an error if an R package cannot be installed?

前端 未结 3 1910
遇见更好的自我
遇见更好的自我 2020-12-19 03:37

install.packages() returns a warning if a package cannot be installed (for instance, if it is unavailable); for example:

install.packages(\"nota         


        
3条回答
  •  隐瞒了意图╮
    2020-12-19 04:31

    Having just solved this for myself, I noted that install.packages() results in calling library(thepackage) to see it its usable.

    I made a R script that installs the given packages, uses library on each to see if its loadable, and if not calls quit with a non-0 status.

    I call it install_packages_or_die.R

    #!/usr/bin/env Rscript
    
    packages = commandArgs(trailingOnly=TRUE)
    
    for (l in packages) {
    
        install.packages(l, dependencies=TRUE, repos='https://cran.rstudio.com/');
    
        if ( ! library(l, character.only=TRUE, logical.return=TRUE) ) {
            quit(status=1, save='no')
        }
    }
    

    Use it like this in your Dockerfile. I'm grouping them in useful chunks to try and make reasonable use of Docker build cache.

    ADD install_packages_or_die.R /
    
    RUN Rscript --no-save install_packages_or_die.R profvis devtools memoise
    
    RUN Rscript --no-save install_packages_or_die.R memoise nosuchpackage
    

    Disclaimer: I do not consider myself an R programmer at all currently, so there may very well be better ways of doing this.

提交回复
热议问题