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