Skip to content

Instantly share code, notes, and snippets.

@daviosa
Created August 14, 2013 13:36
Show Gist options
  • Save daviosa/6231143 to your computer and use it in GitHub Desktop.
Save daviosa/6231143 to your computer and use it in GitHub Desktop.
Function to require a R package and install it if needed.
require.install <- function(pkg, github=""){
req <- suppressWarnings(
suppressMessages(
require(package=paste(pkg), character.only=TRUE)
)
);
#print(github);
if(!req){
installOk <- FALSE;
if(length(github)<=0){
suppressWarnings(
suppressMessages(
install.packages(paste(pkg),dep=TRUE)
)
);
installOk <- TRUE;
}else if(length(github)>0){
if(suppressWarnings(
suppressMessages(
require("devtools")
)
)
){
suppressWarnings(
suppressMessages(
install_github(paste(pkg),paste(github))
)
);
installOk <- TRUE;
}
}
if(installOk){
suppressWarnings(
suppressMessages(
require(package=paste(pkg), character.only=TRUE)
)
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment