Skip to content

Instantly share code, notes, and snippets.

@keuv-grvl
Created September 13, 2017 10:36
Show Gist options
  • Save keuv-grvl/b685d8c0ed7525c521cf7064767515c5 to your computer and use it in GitHub Desktop.
Save keuv-grvl/b685d8c0ed7525c521cf7064767515c5 to your computer and use it in GitHub Desktop.
Install a R package if not already installed, then load it
loadOrInstall <- function(pkgname, CRANmirror.index=1) {
chooseCRANmirror(ind=CRANmirror.index)
if (! pkgname %in% installed.packages()[,1]) {
write("installing", stderr())
install.packages(pkgname, dep=T)
} else { write("already installed", stderr()) }
library(pkgname, character.only = TRUE)
write("loaded",stderr())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment