Skip to content

Instantly share code, notes, and snippets.

@JoseAlanis
Last active September 1, 2022 09:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoseAlanis/86da75bf223e10344b7c16791f45bafe to your computer and use it in GitHub Desktop.
Save JoseAlanis/86da75bf223e10344b7c16791f45bafe to your computer and use it in GitHub Desktop.
Checks if multiple R packages are installed. If yes, they are required. If not, they are installed and required.
load.package <- function(package, repos) {
# list of packages missing
missing <- package[!package %in% installed.packages()[, 'Package']]
# check wich packages are not intalled and install them
if (!is.null(missing)) {
if (missing(repos)) {
# use Goettingen (Germany) mirror as default
repos <- 'https://ftp.gwdg.de/pub/misc/cran/'
}
install.packages(missing, dependencies = TRUE,
repos = repos)
}
# load all packages
sapply(package, require, character.only = TRUE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment