[r] require-vs-library
r2evans opened this issue · 1 comments
r2evans commented
The suggested R code starts with require(httr)
. While that may work, it is misusing the require
function, as it can return FALSE
(the package is not installed/available) but provide no error to the user. It is almost always better (especially in this type of use) to use library(httr)
instead, as it will emit an error if the function is not available.
An alternative is
if (!require("httr")) {
# do something here, perhaps
install.packages("httr")
library(httr)
}
Refs:
- https://stackoverflow.com/a/51263513/3358272
- https://yihui.org/en/2014/07/library-vs-require/
- https://r-pkgs.org/namespace.html#search-path
Thanks.