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

前端 未结 3 1937
遇见更好的自我
遇见更好的自我 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:21

    The R function WithCallingHandlers() lets us handle any warnings with an explicitly defined function. For instance, we can tell the R to stop if it receives any warnings (and return the warning message as an error message).

    withCallingHandlers(install.packages("notapackage"),
                        warning = function(w) stop(w))
    

    I am guessing that this is not ideal, since presumably a package could install successfully but still throw a warning; but haven't encountered that case. As Dirk suggests, testing require for the package is probably more robust.

提交回复
热议问题