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

Asked

Viewed 858 times

6

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

2 answers

5


The following information is provided by the function’s own help 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() does by default) if the package does not exist

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

There is no difference for those who use these functions in everyday life. The two do the same thing.

0

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

inst_pacote <- require(datamap)
if (inst_pacote == FALSE) {
        install.packages("datamap")
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.