R Language Installing packages Using a CLI package manager -- basic pacman usage


Example

pacman is a simple package manager for R.

pacman allows a user to compactly load all desired packages, installing any which are missing (and their dependencies), with a single command, p_load. pacman does not require the user to type quotation marks around a package name. Basic usage is as follows:

p_load(data.table, dplyr, ggplot2)

The only package requiring a library, require, or install.packages statement with this approach is pacman itself:

library(pacman)
p_load(data.table, dplyr, ggplot2)

or, equally valid:

pacman::p_load(data.table, dplyr, ggplot2)

In addition to saving time by requiring less code to manage packages, pacman also facilitates the construction of reproducible code by installing any needed packages if and only if they are not already installed.

Since you may not be sure if pacman is installed in the library of a user who will use your code (or by yourself in future uses of your own code) a best practice is to include a conditional statement to install pacman if it is not already loaded:

if(!(require(pacman)) install.packages("pacman")
pacman::p_load(data.table, dplyr, ggplot2)