spencermathews
1/9/2013 - 2:57 PM

Get/install an R package's dependencies.

Get/install an R package's dependencies.

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)
}
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)
}

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]])
}
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)
}