R - Load and Install multiple package at once and auto install if package is not found

Install multiple package if not available in R library and load into session. Copy and paste below code to load libraries.

I've taken "ggplot2", "dplyr", "curl" package in my package checklist:

 

list.of.packages <- c("ggplot2","dplyr", "curl")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
repo='http://nbcgib.uesc.br/mirrors/cran/'
if(length(new.packages)) install.packages(new.packages, repo = repo)
lapply(list.of.packages, require, character.only = TRUE)

It will load all the required package from list, if unavailable it will install and then load.

Leave a Comment