How to load a package in R?

A comprehensive guide on how to load a package in R.

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:

  • Using library() function

  • Using require() function.

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

Explore Identifying Product Bundles from Sales Data Using R Language 

How to load a package in R?

Let's dive into the step-by-step process of how to load a package in R.

Step 1:Installing packages

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

install.packages("MASS")

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: Load a package in R (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.

Syntax:

require(package_name)

library(package_name)

require(MASS)

Loading required package: MASS

Warning message:

"package 'MASS' was built under R version 3.6.3"

library(MASS)

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'

Traceback:

1. library("abc")

test1

Error in eval(expr, envir, enclos): object 'test1' not found

Traceback:

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'"

test2

FALSE

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 to load a package in R when the returned logical value needs to be used.

How to load library in R?

R libraries, a collection of pre-written functions, are vital for enhancing the capabilities of the R language. They expand the range of tasks that can be accomplished. Here’s a step-by-step guide on how to load a library in R:

Step 1: Installation of Packages

To begin, ensure you have the package you want to load installed. You can use the install.packages("package_name") command to do so. Replace "package_name" with the name of the package you want to install. For example:

install.packages("ggplot2")

Step 2: Loading a Library

Once the package is installed, you can load it using the library() function. This function is widely used for this purpose. For example:

library(ggplot2)

The require() function is another way to load packages in R. It returns a logical value, TRUE if the package is present and successfully loaded, or FALSE if not. For example:

require(ggplot2)

Step 3: Checking for Errors

After loading the package, it’s important to check if it was loaded without errors. You can do this by testing if a variable was created during loading. Here's an example:

test = library("unknown_package")

# If an error occurs, the variable "test" won't be created.

# To check if the package was loaded successfully, you can also use "require()" as follows:

test2 = require("unknown_package")

# If "test2" is FALSE, the package didn't load successfully.

And that's how you load a library in R, expanding its capabilities and enabling you to utilize various functions provided by the library for data analysis, visualization, and more.

Explore Exciting R projects with ProjectPro!

R packages are indispensable for expanding the capabilities of R and unleashing its full potential in data analysis. This guide has emphasized the importance of loading packages in R using "require()" and "library()" functions along with guiding you on how to load a library in R.

If you are curious to learn more about R programming and its use in projects related to data science then check out ProjectPro, a premier resource that offers a diverse array of projects and learning materials to empower data enthusiasts. These projects provide practical exposure to harnessing the power of R packages for data analysis and manipulation. By joining ProjectPro, you not only access a wealth of data science and big data projects but also equip yourself with the skills needed for a successful career in the two AI-related fields.

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.