There is no real difference between the two if the packages are not being loaded inside the function. require () function is usually used inside function and throws a warning whenever a particular package is not found. On the flip side, library () function gives an error message if the desired package cannot be loaded.
library() | require() |
---|---|
* Library () function gives an error message display, if the desired package cannot be loaded. | * Require () function is used inside function and throws a warning messages whenever a particular package is not Found |
* It loads the packages whether it is already loaded or not, | * It just checks that it is loaded, or loads it if it isn’t (use in functions that rely on a certain package). The documentation explicitly states that neither function will reload an already loaded package. |
Consider a related program for the above differentiation.
if(!require(package, character.only=T, quietly=T)) {
install.packages (package)
library(package, character.only=T)
}
For multiple packages you can use
for(package in c(”, ”)) {
if(!require(package, character.only=T, quietly=T)) {
install.packages (package)
library(package, character.only=T)
}
}