Skip to content

Instantly share code, notes, and snippets.

@agdamsbo
Forked from smithdanielle/check.packages.r
Last active June 28, 2023 12:13
Show Gist options
  • Save agdamsbo/e3b486b32c614cbea5b971c333740688 to your computer and use it in GitHub Desktop.
Save agdamsbo/e3b486b32c614cbea5b971c333740688 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.
# Supplied repo to be used with RScript in terminal
check.packages <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE, repos=structure(c(CRAN="http://cloud.r-project.org/")))
sapply(pkg, require, character.only = TRUE)
}
# Usage example
# packages<-c("REDCapR", "dplyr", "lubridate")
# check.packages(packages)
@agdamsbo
Copy link
Author

Forked

@agdamsbo
Copy link
Author

agdamsbo commented Jun 28, 2023

Updated as per this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment