What are the differences between require () and library ()?

5

In R, what are the differences between require() and library() , if any? When should I include require() and when should I include library() ?

    
asked by anonymous 18.09.2016 / 05:50

2 answers

4

The following information consists of the help itself of the function require of R :

  

library (package) and require (package) both load the namespace of the package with name package and attach it on the search list. require is designed for use inside other functions; it returns FALSE and gives a warning (rather than an error as library () by default) if the package does not exist

That is, it is suggested that require be used inside other functions, because it returns a warning if the packet to be loaded is not on the system. The library command, by default, returns an error when the package is not installed.

There is no difference for anyone who uses these functions on a day to day basis. They both do the same thing.

    
18.09.2016 / 06:14
0

In the pure execution of these commands there is no difference, but with the command require it is possible to automate the verification of the presence of the package and only after that, installation of this one happens, because it returns TRUE or FALSE, follows example to install the package "datamap":

inst_pacote <- require(datamap)
if (inst_pacote == FALSE) {
        install.packages("datamap")
}
    
14.05.2017 / 12:31