Skip to content

Instantly share code, notes, and snippets.

@KarenGoncalves
Last active January 11, 2024 19:36
Show Gist options
  • Save KarenGoncalves/0db105bceff4ff69547ee25460dda978 to your computer and use it in GitHub Desktop.
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.
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