Last active
January 11, 2024 19:36
-
-
Save KarenGoncalves/0db105bceff4ff69547ee25460dda978 to your computer and use it in GitHub Desktop.
Install r packages from CRAN, bioconductor and github and load them all with a single function.
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
install_cran <- function(cran_packages) { | |
pkgs.To.Install = which(! cran_packages %in% installed.packages()) | |
if (length(pkgs.To.Install) != 0) { | |
install.packages(cran_packages[pkgs.To.Install]) | |
} | |
return(NULL) | |
} | |
install_bioconductor <- function(bioconductor_packages) { | |
if (!require("BiocManager", quietly = TRUE)){ | |
install.packages("BiocManager")} | |
pkgs.To.Install = which(! bioconductor_packages %in% installed.packages()) | |
if (length(pkgs.To.Install) != 0) { | |
BiocManager::install(bioconductor_packages[pkgs.To.Install]) | |
} | |
return(NULL) | |
} | |
install_from_github <- function(github_packages) { | |
if (! "devtools" %in% installed.packages()) { | |
install.packages("devtools") | |
} | |
repos.github = github_packages | |
# In the line below, we remove the folder name | |
pkgs.github = gsub(".+/", "", repos.github) | |
pkgs.To.Install = which(! pkgs.github %in% installed.packages()) | |
if (length(pkgs.To.Install) != 0) { | |
# To install the package, we need the folder + package name | |
devtools::install_github(repos.github[pkgs.To.Install]) | |
} | |
return(pkgs.github) | |
} | |
install_from_dif_sources <- function( | |
cran_packages = c(), | |
bioconductor_packages = c(), | |
github_packages = c()) { | |
all_packages = c(cran_packages, bioconductor_packages) | |
#### Install from CRAN #### | |
if (length(cran_packages) != 0) install_cran(cran_packages) | |
#### Install from Bioconductor #### | |
if (length(bioconductor_packages) != 0) install_bioconductor( | |
bioconductor_packages | |
) | |
#### Install from github #### | |
if (length(github_packages) != 0) { | |
all_packages = c(all_packages, | |
install_from_github(github_packages) | |
) | |
} | |
for (curPkg in all_packages) library(curPkg, character.only = T) | |
return("All done!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment