Skip to content

Instantly share code, notes, and snippets.

@pH-7
Last active August 25, 2023 12:00
Show Gist options
  • Save pH-7/4e35c9546edf976982f2197c31a15129 to your computer and use it in GitHub Desktop.
Save pH-7/4e35c9546edf976982f2197c31a15129 to your computer and use it in GitHub Desktop.
Which one to load a library in R - "library()" VS "require()" in R

library VS require in R

When loading a library, always use library. Never use require

TL;DR: require breaks one of the fundamental rules of robust software systems, fail early. In a nutshell, this is because, when using require, your code might yield different, erroneous results, without signalling an error. This is rare but not hypothetical! Consider this code, which yields different results depending on whether {dplyr} can be loaded:

On the other hand, require does install the package if hasn’t been installed (whereas library doesn’t).


Original answer: Stack Overflow - What is the difference between require() and library().

@pH-7
Copy link
Author

pH-7 commented Aug 25, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment