Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.