Using Packages in R

Things You Need to Know

Md Ahsanul Islam
3 min readFeb 21, 2023

In R, packages are collection of functions, datasets and documentations. These are typically created and maintained by third-party developers, and they can be downloaded and installed from various sources such as the Comprehensive R Archive Network (CRAN), GitHub, etc.

Installing Packages

Run the following code -

install.packages("packages_name")

You can pass the names of multiple packages to this function. You can specify the name of the repository using the argument repos . For example:

install.packages('packages', repos='http://cran.us.r-project.org')

You can install a package manually for a compressed file:

install.packages('C:/Users/User/Downloads/package_2.1.zip', 
repos=NULL, type='source')

Loading packages

Using the function library() :

library("package_name")

Another function to load package is require() :

require("package_name")

You can pass the name of only one package to these functions.

These two differ from one another in terms of output. If the desired package is not installed, library() throws an error and stops, whereas require() a warning and returns FALSE.

Reproducibility

If you send your R code to someone, there is a high chance that the user does not have all the packages installed. After running the code, he may need to manually check the error messages and install the packages.

To overcome this issue, I came up with this solution that will directly load the packages if they are installed, otherwise it will install those and then load into the environment:

packages <- c("tidyverse","readxl")

# checks which are not installed
not_installed <- packages[!packages %in% rownames(installed.packages())]

# installs the packages that are not installed
install.packages(not_installed) |> suppressMessages()

# load all packages that are in the variable'packages'
lapply(X = packages, FUN = library, character.only = TRUE) |> invisible()

I have written a function load_packages() that does the task easily. You can use the function by sourcing it into your computer using —

source("https://raw.githubusercontent.com/MdAhsanulHimel/load_packages_R/master/script.R")

# using the function
pkges <- c("apaTables", "anytime", "AER")
load_packages(packages = pkges)

Hope it helps. You can find the function in my GitHub repo. Feel free to give feedback.

Using install_load()

There is a package called install.load in R. This package provides a convenient way to install and load multiple packages in a single command.

To use the install.load package, you first need to install it by running install.packages('install.load') .

Once the package is installed, you can use the install_load() function to install and load multiple packages in a single command. For example, to install and load the "dplyr" and "ggplot2" packages, you can use the following code:

# Install and load the dplyr and ggplot2 packages
install.load::install_load(c("dplyr", "ggplot2"))

This will firstly check whether the packages are already installed or not, and if not then install and load the “dplyr” and “ggplot2” packages into your R session.

Using p_load()

The “pacman” package provides a convenient way to install and load multiple packages in a single command, similar to the install.load package.

To use the “pacman” package, you first need to install it by running install.packages('pacman').

Once the package is installed, you can use the p_load() function to install and load multiple packages in a single command. For example, to install and load the "dplyr" and "ggplot2" packages, you can use the following code:

# Install and load the dplyr and ggplot2 packages
pacman::p_load(dplyr, ggplot2)

The main difference between install_load() and p_load() is that the "pacman" package has additional features such as checking for package updates, searching for packages on multiple CRAN mirrors, managing multiple library locations, etc.

Follow me for more posts on R Programming and Statistics. Thanks for reading.

--

--

Md Ahsanul Islam
Md Ahsanul Islam

Written by Md Ahsanul Islam

Statistics & Data Science Enthusiast

No responses yet