Open In App

The Difference Between require() and library() in R

R Programming Language has a lot of packages with different functions which can be used for data analysis. To make use of the functions, we have to install the packages to which the functions belong.

Difference Between require() and library() in R

require()

library()

Only gives a warning if the package is not available.

Gives an error if the package is not available.

The rest of the script will be executed even if the package is not available.

Execution stops when an error occurs.

Mostly used inside functions to check the availability of a package before using its functionality.

Mostly used at the beginning of the script to load required packages that have been installed beforehand.

Installing packages in R

We write “install.packages()” to install new packages. For example, this is how we would install the “tidyr” package



install.packages("tidyr")

Loading packages in R

Once the packages have been installed, we need to load them in the current environment. Packages can be loaded in R using two methods.

  1. library()
  2. require()

Load package using library()

The mlbench or the “Machine Learning Benchmark” package contains many datasets which can be used for machine learning projects. The Breast Cancer Dataset is also a part of this package.



In the following example we are trying to load a package which we have not installed first. The code will result in an error and rest of the script will not be executed because we had used library() function to load the package.




#loading the mlbench package
library(mlbench)
 
#loading Breast Cancer dataset
data(BreastCancer)
 
#view total number of rows in Breast Cancer dataset
nrow(BreastCancer)

Output:

Error in library(mlbench) : there is no package called ‘mlbench’

Load package using require()

Had we used require() to load the package, only a warning message would have been displayed without stopping the execution of the script.

In the following example, we try to load the package using require(). This will produce a warning message saying “there is no package called ‘mlbench’”. Since we have not installed the package, loading the dataset will also produce a warning and finally when we try to count the number of rows in the dataset, we will get an error and the execution will stop.




#loading the mlbench package
require(mlbench)
 
#loading Breast Cancer dataset
data(BreastCancer)
 
#view total number of rows in Breast Cancer dataset
nrow(BreastCancer)

Output:

Loading required package: mlbench
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘mlbench’

Upon comparing the outputs of both the examples, we can see that with require() the execution does not stop and all lines in the script are executed.

Suitable use of require()

require() returns TRUE if the package is available to be loaded otherwise it returns FALSE. We can use require() to check whether the package that we are trying to load is available or not. For example, if the package is not present then we can either decide to install it or terminate the script.




# Storing the return value of require(mlbench) in variable x
x <- require(mlbench)
 
# If the mlbench package is not available, we install and then load it
if(!require(mlbench)) { install.packages("mlbench"); library(mlbench) }

Output:

package ‘mlbench’ successfully unpacked and MD5 sums checked
Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘mlbench’
2: package ‘mlbench’ was built under R version 4.3.2

Conclusion

In conclusion, both require() and library() in R serve the purpose of loading and attaching packages, but with a key distinction. While library() code execution and throws an error if the package is not available, require() returns a logical value, allowing the script to continue execution. Choose library() for critical dependencies, and require() for conditional loading without interrupting code flow.


Article Tags :