Skip to content

Instantly share code, notes, and snippets.

@jlmelville
Created November 30, 2021 16:39
Show Gist options
  • Save jlmelville/d9dd104600305bb3fe92d229de08c4dc to your computer and use it in GitHub Desktop.
Save jlmelville/d9dd104600305bb3fe92d229de08c4dc to your computer and use it in GitHub Desktop.
transitive dependencies in R
# modified slightly from https://gist.github.com/floybix/6533090
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 <- pkg
it <- 1
while (it <= length(deps)) {
this <- deps[it]
deps <- c(deps, dependencies(this, pl = pl))
deps <- unique(deps)
it <- it + 1
}
deps
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment