When loading a package there are two ways to do it: library()
and require()
. What differences, if any, are there between the two methods?
Free translation and reworking of: What is the difference between require() and library()?
When loading a package there are two ways to do it: library()
and require()
. What differences, if any, are there between the two methods?
Free translation and reworking of: What is the difference between require() and library()?
Both point
?library
to?requiere
the same help. They both do the same thing: they load a certain package into the active session. The difference between the two is detailed in the help:Basically:
require()
:FALSE
and issues awarning
in case of not being able to load the packageLoading required package: <paquete>
that can be eliminated by means ofquietly = TRUE
library()
:error
in case of not being able to load the packagelogical.return = TRUE
The use of
require()
overlibrary()
is a bit vague, normally the packages are loaded at the beginning of a script and it is logical that if it cannot be loaded the execution is not continued, so itlibrary()
would be the appropriate option, on the other hand we could eventually find ourselves with these scenarios:Try to load a package and in case of error try to install it, for example:
For these cases the use of
require()
would be justified since the execution is not stopped.source: What is the difference between require() and library()?