Difference Between library & require in R (2 Examples)
This article shows the difference between the library and require functions in R.
Table of contents:
Let’s get started!
Example 1: Error vs. Warning Message After Running library() & require()
In Example 1, I’ll illustrate the main difference between the library() and require() functions in R:
In case a package is not installed, the library functions returns an error message and the require function returns a warning message.
Let’s see this in practice…
For this example, we’ll use the stringr package. First, let’s remove the package from R:
remove.packages("stringr") # Remove stringr package
If we now try to load the stringr package with the library function, an error message is returned:
library("stringr") # Trying to load stringr # Error in library("stringr") : there is no package called ‘stringr’
In contrast, if we try to load the stringr package using the require function, only a warning message is returned:
require("stringr") # Trying to load stringr # Loading required package: stringr # Warning message: # In install.packages(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : # there is no package called 'stringr'
You might ask now: Why does this even matter?
Keep on reading! Because that’s what I’ll show next.
Example 2: library() & require() within User-Defined Functions
As explained in Example 1, the major difference between library and require is that library returns an error and require returns a warning in case a package is not installed yet.
This is especially relevant when we want to use packages within user-defined functions.
Consider the following function, in which we are applying the library function:
my_fun1 <- function() { # Create user-defined function library("stringr") x1 <<- 5 }
Now, let’s try to run this function:
my_fun1() # Apply user-defined function # Error in library("stringr") : there is no package called ‘stringr’
As you can see, our user-defined function returns the error message “Error in library(“XXX”) : there is no package called ‘XXX’”. This is not a surprise after the explanations of Example 1.
However, even more important is the following:
x1 # Try to print output of function # Error: object 'x1' not found
The object x1 – that we wanted to create in our user-defined function – does not exist. The reason for this is that our user-defined function stopped running after the library error occurred.
Let’s compare this with a user-defined function containing require instead of library. First, we have to create our function…
my_fun2 <- function() { # Create user-defined function require("stringr") x2 <<- 5 }
…and then we have to run our function:
my_fun2() # Apply user-defined function # Loading required package: stringr # Warning message: # In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : # there is no package called ‘stringr’
The RStudio console returns the warning we have already seen in Example 1.
Let’s have a look at the data object we wanted to create within our manually specified function:
x2 # Output of function exists # 5
It exists!
In other words: If you want to keep on running a user-defined function even when a package used in the function is not installed yet, then you might use require instead of library.
Video & Further Resources
Have a look at the following video tutorial of my YouTube channel. In the video, I’m explaining the R programming codes of this article in a programming session in the R programming language.
The YouTube video will be added soon.
Furthermore, you may read the other tutorials on my website.
In summary: In this post, I illustrated how to use library and require in the R programming language. Let me know in the comments below, in case you have any additional comments or questions.
Statistics Globe Newsletter