Tagarchief: require

Library and require in R

So, what’s the difference between the require and library functions in R? Yihui Xie explains it pretty clear: require() tries to load a package, while library() actually loads a package.

This means that require returns true if it succeeded and false if it failed. Library, on the other hand, generates an error if the package could not be loaded, instantaneously terminating the script. This is more desirable, because such an error is very traceable and more easily solved. Require simply returns false in this scenario and an error will only occur at the point the package is called for.

Is require therefore obsolete? No, it isn’t. When you want to use the success (or failure) of loading a package in your script, you can only use require. In most cases, however, library will be the better choice.

Reference: http://yihui.name/en/2014/07/library-vs-require/