Introduction to the pacman Package in R (3 Examples)
In this R tutorial you’ll learn how to use the functions of the pacman add-on package.
The content of the post is structured as follows:
Let’s get started!
Basic Information About the pacman Package
The pacman R package was developed by Tyler Rinker, Dason Kurkiewicz, Keith Hughitt, Albert Wang, Garrick Aden-Buie, and Lukas Burk. The package provides tools to conveniently manage add-on packages in the R programming language.
The package relies on Base R functions such as library() or install.packages() and combines these functions into new functions that are more intuitive and efficient.
- Here you can find the documentation of the pacman package.
- Here you can find the CRAN page of the pacman package.
- Here you can find an introduction to the pacman package.
In the following tutorial, I’ll show some practical examples for the application of the pacman functions in R.
Install pacman Package in R
In order to use the functions that are contained in the pacman package, we have to install and load the package to R:
install.packages("pacman") # Install pacman package library("pacman") # Load pacman package
After running the previous lines of code, we can use the functions provided by the pacman package.
Let’s take a look at some pacman R codes in action!
Example 1: Install & Load Multiple R Packages Using p_load Function
In this example, I’ll illustrate how to apply the p_load function to load multiple add-on packages in one line of code.
Have a look at the following R syntax:
p_load(ggplot2, dplyr, stringr) # Install & load packages
After executing the previous code, we have loaded the three packages ggplot2, dplyr, and stringr.
Note that the p_load function also checks whether a package is installed already. If the package is not installed yet, it is installed automatically by the pacman package.
The p_load function basically replaces the Base R library, install.packages, and require functions and allows installing and loading numerous packages simultaneously using much less R code.
Example 2: Unload Multiple R Packages Using p_unload Function
Another useful function of the pacman package is the p_unload function. The p_unload function can be used to detach one or multiple loaded packages from within R.
Let’s apply the p_unload command to unload the three packages ggplot2, dplyr, and stringr that we have loaded before:
p_unload(ggplot2, dplyr, stringr) # Unload packages # The following packages have been unloaded:ggplot2, dplyr, stringr
The previous output of the RStudio console tells us that our three packages were detached.
In case you want to unload packages within an R session, the p_unload function is a convenient replacement for the Base R detach function.
Example 3: Update Outdated R Packages Using p_update Function
In this example, I’ll show how to use the p_update function of the pacman package to check for outdated packages AND to update all of these packages.
First, let’s check which of my packages are outdated:
p_update(update = FALSE) # Check for outdated packages #  "backports" "broom" "data.table" "diffobj" "ggplot2" "isoband" #  "jsonlite" "officer" "patchwork" "pkgbuild" "processx" "ps" #  "rgdal" "rlang" "sf" "testthat" "vctrs" "codetools" #  "foreign" "KernSmooth" "Matrix" "nlme"
When I used this function the first time I was really impressed. I would never have guessed that so many of my packages were out of date…
Fortunately, the pacman package provides an easy solution to how we can update all outdated packages with only one line of R code.
Make sure that you have some time before running the following R code. This may take some time, depending on the number of packages you need to update:
p_update() # Update all packages
How efficient was that?! Awesome!
Video & Further Resources
Do you need more info on the R programming syntax of this tutorial? Then I can recommend watching the following video of my YouTube channel. I show the R programming codes of this article in the video.
The YouTube video will be added soon.
Additionally, you may want to read some of the other tutorials on my website. I have published many tutorials on the handling of add-on packages in R already:
- Load Multiple Packages at Once in R
- Check if Package is Missing and Install Automatically
- Detach All User-Installed Packages in R
- Unload Package without Restarting R
- Find Out Which Package Version is Loaded in R
- Get & Set Directory Path of Installed Packages Using libPaths Function
- R Warning: cannot remove prior installation of Package ‘X’
- Introduction to R Programming
This tutorial has illustrated how to apply some of the functions of the pacman package in R programming. Don’t hesitate to let me know in the comments section, in case you have additional questions.