install.packages()
returns a warning if a package cannot be installed (for instance, if it is unavailable); for example:
install.packages(\"nota
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.