Created
August 17, 2017 18:22
-
-
Save seasmith/e4daad791fa01f524f6504bc64102a1d to your computer and use it in GitHub Desktop.
Creating a hairball graph of CRAN package dependencies (with version info striped)
This file contains hidden or 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
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