Let pacman Eat Up library and require

[This article was first published on data_steve, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

One of the packages I use all the time now in my interactive R sessions is pacman. It’s more than just a nicer way to load packages, it’s a great way to explore packages, functions, package libraries, versioning, etc.

Grab it off cran with install.packages("pacman").

Out of the box, the function I use every day in all my scripts or at the console is pacman::p_load(); it’s a great substitute for library() or require(). Yihui has already written about his preferences/understanding on the library vs. require.

I prefer pacman::p_load() for different reasons than he describes. Instead of having to write 5 lines of code to load for 5 common data munging packages, like so:

library(dplyr)
library(tidyr)
library(plyr) 
library(magrittr)
library(ggplot2)

You can write one line

pacman::p_load(dplyr,tidyr,plyr,ggplot2, magrittr)

If you want to unload a package, say to update it, the function pacman::p_unload() is pretty useful. Even better pacman::p_load() will install and load new packages in the same command. And for github or bitbucket accounts, use pacman::p_load_gh().

Yes, I know that if brevity in syntax for package loading were my main motivation, I could just do import pandas for python or library(data.table) in R and get the equivalent functionality in one package load; but brevity of package loading isn’t my main concern in language or package choosing.

Something else that’s really nice is a way to print all the functions available in a package. Try it with pacman::p_funs(magrittr). You may find the print out overwhelming for a large packages like pacman::p_funs(stats); but I still like it in cases when IntelliSense auto-complete doesn’t quite get me to the function I’m looking for or is cramped for reading.

Also, whenever you’re doing a update to R or you just want to quickly back up all your packages, I find pacman::p_lib() support helpful. For example,

writeLines(pacman::p_lib(), "~/Desktop/list_of_R_packages.csv")

Lastly, to find out what the path to your R-library, the function pacman::p_path() is a real help. Check out the pacman github page for some more details.

In the next blog post, we’ll have a bit of fun with

To leave a comment for the author, please follow the link and comment on their blog: data_steve.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)