Last active
April 20, 2019 11:47
-
-
Save trinker/2e3ae987139d8c6305515813dbac7620 to your computer and use it in GitHub Desktop.
Install datasci packages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #' @param packages An optional vector of Campus Labs packages to install. | |
| #' @param pattern An optional grep pattern of campus labs packages to install. | |
| install_cl <- function(packages = NULL, pattern = '.', ...){ | |
| try_install_cran <- function(package){ | |
| if (!require(package, character.only = TRUE, quietly = TRUE)) { | |
| message(sprintf('The "%s" package is missing; do you want me to install it?', package)) | |
| ans <- menu(c("Yes", "No")) | |
| if (ans == "2") { | |
| stop(sprintf('ABORTED!\n"%s" must be installed: install.packages("%s")', package, package)) | |
| } else { | |
| install.packages(package) | |
| if (!require(package)) warning(sprintf('I don\'t think "%s" installed b/c I can\'t load it', package)) | |
| } | |
| } | |
| } | |
| on.exit(options(repos = options()$repos)) | |
| options(repos = 'http://cran.us.r-project.org') | |
| try_install_cran('devtools') | |
| try_install_cran('magrittr') | |
| loc <- file.path( | |
| if (Sys.info()[['sysname']] == "Windows") 'L:\\' else '/Volumes/shared', | |
| 'DataScience\\`Data_Science_Admin\\`Admin_Projects\\Data_Science_Packages/datascience-packages' | |
| ) | |
| loc %>% | |
| dir( | |
| pattern = '^[A-Za-z0-9]+$', | |
| full.names = TRUE | |
| ) %>% | |
| ## The `grep` is way to grab just some packages via pattern; defaults to all | |
| {grep(pattern = pattern, ., value = TRUE)} %>% | |
| { | |
| x <- . | |
| out <- c( | |
| grep('clLoad$', x, value = TRUE), | |
| grep('cl$', x, value = TRUE), | |
| grep('cl(Load)?$', x, invert = TRUE, value = TRUE) | |
| ) | |
| if (is.null(packages)) return(out) | |
| out[basename(out) %in% packages] | |
| } %>% | |
| {invisible(lapply(., function(x){ | |
| package <- basename(x) | |
| cat(sprintf('Attempting to install \'%s\' ...', package), '\n'); flush.console() | |
| out <- try({devtools::install( | |
| pkg = x, | |
| dependencies = TRUE, | |
| upgrade = "never", | |
| build_vignettes = TRUE, | |
| force = FALSE, | |
| quiet = TRUE | |
| )}, silent = TRUE) | |
| if (inherits(out, 'try-error')) { | |
| warning(sprintf('`%s` failed to install/update', package), immediate. = TRUE) | |
| cat('\n\n'); flush.console() | |
| } else { | |
| if (!suppressMessages(require(basename(x), character.only = TRUE, quietly = TRUE))) { | |
| warning(sprintf('`%s` failed to load', package), immediate. = TRUE) | |
| } | |
| cat('\n\n'); flush.console() | |
| } | |
| }))} | |
| cat('Installation process complete!\n') | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment