Load multiple packages at once

RPackages

R Problem Overview


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)

R Solutions


Solution 1 - R

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)

Solution 2 - R

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.

Solution 3 - R

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!)

Solution 4 - R

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)

Solution 5 - R

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 ‘timerequire("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.

Solution 6 - R

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.

Solution 7 - R

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")

Solution 8 - R

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)

Solution 9 - R

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.

Solution 10 - R

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTyler RinkerView Question on Stackoverflow
Solution 1 - RdaroczigView Answer on Stackoverflow
Solution 2 - RTyler RinkerView Answer on Stackoverflow
Solution 3 - RJosh O'BrienView Answer on Stackoverflow
Solution 4 - RbalaView Answer on Stackoverflow
Solution 5 - RAshirwadView Answer on Stackoverflow
Solution 6 - RluchonachoView Answer on Stackoverflow
Solution 7 - REspantaView Answer on Stackoverflow
Solution 8 - RgoclemView Answer on Stackoverflow
Solution 9 - RricardoView Answer on Stackoverflow
Solution 10 - RmattadorView Answer on Stackoverflow