Created
November 30, 2021 16:39
-
-
Save jlmelville/d9dd104600305bb3fe92d229de08c4dc to your computer and use it in GitHub Desktop.
transitive dependencies in R
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
# 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