Smart Library Loading with the pacman Package

Published Apr 23, 2022
Updated Nov 15, 2025
4 minutes read
Note

This old post is translated by AI.

##Introduction

Hello! 😊 I've been exploring various ways to write blog posts lately. I've changed the style quite a bit from this article, so the display might be different from before... But this should make things much more efficient...!

Anyway! This time I'll introduce the pacman package, which simplifies the essential package loading operation in R! Looking at foreign R-related blogs, maybe 2 out of 5 people seem to use it.

Starting with the pacman::p_load() function that can load packages more smartly than the built-in library(), this is a recommended package that can extremely streamline the essential package management tasks in R analysis.

The pacman package is famous as a simple package loader, but it actually has functions that are useful when dealing with installation errors. I've summarized functions that are convenient for dealing with package installation errors at the end of this article, so it's good to keep them in mind!

##Function Introduction

###Loading: p_load()

Honestly, just remembering this is enough. Use it like this:

pacman::p_load(dplyr, tidyr)

Normally, you can't use the p_load() function without first calling the pacman package with library(pacman), but by specifying it as package_name::function(), you can use the function without calling the package.

Since pacman is a compact package, there's absolutely no problem calling only p_load() as shown in the example above.

Benefit of p_load(): No Messages

There are several great things about this function.

First, the message when loading a package is omitted. Normally, when you load a package with the library() function, various messages are displayed depending on the package. The most common example is probably the message when loading the tidyverse package.

r$> library(tidyverse)
# ── Attaching packages ──────────────────────────────────────────── tidyverse 1.3.1 ──
# ✔ ggplot2 3.3.5     ✔ purrr   0.3.4
# ✔ tibble  3.1.6     ✔ dplyr   1.0.8
# ✔ tidyr   1.2.0     ✔ stringr 1.4.0
# ✔ readr   2.1.2     ✔ forcats 0.5.1
# ── Conflicts ─────────────────────────────────────────────── tidyverse_conflicts() ──
# ✖ dplyr::filter() masks stats::filter()
# ✖ dplyr::lag()    masks stats::lag()
# Warning message:
# In system("timedatectl", intern = TRUE) :
#   running command 'timedatectl' had status 1

This isn't the main topic, but the tidyverse package display message actually contains good information. First, what's written under Attaching packages is a list of child libraries that were actually loaded by loading the tidyverse package. As written, ggplot2 and dplyr automatically become available. Next, what's written under Conflicts indicates that functions with the same name are conflicting. And it says that dplyr's functions are masking and disabling stats's functions.

Depending on the environment it's colorful and nice, but honestly having this message appear every time is annoying. In the old days I often used the suppresspackagestartupmessages() function, but that's also troublesome (I can't remember such a long function name).

With pacman::p_load it becomes like this:

r$> pacman::p_load(tidyverse)
 
r$>

Nothing. Void. It's very simple and nice.

By the way, you can also load multiple packages at once.

pacman::p_load(tidyverse, ComplexHeatmap)

Benefit of p_load(): Installs If Not Present

It's rare, but sometimes a package you thought was installed actually isn't. Or when you pass R code you wrote to someone else and they don't have it installed.

In such cases, the library() function will stop with a "package does not exist" error angrily telling you "The package doesn't exist!" 😓 In such cases, p_load() will "install if not present".

You can do something similar using the built-in require() function, but it gets quite complicated.

if (!require("dplyr")) {install.packages("dplyr")}

Many people also use the above code to set up pacman itself 🙄

###Install with Version Specification: p_install_version()

This also happens only a few times a year, but rarely the latest version has a bug, or the specification has changed, and code that was working until yesterday stops working.

In such cases, you need to install with a version specification to downgrade the package. p_install_version() is effective for such cases.

pacman::p_install_version(
    c("pacman", "testthat"),
    c("0.2.0", "0.9.1")
)

###Trial Installation: p_temp()

As you get used to R, you search for and install various useful packages. And before you know it, there's a mountain of junk packages... that's a common occurrence.

For packages you're not sure you'll keep using, it's good to do a trial installation with p_temp() 👍

Using p_temp() puts you in a state where "it's not installed but it's loaded", and when you close the session, nothing remains.

pacman::p_temp(ggbeeswarm)
#..
#(omitted)
#The downloaded source packages are in
#        '/tmp/RtmpSUbJH4/downloaded_packages'
#
#ggbeeswarm installed

It seems this method installs to /tmp.

###Update Packages: p_update()

This also happens with tidyverse-related packages, but sometimes there are medium-scale updates about once a year. Updates can also be done from RStudio's settings screen, but you can also do it with pacman.

pacman::p_update(dplyr)

###Uninstall: p_remove()

Package deletion is easy to do from RStudio's package manager screen, but you can also quickly uninstall using pacman.

pacman::p_delete(ggbeeswarm)

###Open Current Folder in File Browser: p_opendir()

When working on R's command line, you can quickly open it in an explorer app using the following method. Path specification is also possible, so it can be executed at any location.

pacman::p_opendir(".")

From what I confirmed, on Windows it opens Explorer, and on WSL2 and Ubuntu terminals it opened neovim+file browser in the terminal. I think Finder opens on Mac too but I haven't confirmed 🤔

##Other Useful Functions

  • p_citation()
    • Convenient when you want to cite a package in a paper, etc.
  • p_functions()
    • Gets a list of available functions.
  • p_vignette()
    • You can reference vignettes (in R, this refers to something like a manual).

###Functions That Might Help with Package Installation Errors

Rarely, even following the manual, a package won't work and you're stuck. The following might help with finding the cause and solving the problem.

Check If Package Is Installed

One cause of "a package not working for some reason" is "a dependency package that should be installed isn't installed". The following functions are useful in such cases.

  • Display list of installed packages: p_library()
  • Directly check if installed: p_isinstalled()

Avoid the "Failed to lock directory" Problem

This is also an error when package installation fails that you'll encounter at least once if you use R. The error typically looks like this:

Error in install.packages : ERROR: failed to lock directory 'C:\Program Files\R\R-4.0.1\library' for modifying
    Try removing 'C:\Program Files\R\R-4.0.1\library/00LOCK'

https://community.rstudio.com/t/error-install-packages-failed-to-lock-directory/40018

This error message troubles many people, but the solution is to delete the file "00LOCK" shown in the error.

Alternatively, installing with the --no-lock option is also a countermeasure.

install.packages("Rcpp", INSTALL_opts = c('--no-lock'))

On Linux or Mac[ref]I think it probably works from Windows PowerShell too.[/ref], using the R CMD command from the console is one option.

R CMD INSTALL --no-lock Rcpp

That was a long preamble, but using the following pacman package makes deleting "00LOCK" easy.

  • p_unlock()
    • You can delete the 00LOCK directory.

##Summary

That's it, this time I introduced the pacman package! It's a package that can be used as a daily alternative to install.packages(), so please make use of it 🌟