Skip to content

Instantly share code, notes, and snippets.

@seasmith
Created August 17, 2017 18:22
Show Gist options
  • Save seasmith/e4daad791fa01f524f6504bc64102a1d to your computer and use it in GitHub Desktop.
Save seasmith/e4daad791fa01f524f6504bc64102a1d to your computer and use it in GitHub Desktop.
Creating a hairball graph of CRAN package dependencies (with version info striped)
library(dplyr)
library(purrr)
library(data.table)
library(stringi)
library(igraph)
library(ggraph)
# Import
cran <- available.packages() %>% as_tibble()
# Clean
cran$Depends <- cran$Depends %>% stri_trim_both()
# Count number of dependencies of each package
cran$dep_num <- cran$Depends %>%
stri_split(regex = ",") %>%
map(function(x) sum(!grepl("^R \\(", x))) %>%
unlist()
cran$dep_pkgs <- cran$Depends %>%
stri_split(regex = ",") %>%
map(stri_trim_both) %>%
map(function(x) x[!stri_detect(x, regex = "^R \\(|^R\\(")])
cran$dep_pkgs <- cran$dep_pkgs %>%
map(function(x) if (length(x) == 0) x <- NA else x)
cran_wv <- cran # wv = With (package) Versions
cran$dep_pkgs <- cran$dep_pkgs %>%
map(function(x) stri_replace_all(x, "", regex = "\\s\\(.*| \\(.*|\\(.*")) %>%
map(stri_trim_both)
# -- Row bind a list into a tibble using data.table::rbindlist()
bind_list_rows <- function(list_rows) {
as_tibble(rbindlist(list_rows))
}
pkg_deps <- cran %>% {Map(function(Package, dep_pkgs) tibble(from = rep(Package, length(dep_pkgs)), to = dep_pkgs),
Package = .$Package,
dep_pkgs = .$dep_pkgs)} %>%
bind_list_rows() #%>%
# filter(!is.na(to)) # remove packages with no dependencies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment