How to load a package in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to load a package in R?

How to load a package in R?

This recipe helps you load a package in R

0

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

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: 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.

Syntax:

  1. require(package_name)
  2. 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 when the returned logical value needs to be used.

Relevant Projects

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.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

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.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

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.