Skip to content

Instantly share code, notes, and snippets.

@DrK-Lo
Last active June 9, 2021 07:46
Show Gist options
  • Save DrK-Lo/a945a29d6606b899022d0f03109b9483 to your computer and use it in GitHub Desktop.
Save DrK-Lo/a945a29d6606b899022d0f03109b9483 to your computer and use it in GitHub Desktop.
Easy code for installing packages in R (if not installed) and calling their libraries
packages_needed <- c("raster", "FNN", "RColorBrewer", "colorRamps", "adehabitatLT",
"data.table", "tidyverse", "fields", "ggplot2", "hexbin",
"rgdal", "tmap", "gstat", "sp", "maptools", "sf", "fasterize",
"fansi", "raster", "tmap", "gstat", "ContourFunctions","ash"
)
for (i in 1:length(packages_needed)){
if(!(packages_needed[i] %in% installed.packages())){install.packages(packages_needed[i])}
}
# require(ggplot2)}
for (i in 1:length(packages_needed)){
library( packages_needed[i], character.only = TRUE)
}
# bioconductor packages
bioc_packages_needed <- "karyoploteR"
if(length(bioc_packages_needed)>0){
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
for (i in 1:length(packages_needed)){
if(!(bioc_packages_needed[i] %in% installed.packages())){BiocManager::install(packages_needed[i])
}
}
}
for (i in 1:length(bioc_packages_needed)){
library(bioc_packages_needed[i], character.only = TRUE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment