Introduction to pacman

Tyler W. Rinker & Dason Kurkiewicz

The pacman package is an R package management tool that combines the functionality of base library related functions into intuitively named functions. This package is ideally added to .Rprofile to increase workflow by reducing time recalling obscurely named functions, reducing code, and integrating functionality of base functions to simultaneously perform multiple actions. The function p_load is particularly well suited for help forums and blog posts, as it will load and, if necessary, install missing packages.

Function names in the pacman package follow the format of p_xxx where 'xxx' is the task the function performs. For instance the p_load function allows the user to load one or more packages as a more generic substitute for the library or require functions and if the package isn't available locally it will install it for you. In keeping with the library function, the user need not use quotes around package names for most *pacman functions.

Installing, Loading, Unloading, Updating, & Deleting

Quick Reference Table

pacman Function Base Equivalent Description
p_load install.packages + library Loads and Install Packages
p_install install.packages Install Packages from CRAN
p_load_gh NONE Loads and Install GitHub Packages
p_install_gh NONE Install Packages from GitHub
p_install_version install.packages &
packageVersion
Install Minimum Version of Packages
p_temp NONE Install a Package Temporarily
p_unload detach Unloads Packages from the Search Path
p_update update.packages Update Out-of-Date Packages

The heart of pacman is it's ability to reduce typing in package management actions. The functions in this section act on packages.

Installing and Loading

p_load is a general use tool that can install, load, and update packages. The form for the function is:

p_load(..., char, install = TRUE, update = getOption("pac_update"), character.only = FALSE)

where the ... argument allows the user to pass in quoted (or unquoted) package names. For example, many blog posts begin coding with this sort of package call:

packs <- c("XML", "devtools", "RCurl", "fakePackage", "SPSSemulate")
success <- suppressWarnings(sapply(packs, require, character.only = TRUE))
install.packages(names(success)[!success])
sapply(names(success)[!success], require, character.only = TRUE)

With pacman this call can be reduced to:

pacman::p_load(XML, devtools, RCurl, fakePackage, SPSSemulate)

Just Installing

The user may wish to only install packages. The p_install (aliased as p_get) will allow the user to install with the same ease of format as p_load. For example:

p_install(dbConnect, qdap, reports)

Installing & Loading from GitHub

pacman provides a wrapper to the devtools package's install_github function for installing and loading GitHub packages. p_load_gh and p_install_gh are wrapper functions that are named and operate similarly to pacman's p_load and p_install.

Note: Packages are passed as character vector repository addresses in the form of username/repo[/subdir][@ref|#pull].
p_install_gh(c("Dasonk/githubSearch", "trinker/regexr", "hadley/httr@v0.4"))
p_load_gh("Dasonk/githubSearch", "trinker/regexr", "hadley/httr@v0.4")

Installing a Minimal Package Version

The user may need a minimal version of a particular package installed. The p_install_version ensures that a package is at least some version cut off. If the package does not exist or the minimum version is not met, p_install attempts to install the package from CRAN. This may be helpful in blog posts where a new function is being demonstrated, requiring a particular version of a package. For example:

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

Version of pacman (v. 0.2.0) is suitable

Version of testthat (v. 0.9.1) is suitable

Installing Temporarily

Lastly, p_temp enables the user to temporarily install a package. This allows a session-only install for testing out a single package without muddying the user's library.

p_temp(aprof)

p_isinstalled(aprof)
p_isloaded(aprof)
> p_temp(aprof)
trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.1/aprof_0.2.zip'
Content type 'application/zip' length 34037 bytes (33 Kb)
opened URL
downloaded 33 Kb

package 'aprof' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\you\AppData\Local\Temp\RtmpYh6bSr\downloaded_packages

aprof installed
Loading required package: aprof
> 
> p_isinstalled(aprof)
[1] FALSE
> p_isloaded(aprof)
aprof 
 TRUE 
Note: When using p_load and friends inside of functions, the user may want to explicitly supply the package name(s) as character strings and set character.only = FALSE or supply a character vector of package names directly to the char argument if it is available.

Unloading

To unload package(s) from the search path use p_unload. p_unload will not unload the base install packages that load when R boots up. The form for the function is:

p_unload(..., negate = FALSE, char, character.only = FALSE)

where the ... argument allows the user to pass in quoted package names. Users may also supply "all" to ... to remove all add on packages. The negate argument negates, unloading all packages except those supplied to ....

p_load(lattice)
p_isloaded(lattice)
p_unload(lattice)
p_isloaded(lattice)
> p_load(lattice)
> p_isloaded(lattice)
lattice 
   TRUE 
> p_unload(lattice)
The following packages have been unloaded:
lattice

> p_isloaded(lattice)
lattice 
  FALSE 

Updating

The p_update (aliased as p_up) is a wrapper for update.packages (defaults to ask = FALSE) and old.packages. To update packages use:

p_update() 

The user may just query (not actually update) for out-of-date packages using:

p_update(FALSE) 

Deleting

The task of recalling the name remove.packages for permanently deleting a package from your library has been replaced by the more consistently named p_delete (aliased as p_del). Additionally, the user may delete multiple packages in a single call. The function protects the user from accidental attempted base package deletion as it will not delete base installed packages (see installed.packages(priority = "base")). Try it out:

p_delete(fakePackage, stats)
> p_delete(fakePackage, stats)
The following packages are a base install and will not be deleted:
stats

The following packages not found in library:
fakePackage

Session Information

Quick Reference Table

pacman Function Base Equivalent Description
p_loaded .packages & sessionInfo List Attached Packages
p_isloaded NONE Logical Test of Attached Package

The p_loaded function allows the user to quickly determine what packages are attached.

p_loaded()

This returns non-base packages that are attached. Adding all = TRUE will return the base packages as well.

p_loaded(all = TRUE)

Supplying a package name to p_loaded (or alternatively p_isloaded()) provides a logical check of whether package(s) are attached:

p_loaded(base, MASS)
 base  MASS 
 TRUE FALSE 
p_isloaded(methods, stats)
methods   stats 
   TRUE    TRUE 

Local Package Information

Quick Reference Table

pacman Function Base Equivalent Description
p_author packageDescription Author of Package
p_citation citation Citation for Package
p_data data List Package Data Sets
p_depends &
p_depends_reverse
package_dependencies &
dependsOnPkgs
Package Dependencies/Reverse Dependencies Locally/on CRAN
p_exists available.packages &
list.files + .libPaths
Logical Check if Package
Exists Locally/on CRAN
p_functions loadNamespace + getNamespaceExports Functions from Package
p_help help HTML/PDF Manuals Viewing
p_information packageDescription Information for Package
p_interactive NONE Search Packages Interactively
p_news news Package NEWS
p_version packageVersion & R.Version Version of Package
p_vignette browseVignettes Package Vignettes Viewing

The functions in this section provide information about a user's local packages.

Package Existence (CRAN & Locally)

The p_exists function checks if a package exists either on CRAN (default) or locally. p_isinstalled is a convenience wrapper for p_exists that is defaulted to check local existence.

p_exists(pacman)  
[1] FALSE
p_exists(pacman, local = TRUE)
[1] TRUE
p_exists(I_dont_exist)
[1] FALSE
## wrapper for `p_exists(local = TRUE)`
p_isinstalled(pacman)
[1] TRUE

Package Dependencies & Reverse Dependencies

The p_depends function checks the dependencies of package either on CRAN (default) or locally. The p_depends_reverse function accomplishes the same task for a package's reverse dependencies.

p_depends(lattice)  
$Imports
[1] "grid"      "grDevices" "graphics"  "stats"     "utils"    

$Suggests
[1] "KernSmooth" "MASS"      
p_depends(lattice, local = TRUE)
$Imports
[1] "grid"      "grDevices" "graphics"  "stats"     "utils"    

$Suggests
[1] "KernSmooth" "MASS"      
p_depends(MASS)  
$Depends
[1] "grDevices" "graphics"  "stats"     "utils"    

$Suggests
[1] "lattice"  "nlme"     "nnet"     "survival"
p_depends(MASS, local = TRUE)
$Depends
[1] "grDevices" "graphics"  "stats"     "utils"    

$Suggests
[1] "lattice"  "nlme"     "nnet"     "survival"

Package Attributes

The following subsections discuss pacman functions that are used to grab package information.

Package Information

p_information (aliased as p_info) is a more general use function to grab information about a package from its 'DESCRIPTION' file; for example, the following fields come from the base package: (a) Package, (b) Version, (c) Priority, (d) Title, (e) Author, (f) Maintainer, (g) Description, (h) License, and (i) Built.

The form for the function is:

p_info(package, ..., fields = NULL)

where package is a package name and ... argument allows the user to request specific fields (the fields argument is a character only 'escape hatch' for use of p_info inside of functions). If both ... and fields are blank, p_info returns a list of all package fields from the package 'DESCRIPTION' file. p_info returns a list.

## Defaults to supply a list of fields with information about R's base package
p_info()
Package: base
Version: 3.1.2
Priority: base
Title: The R Base Package
Author: R Core Team and contributors worldwide
Maintainer: R Core Team <R-core@r-project.org>
Description: Base R functions
License: Part of R 3.1.2
Built: R 3.1.2; ; 2014-10-31 11:06:01 UTC; windows

-- File: C:/R/R-31~1.2/library/base/Meta/package.rds 
names(p_info())
[1] "Package"     "Version"     "Priority"    "Title"       "Author"     
[6] "Maintainer"  "Description" "License"     "Built"      
p_info(pacman, Author)
$Author
[1] "Tyler Rinker [aut, cre, ctb], Dason Kurkiewicz [aut, ctb]"
p_info(pacman, BugReports, URL)
$BugReports
[1] "https://github.com/trinker/pacman/issues?state=open"

$URL
[1] "https://github.com/trinker/pacman"
p_info(pacman, fields = "Version")
$Version
[1] "0.2.0"
Extracting Package Names
Note: The p_extract function is particularly useful for converting the “Depends”, “Imports”, and “Suggests” fields from a single string into a vector of package names.
## without `p_extract`
p_info(MASS, "Depends")  
$Depends
[1] "R (>= 3.0.0), grDevices, graphics, stats, utils"
p_extract(p_info(MASS, "Depends"))
R (>= 3.0.0)    grDevices     graphics        stats        utils 
         "R"  "grDevices"   "graphics"      "stats"      "utils" 
p_extract(p_info(methods, "Imports"))
  utils 
"utils" 

Package Author

p_author provides author information for a package as a string.

p_author(pacman)
[1] "Tyler Rinker [aut, cre, ctb], Dason Kurkiewicz [aut, ctb]"
p_author()
[1] "R Core Team and contributors worldwide"

Package Citation

p_citation (aliased as p_cite) provides citation information for a package to the console and in interactive sessions, attempts to copy to the users clipboard (OS dependent).

p_cite(pacman)

To cite package 'pacman' in publications use:

  Tyler Rinker and Dason Kurkiewicz (2012). pacman: Package
  Management Tool. R package version 0.2.0.
  https://github.com/trinker/pacman

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {pacman: Package Management Tool},
    author = {Tyler Rinker and Dason Kurkiewicz},
    year = {2012},
    note = {R package version 0.2.0},
    url = {https://github.com/trinker/pacman},
  }
p_citation()

To cite R in publications use:

  R Core Team (2014). R: A language and environment for
  statistical computing. R Foundation for Statistical Computing,
  Vienna, Austria. URL http://www.R-project.org/.

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {R: A Language and Environment for Statistical Computing},
    author = {{R Core Team}},
    organization = {R Foundation for Statistical Computing},
    address = {Vienna, Austria},
    year = {2014},
    url = {http://www.R-project.org/},
  }

We have invested a lot of time and effort in creating R, please
cite it when using it for data analysis. See also
'citation("pkgname")' for citing R packages.

Package Data

p_data provides a data.frame of a package's data sets and a description.

p_data(lattice)
  Data          Description                                          
1 barley        Yield data from a Minnesota barley trial             
2 environmental Atmospheric environmental conditions in New York City
3 ethanol       Engine exhaust fumes from burning ethanol            
4 melanoma      Melanoma skin cancer incidence                       
5 singer        Heights of New York Choral Society singers           

Package Functions

p_functions (aliased as p_funs) provides a list of the functions utilized by a package. Setting all = TRUE will yield the non-exported functions as well.

p_functions(pacman)
 [1] "comma_string2vector"      "is.base_package"         
 [3] "is.installed"             "is.loaded_package"       
 [5] "left.just"                "object_check"            
 [7] "p_author"                 "p_base"                  
 [9] "p_basepath"               "p_citation"              
[11] "p_cite"                   "p_cran"                  
[13] "p_data"                   "p_del"                   
[15] "p_delete"                 "p_delete_single"         
[17] "p_delete_warning"         "p_dependencies_single"   
[19] "p_depends"                "p_depends_helper"        
[21] "p_depends_reverse"        "p_detectOS"              
[23] "p_egg"                    "p_exists"                
[25] "p_extract"                "p_functions"             
[27] "p_funs"                   "p_get"                   
[29] "p_help"                   "p_info"                  
[31] "p_information"            "p_install"               
[33] "p_install_gh"             "p_install_version"       
[35] "p_install_version_single" "p_inter"                 
[37] "p_interactive"            "p_iscran"                
[39] "p_isinstalled"            "p_isloaded"              
[41] "p_lib"                    "p_library"               
[43] "p_load"                   "p_load_gh"               
[45] "p_load_single"            "p_load_single_gh"        
[47] "p_loaded"                 "p_news"                  
[49] "p_opendir"                "p_path"                  
[51] "p_sa"                     "p_search_any"            
[53] "p_search_library"         "p_set_cranrepo"          
[55] "p_sl"                     "p_tar"                   
[57] "p_temp"                   "p_unload"                
[59] "p_unlock"                 "p_up"                    
[61] "p_update"                 "p_ver"                   
[63] "p_version"                "p_vign"                  
[65] "p_vignette"               "parse_git_repo"          
[67] "paste0"                   "print.search_any"        
[69] "print.wide_table"         "Trim"                    
[71] "writeToClipboard"        
p_funs(pacman, all=TRUE)
 [1] "comma_string2vector"      "is.base_package"         
 [3] "is.installed"             "is.loaded_package"       
 [5] "left.just"                "object_check"            
 [7] "p_author"                 "p_base"                  
 [9] "p_basepath"               "p_citation"              
[11] "p_cite"                   "p_cran"                  
[13] "p_data"                   "p_del"                   
[15] "p_delete"                 "p_delete_single"         
[17] "p_delete_warning"         "p_dependencies_single"   
[19] "p_depends"                "p_depends_helper"        
[21] "p_depends_reverse"        "p_detectOS"              
[23] "p_egg"                    "p_exists"                
[25] "p_extract"                "p_functions"             
[27] "p_funs"                   "p_get"                   
[29] "p_help"                   "p_info"                  
[31] "p_information"            "p_install"               
[33] "p_install_gh"             "p_install_version"       
[35] "p_install_version_single" "p_inter"                 
[37] "p_interactive"            "p_iscran"                
[39] "p_isinstalled"            "p_isloaded"              
[41] "p_lib"                    "p_library"               
[43] "p_load"                   "p_load_gh"               
[45] "p_load_single"            "p_load_single_gh"        
[47] "p_loaded"                 "p_news"                  
[49] "p_opendir"                "p_path"                  
[51] "p_sa"                     "p_search_any"            
[53] "p_search_library"         "p_set_cranrepo"          
[55] "p_sl"                     "p_tar"                   
[57] "p_temp"                   "p_unload"                
[59] "p_unlock"                 "p_up"                    
[61] "p_update"                 "p_ver"                   
[63] "p_version"                "p_vign"                  
[65] "p_vignette"               "parse_git_repo"          
[67] "paste0"                   "print.search_any"        
[69] "print.wide_table"         "Trim"                    
[71] "writeToClipboard"        

Package Version

p_version (aliased as p_ver) gives the version of a package.

p_version()
[1] "R version 3.1.2 (2014-10-31)"
p_ver(pacman)
[1] '0.2.0'
p_ver(pacman) >= "0.2.0"
[1] TRUE

Documentation

The following subsections discuss pacman functions that are used to view help manuals, news, and vignettes for a package.

Package Help Manual

The p_help function accesses a package's help documentation in HTML or PDF form. If no Internet connection is available the pdf can be built from the package's internal files.

p_help(pacman)
p_help(pacman, web = FALSE)
p_help(pacman, build.pdf = TRUE)

Package News

p_news provides the NEWS file for for R or an add-on package.

p_news()
p_news(pacman)
## Grab specfic version subsets
subset(p_news(lattice), Version == 0.7)  
Note: p_news yields an object of class: “news_db_from_Rd”, “news_db”, and “data.frame” and can be coerced to an ordinary data.frame via: as.data.frame.

Package Vignette

p_vignette (aliased as p_vign) enables an interactive HTML exploration of packages' vignettes. The default (i.e., no package supplied) is to interact with all of the package vignettes available from a user's local library.

p_vignette()
p_vign(pacman)

Interactive Package Exploration

Lastly, the user may explore packages and package functions more interactively with p_interactive (aliased as p_inter). This function is an interactive mix of p_library and p_functions. p_interactive provides the user's local packages (their library) to choose to explore and then displays available functions for the chosen package. The user may optionally view the documentation for the chosen function from the chosen package. This function has no arguments as this information is passed interactively.

p_interactive()

Internet Based Package Information

Quick Reference Table

pacman Function Base Equivalent Description
p_cran available.package List CRAN Package
p_iscran %in% + available.package Logical Test of CRAN
p_search_any NONE Search for Package by Author

The functions in this section are Internet based and provide information about available CRAN packages.

CRAN Packages

p_cran provides a list of available CRAN packages. Its counterpart, p_iscran provides a logical check whether a package is available on CRAN.

p_cran()
length(p_cran())
[1] 6206
p_iscran("qdap")
[1] TRUE

Search CRAN

p_search_any (aliased as p_sa) enables approximate matching by package or author/maintainer names containing the search term. The first argument to p_search_any, term, is the word or name to search for. The second argument search.by tells the function what fields to search through: 1-"Maintainer", 1-"Author", 2-"Package", 3-"Version".

For example, we may choose to search for packages with the word “color” in the package name or packages with authors/maintainers named “hadley”. We could accomplish these tasks as follows:

p_sa("color", "package")
p_sa("hadley", "author")

Library Information

Quick Reference Table

pacman Function Base Equivalent Description
p_base NONE List Base Package
p_library Sys.getenv + .packages List Packages in User's Library
p_path .libPaths Path to User's Package Library
p_search_library NONE Search Library Packages Via Partial Matching
p_unlock NONE Deletes 00LOCK Directory

The functions in this section interact with the user's local library.

Library Location

p_path, a wrapper for .libPaths, provides the location to the user's library of add-on packages.

p_path()
[1] "C:/R/R-3.1.2/library"

Available Packages

p_library (aliased as p_lib) yields a vector of a user's packages and optionally opens the library location. p_base, similarly, provides the user's packages, but, marks base install packages with a marker (defaults to **).

p_lib()
p_base()

Search Library

p_search_library (aliased p_sl) is a handy function to search the library for a package that's on the tip of your tongue but you can't remember the full name or how to spell it. p_search_library takes then form:

p_search_library(begins.with = NULL, contains = NULL)

The first argument, begins.with allows the user to search for a package by initial characters. The contains argument allows the user to search for character strings within the package name. This will yield a list of matching packages that the user can then choose one to load.

p_sl("pa")
p_sl(contains = "man")
p_sl(begins.with ="pa", contains = "man")

Unlock Library

p_unlock deletes the 00LOCK directory that is accidentally left behind by a fail in install.packages. The documentation for install.packages states that sometimes install.packages can:

fail so badly that the lock directory is not removed: this inhibits any further installs to the library directory (or for –pkglock, of the package) until the lock directory is removed manually.

When this occurs simply enter p_unlock() and the 00LOCK directory is removed.

pacman Tools

Quick Reference Table

pacman Function Base Equivalent Description
p_detectOS Sys.info Detect Operating System
p_extract NONE Extract Packages from String
p_opendir system/shell Open a Directory

The functions in this section are general tools used within the pacman package. They are not package specific functions. Discussion of their functionality is beyond the scope of this vignette.