Created
September 12, 2013 04:32
-
-
Save floybix/6533090 to your computer and use it in GitHub Desktop.
Find all transitive dependencies of an R package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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