Loading R Packages: library() or require()?

When I was an R newbie, I was taught to load packages by using the command library(package). In my Linear Models class, the instructor likes to use require(package). This made me wonder, are the commands interchangeable? What’s the difference, and which command should I use?

Interchangeable commands . . .

The way most users will use these commands, most of the time, they are actually interchangeable. That is, if you are loading a library that has already been installed, and you are using the command outside of a function definition, then it makes no difference if you use “require” or “library.” They do the same thing.

… Well, almost interchangeable

There are, though, a couple of important differences. The first one, and the most obvious, is what happens if you try to load a package that has not previously been installed. If you use library(foo) and foo has not been installed, your program will stop with the message “Error in library(foo) : there is no package called ‘foo’.” If you use require(foo), you will get a warning, but not an error. Your program will continue to run, only to crash later when you try to use a function from the library “foo.”

This can make troubleshooting more difficult, since the error is going to happen in, say, line 847 of your code, while the actual mistake was in line 4 when you tried to load a package that wasn’t installed.

I have come to prefer using library(package) for this reason.

There is another difference, and it makes require(package) the preferred choice in certain situations. Unlike library, require returns a logical value. That is, it returns (invisibly) TRUE if the package is available, and FALSE if the package is not.  This can be very valuable for sharing code. If you are loading a package on your own machine, you may know for sure that it is already installed. But if you are loading it in a program that someone else is going to run, it’s a good idea to check! You can do this with, for example:

if (!require(package)) install.packages('package')
library(package)

will install “package” if it doesn’t exist, and then load it.

One final note: if you look at the source code, you can see that require calls library. This suggests that it is more efficient to use library. As a practical matter, it seems unlikely that this would make a difference on any reasonably modern computer.

 

  • Pingback: Loading R Packages: library() or require()? – Cloud Data Architect()

  • Pingback: Loading R Packages: library() or require()? | A bunch of data()

  • Pingback: Loading R Packages: library() or require()? – Mubashir Qasim()

  • hortonheardme

    Nice explanation of the error vs warning difference in behavior.

    I often write R code that will be run by colleagues who are not necessarily fluent in R on their computers which are configured differently from mine. Because I often use 3-20+ packages in a workflow, I use a slightly different idiom to ensure that the necessary packages (and dependencies) are installed if not present, but not reinstalled if already present (colleagues may have very slow network connections):

    pkgList <- c("reshape2", "plyr", "lubridate", # data munging
    "rgdal", "gdalUtils", # spatial tools
    "rgbif" # pull species data from gbif API
    )
    inst 0) install.packages(pkgList[!inst],dep=TRUE)
    lapply(pkgList, library, character.only = TRUE, quietly=TRUE)

    I expect that your approach is faster for a single package without dependencies. My alternative might be better for multiple packages, especially if I annotate what each package is used for. [I have a rather large block of lines of pkgList values that I subset on new projects.]

  • Nice article Gerald! I also used to use the approach suggested by @hortonheardme:disqus, but I found when less experienced users tried to modify the list of packages, they would forget the trailing comma resulting in an error that blocked the loading of all packages. So now whenever I’m sharing with others, I put in lots of individual library commands ala Gerald’s approach.

  • Héctor Segal

    Do you test pacman package? It load, update, and install a list of package defined by user. Only need a first line for verify if exist pacman package, and install if not exit.

    if (!require(pacman)) install.packages(“pacman”)
    library(pacman)
    p_load(“tidyverse”,”ggplot2″, “ggThemeAssist”, “ggthemes”,”dplyr”,”tidyr”,”broom”,
    “normtest”,”nortest”,”rio”,”xlsx”,”ggfortify”,”pander”, “car”,”lmtest”,”mblm”)

  • adamno227

    After having read this article https://yihui.name/en/2014/07/library-vs-require/ , I came to the conclusion that checking to see if a package was installed was the only time one should use require.