Last active
August 9, 2020 19:19
-
-
Save seakintruth/7e7e7b69a1b7e049d93b7e1be0a427d1 to your computer and use it in GitHub Desktop.
Function that installs and librarys as many packages as you want in one sweep
This file contains 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
# Modified from https://github.com/plynty/bls-ce-r-tools/blob/master/CE_PUMD_Wrangler.R | |
# Function that installs and librarys as many packages as you want in one sweep | |
# This action is similar to the utils::install.packages function | |
# just simplified/custom for my needs | |
# Parameters: string of package(s) name(s) | |
# fOnlyEnsureInstalled if TRUE does not load packages... | |
# Usage: library.packages("data.table","dplyr","stringr","dtplyr") | |
# Returns a matrix of packages and status of install and load/attachment | |
library.packages <- function( | |
..., | |
fOnlyEnsureInstalled=FALSE, | |
characterOnly=TRUE | |
){ | |
if (characterOnly) { | |
packages <- eval(match.call()[[2]]) | |
} | |
else { | |
#packages <- unlist(list(...)) | |
packages <- as.character(match.call(expand.dots = FALSE)[[2]]) | |
} | |
if (length(packages) == 0) { | |
return(invisible()) | |
} | |
.librarySinglePackage <- function(targetPackage){ | |
if(!(as.character(targetPackage) %in% installed.packages())) { | |
cat("Couldn't find the package in installed packages.\n Attempting to install ", | |
targetPackage,"...\n", sep="") | |
install.packages(as.character(targetPackage), dependencies = TRUE) | |
} | |
if (fOnlyEnsureInstalled) { | |
if((as.character(targetPackage) %in% installed.packages())) { | |
cat("Package ",targetPackage," is installed.\n", sep = "") | |
return(list(targetPackage,TRUE,FALSE)) | |
} else { | |
cat("Failed to install package ",targetPackage,".\n", sep = "") | |
return(list(targetPackage,FALSE,FALSE)) | |
} | |
} else { | |
cat("Loading ",targetPackage,".\n", sep = "") | |
if(try(require(as.character(targetPackage), character.only = TRUE))){ | |
return(list(targetPackage,TRUE,TRUE)) | |
} else { | |
return(list( | |
targetPackage, | |
((as.character(targetPackage) %in% installed.packages()) == TRUE), | |
FALSE | |
)) | |
} | |
} | |
} | |
.results <- matrix(unlist(lapply(packages,FUN=.librarySinglePackage)),ncol=3,byrow=TRUE) | |
.results.logical <- matrix(as.logical(.results[,2:3]),ncol=2) | |
colnames(.results.logical) <- c("IsInstalled","IsLoaded") | |
rownames(.results.logical) <- .results[,1] | |
return(.results.logical) | |
} | |
# testIt <- library.packages("data.table","aFakePackageShouldFail","dplyr","stringr","dtplyr",characterOnly = TRUE) | |
# testIt | |
# testIt2 <- library.packages(data.table,aFakePackageShouldFail,dplyr,stringr,dtplyr) | |
# testIt2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#Building this function directly only takes .0013 seconds, using
source("https://gist.githubusercontent.com/seakintruth/7e7e7b69a1b7e049d93b7e1be0a427d1/raw")
#takes roughly 2 seconds...install.packages("config")