Skip to content

Instantly share code, notes, and snippets.

@floybix
Created September 12, 2013 04:32
Show Gist options
  • Save floybix/6533090 to your computer and use it in GitHub Desktop.
Save floybix/6533090 to your computer and use it in GitHub Desktop.
Find all transitive dependencies of an R package.
## find all transitive dependencies of an R package.
distrib.pkgs <- library(lib.loc=R.home("library"))$results[,1]
dependencies <-
function(pkg, dependencies = c("Depends", "Imports", "LinkingTo"),
pl = installed.packages())
{
if (!(pkg %in% rownames(pl))) stop("unknown pkg " pkg)
fields <- pl[pkg, dependencies]
fields <- fields[!is.na(fields)]
deps <- unlist(strsplit(fields, ", "))
deps <- sub(" \\(.*\\)", "", deps)
setdiff(deps, c("R", distrib.pkgs))
}
transitive_dependencies <-
function(pkg, pl = installed.packages())
{
deps <- c()
more <- pkg
while (length(more) > 0) {
this <- head(more, 1)
deps <- c(deps, dependencies(this, pl = pl))
more <- tail(more, -1)
}
unique(deps)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment