require() is used inside the function and it outputs a warning and if the package is not found it continues whereas library() will show an error.
They aren’t that useful in day to day work, I am attaching examples of both below:
Library():
> test <- library("qwe")
Error in library("qwe") : there is no package called 'abc'
> test Error: object 'test' not found
> test <- require("qwe")
Loading required package: abc
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called 'abc'
> test [1] FALSE
require():
if(require("lme3"))
{ print("lme3 is loaded correctly")
} else {
print("trying to install lme3")
install.packages("lme3")
if(require(lme3)){
print("lme3 installed and loaded")
} else {
stop("could not install lme3")
}
}
For any doubts comment down below.