Created
March 15, 2021 06:28
-
-
Save MichaelChirico/2c580ff1a363bd61881362a1fc9bee94 to your computer and use it in GitHub Desktop.
Count usages of data.table functions in Importing revdeps
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
#!/usr/local/bin/Rscript | |
library(data.table) | |
imports_dt <- tools::dependsOnPkgs( | |
"data.table", "Imports", | |
recursive = TRUE, | |
installed = available.packages() | |
) | |
tar_dir <- "/media/michael/69913553-793b-4435-ac82-0e7df8e34b9f/cran-mirror/src/contrib" | |
all_pkg <- list.files(tar_dir, pattern = "\\.tar\\.gz$", full.names = TRUE) | |
dt_funs <- grep("^[.:]", getNamespaceExports("data.table"), invert = TRUE, value = TRUE) | |
infix_idx <- grepl("^%.*%$", dt_funs) | |
dt_funs[!infix_idx] <- paste0("\\b", dt_funs[!infix_idx], "\\(") | |
counts <- data.table(fun = dt_funs, count = 0L, key = 'fun') | |
n_pkg <- 0L | |
for (pkg in imports_dt) { | |
tmp <- file.path(tempdir(), pkg) | |
pkg_path <- grep(sprintf("/%s_[0-9.]+\\.tar\\.gz", pkg), all_pkg, value = TRUE) | |
if (!length(pkg_path)) next | |
n_pkg <- n_pkg + 1L | |
pkg_path <- pkg_path[1L] | |
# --strip-components makes sure the output structure is | |
# /path/to/tmp/pkg/ instead of /path/to/tmp/pkg/pkg | |
utils::untar(pkg_path, exdir = tmp, extras="--strip-components=1") | |
r_src <- lapply(list.files(file.path(tmp, "R"), pattern = "\\.R", full.names = TRUE), readLines, warn = FALSE) | |
r_src <- unlist(r_src) | |
pkg_counts <- sapply(dt_funs, function(fun) sum(grepl(fun, r_src))) | |
counts[.(fun = names(pkg_counts)), count := count + pkg_counts] | |
unlink(tmp, recursive = TRUE) | |
} | |
counts[ , fun := gsub("^[\\]b|[\\][(]$", "", fun)] | |
counts[ , usage_per_pkg := count/n_pkg] | |
counts[order(-count)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment