Skip to content

Instantly share code, notes, and snippets.

@lgatto
Last active December 10, 2015 21:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgatto/4493747 to your computer and use it in GitHub Desktop.
Save lgatto/4493747 to your computer and use it in GitHub Desktop.
Get/install an R package's dependencies.
getDependencies <- function(p,
rep = c("BioCsoft", "BioCann", "BioCexp", "BioCextra", "CRAN"),
biocVersion = "2.12",
depLevels = c("Depends", "Imports", "Suggests"),
filter = TRUE) {
rep <- match.arg(rep)
if (rep == "CRAN") {
rep <- getOption("repos")["CRAN"]
} else {
biocMirror <- getOption("BioC_mirror", "http://bioconductor.org")
biocPaths <- switch(rep,
BioCsoft = "bioc",
BioCann = "data/annotation",
BioCexp = "data/experiment",
BioCextra = "extra")
rep <- paste(biocMirror,
"packages",
biocVersion,
biocPaths,
sep = "/")
}
pDetails <- available.packages(contrib.url(rep))[p, ]
depLevels <- match.arg(depLevels, several.ok = TRUE)
deps <- sapply(depLevels,
function(.depLevel)
tools:::package.dependencies(pDetails, depLevel = .depLevel)[[1]][, 1])
deps <- unlist(deps)
i <- match("R", deps)
if (length(i) > 0)
deps <- deps[-i]
if (filter) {
installedP <- installed.packages()[,"Package"]
deps <- deps[!deps %in% installedP]
}
return(deps)
}
getPackagesInBiocView <- function(view,
rep = c("BioCsoft", "BioCann",
"BioCexp", "BioCextra"),
biocVersion = "2.12") {
suppressPackageStartupMessages(require("biocViews"))
data(biocViewsVocab)
rep <- match.arg(rep)
biocMirror <- getOption("BioC_mirror", "http://bioconductor.org")
biocPaths <- switch(rep,
BioCsoft = "bioc",
BioCann = "data/annotation",
BioCexp = "data/experiment",
BioCextra = "extra")
rep <- paste(biocMirror,
"packages",
biocVersion,
biocPaths,
sep = "/")
bv <- getBiocViews(rep, biocViewsVocab, "NoViewProvided")
if (!view %in% names(bv)) {
message("BiocView ", view, " not found.")
return(NULL)
}
return(bv[[view]])
}
installDependencies <- function(p, ...) {
message("Installing ",p, " dependencies and updating available packages...")
deps <- getDependencies(p, ...)
if (!require(BiocInstaller))
source("http://bioconductor.org/biocLite.R")
biocLite(deps, ask = FALSE)
invisible(NULL)
}
installPackages <- function(ps) {
installed <- installed.packages()[,"Package"]
ps <- ps[!ps %in% installed]
message("Installing dependencies and updating available packages...")
if (!require(BiocInstaller))
source("http://bioconductor.org/biocLite.R")
biocLite(ps, ask = FALSE)
invisible(NULL)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment