[Solved] Load multiple packages at once

How can I load a bunch of packages at once with out retyping the require command over and over? I’ve tried three approaches all of which crash and burn.

Basically, I want to supply a vector of package names to a function that will load them.

x<-c("plyr", "psych", "tm")

require(x)
lapply(x, require)
do.call("require", x)

Solution #1:

Several permutations of your proposed functions do work — but only if you specify the character.only argument to be TRUE. Quick example:

lapply(x, require, character.only = TRUE)
Respondent: daroczig

Solution #2:

The CRAN package pacman that I maintain (authored with Dason Kurkiewicz) can accomplish this:

So the user could do:

## install.packages("pacman")
pacman::p_load(dplyr, psych, tm) 

and if the package is missing p_load will download it from CRAN or Bioconductor.

Respondent: Tyler Rinker

Solution #3:

This should do the trick:

lapply(x, FUN = function(X) {
    do.call("require", list(X)) 
})

(The key bit is that the args argument in do.call(what, args) must be a list — even if it only has a single element!)

Respondent: Josh O’Brien

Solution #4:

For someone who wants to install and load packages simultaneously I came across this function from this link

# ipak function: install and load multiple R packages.
# check to see if packages are installed. Install them if they are not, then load them into the R session.

ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("ggplot2", "plyr", "reshape2", "RColorBrewer", "scales", "grid")
ipak(packages)
Respondent: bala

Solution #5:

An alternative option comes from the package easypackages. Once installed, you can load packages in the most intuitive way:

libraries("plyr", "psych", "tm")

The package also includes a function to install several packages:

packages("plyr", "psych", "tm")

Reference here.

Respondent: luchonacho

Solution #6:

I think the code that @daroczig has provided can be improved by replacing the require with library and wrapping the lapply call inside the invisible() function. So, the improved code will look like the following:

invisible(lapply(x, library, character.only = TRUE))

This code is improved because:

  1. library() is generally preferred over require() for loading packages because the former gives an error if the package is not installed while the latter just gives a warning. Moreover, require() calls library(), so why not just use library() directly!

    library("time")
    # Error in library("time") : there is no package called ‘time’
    
    require("time")
    # Loading required package: time
    # Warning message:
    # In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
    # there is no package called ‘time’
    
  2. The list object returned and printed by the lapply() call is not meaningful in this case, so it makes sense to make the output invisible. Say you use R Notebook for your analysis work, using the invisible() function will suppress the contents of the list object and prevent the clutter in the rendered notebook file.

Respondent: Ashirwad

Solution #7:

You can simply use lubripack package and it neatly installs new packages and then load all of them in one line.

lubripack("plyr", "psych", "tm")

Here is the output after you run above code in RStudio.

enter image description here

How to Install Package:

Run below code to download the package and install it from GitHub. No need to have GitHub Account.

library(devtools)
install_github("espanta/lubripack")
Respondent: Espanta

Solution #8:

Building on daroczig’s solution, if you do not want to specify a list as input you can use

# Foo
mLoad <- function(...) {
  sapply(sapply(match.call(), as.character)[-1], require, character.only = TRUE)
}

# Example 
mLoad(plyr, dplyr, data.table)

… which is shorter than

lapply(list('plyr', 'dplyr', 'data.table'), require, character.only = TRUE)
Respondent: goclem

Solution #9:

I use the following function:

mrip <- function(..., install = TRUE){
    reqFun <- function(pack) {
        if(!suppressWarnings(suppressMessages(require(pack, character.only = TRUE)))) {
            message(paste0("unable to load package ", pack,
                           ": attempting to download & then load"))
            install.packages(pack)
            require(pack, character.only = TRUE)
        }
    }
    lapply(..., reqFun)
}

This tries to load, and if it fails installs and then try to load again.

Respondent: ricardo

Solution #10:

Slight mod of Tyler Rinker’s answer to add a check to install & load pacman:

#Install/load pacman
if(!require(pacman)){install.packages("pacman");require(pacman)}
#Install/load tons of packages
p_load(plyr,psych,tm)

I like the p_load solution because it avoids quoting!

Respondent: mattador

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.