Skip to content

Instantly share code, notes, and snippets.

@danieltomasz
Forked from smithdanielle/check.packages.r
Created June 5, 2018 14:27
Show Gist options
  • Save danieltomasz/1db8c0d6404b0793114eca38210f23a8 to your computer and use it in GitHub Desktop.
Save danieltomasz/1db8c0d6404b0793114eca38210f23a8 to your computer and use it in GitHub Desktop.
Check if multiple R packages are installed. Install them if they are not,then load them into the R session.
# check.packages 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.
check.packages <- 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 example
packages<-c("ggplot2", "afex", "ez", "Hmisc", "pander", "plyr")
check.packages(packages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment