Load and update multiple packages in R Quickly
Load and update multiple packages in R, In this R tutorial, you’ll learn how to use the pacman add-on package’s functionalities.
Let’s get going!
The Pacman Package’s Basic Information
Tyler Rinker, Dason Kurkiewicz, Keith Hughitt, Albert Wang, Garrick Aden-Buie, and Lukas Burk created the pacman R package.
In the R programming language, the package provides facilities for managing add-on packages.
The package uses Base R functions such as library() and install.packages() and merges them into new, more intuitive and efficient functions.
 Load and update multiple packages in R
To use the functions provided in the pacman package, we must first install and load the package to R.
install.packages("pacman")Â Â Â Â Â Â Â Â
library("pacman")Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â We can use the functions offered by the pacman package after running the previous lines of code.
Let’s look at some examples of pacman R scripts in action.
Example 1: Using the p_load function, you can install and load several R packages
I’ll show you how to use the p_load method to load numerous add-on packages in one line of code in this example.
Take a look at the R syntax below.
p_load(gtools, dplyr, xlsx)
We loaded the three packages gtools, dplyr, and xlsx after running the prior code.
The p_load method additionally checks whether a package has previously been installed. If the package hasn’t been installed yet, the pacman package will do it for you.
The p_load function essentially replaces the Base R library, install.packages, and require functions, allowing you to install and load multiple packages at once with far less R code.
Example 2: Multiple R packages must be unloaded Using the p_unload method
The p_unload function is another useful feature of the pacman package. The p_unload function in R can be used to unload one or more loaded packages.
Let’s use the p_unload command to uninstall the three packages we loaded previously: gtools, dplyr, and xlsx
p_unload(gtools, dplyr, xlsx)
The following packages have been unloaded:gtools, dplyr, xlsx
Our three packages were detached, according to the prior RStudio console output.
The p_unload function is a simple replacement for the Base R detach function if you want to unload packages within an R session.
Side-by-Side plots with ggplot2 »
Example 3: Using the p_update function, you can update outdated R packages
In this example, we’ll show how to utilize the pacman package’s p update function to check for outdated packages and then update them all.
Let’s start by seeing whether my packages are outdated.
p_update(update = FALSE)
[1] "car"Â Â Â Â Â "cli"Â Â Â Â Â "dplyr"Â Â Â "ggplot2"Â "globals"Â "knitr"Â Â Â "nloptr" Â [8] "ps"Â Â Â Â Â Â "quantreg" "testthat" "tibble"Â Â "xfun"Â Â Â Â "cluster"Â "MASS"Â Â Â [15] "Matrix"Â Â "mgcv"Â Â Â Â "nlme"Â Â Â Â "survival"
I was pleasantly impressed the first time we tried this feature.
Fortunately, the pacman package offers a simple way to update all outdated packages with only one line of R code.
Before you run the following R code, make sure you have some time.
Depending on the number of packages you need to update, this could take some time.
p_update() # Update all packages
How productive was that? Awesome!