x <- c("readr","stringr","lubridate","dplyr","ggplot2","scales","gridExtra","htmlwidgets","plotly") ipak <- function(pkg){ new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])] print(new.pkg) if (length(new.pkg)>0){ install.packages(new.pkg, dependencies = TRUE) sapply(pkg, require, character.only = TRUE) } } ipak(pkg=x) lapply(x, require, character.only = TRUE)