How to load a package in R?

How to load a package in R?

How to load a package in R?

This recipe helps you load a package in R


Recipe Objective

R packages consists of a collection of R functions, data sets and compiled code which adds value to the existing R-functionalities. They are stored in the 'library' directory in the R-environment and developed by the community. For Example, "dplyr" is one of the commonly used packages in R which adds further functionalities with respect to working with dataframes.

There already exists some default packages in the local directory 'library' on your machine when you install R. We can all the default packages by using code : 'library()'. It is very crucial to load the package before using it in code. And this can be done in two ways:

  1. Using library() function
  2. Using require() function.

This recipe demonstrates the two different ways to load a package after installation.

Step 1: Installing packages

We use the command " install.packages("name of the package") " to install the package


After running the command, you might recieve some messages which is based on the OS, dpendencies installed and the status of the package.

Step 2: Loading a package (require() vs library())

The most commonly used function to load the package is library(). require() is only used when we have to use the logical values that it returns. It will return TRUE if the package is present and successfully loaded. It is used in a error checking loop given by thierry.


  1. require(package_name)
  2. library(package_name)
Loading required package: MASS
Warning message:
"package 'MASS' was built under R version 3.6.3"
Warning message:
"package 'MASS' was built under R version 3.6.3"

Step 3: Checking for an error

We will store and display the output of the two functions by using an invalid package name to check for error.

test1 = library("abc")
Error in library("abc"): there is no package called 'abc'

1. library("abc")
Error in eval(expr, envir, enclos): object 'test1' not found
test2 = require("abc")
Loading required package: abc
Warning message in library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
"there is no package called 'abc'"

From the the above two tests, we can see that test2 (require()) returns a logical value FALSE when an error was occurred while test1 (library()) object was not created when error had occurred. Hence, require() is only used when the returned logical value needs to be used.

Relevant Projects

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.