How to make install.packages return an error if the R package cannot be installed?

install.packages()returns a warning if the package cannot be installed (for example, if it is not available); eg:

install.packages("notapackage")

( EDIT : I would like to make a mistake, regardless of the reason the package could not be installed, and not just in this example with the missing package).

I run the command install.packagesin a script and I would like it to throw the correct error and complete the execution. I do not see an obvious option inside install.packagesto handle this behavior. Any suggestions?

+3
source share
2 answers

R WithCallingHandlers() . , , R , - ( ).

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

, , , ; . , require , , .

+4

:

R> AP <- available.packages()
R> "notapackage" %in% AP[,1]      # expected to yield FALSE
[1] FALSE
R> "digest" %in% AP[,1]           # whereas this should be TRUE
[1] TRUE
R> 
0

Source: https://habr.com/ru/post/1694531/


All Articles