How to install and load multiple packages at once?

install packages

To install multiple packages with a single call to install.packages, pass the names of the packages as a character vector to the install.packages() function.

install.packages(c("gganimate", "tidyverse", "gapminder"))

load packages

Once you have the packages installed, you can make their contents available to use in your current R session by running,

lapply(c("gganimate", "tidyverse", "gapminder"), require, character.only = TRUE)
## [[1]]
## [1] TRUE
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] TRUE

check and install missing packages

list.of.packages <- c("gganimate", "tidyverse", "gapminder")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

For other alternatives see

  1. littler
library(littler)
install.r EIAdata gdata ggmap ggplot2 
  1. ipak
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("gganimate", "tidyverse", "gapminder")
ipak(packages)
## gganimate tidyverse gapminder 
##      TRUE      TRUE      TRUE
  1. easypackages

Related