Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Created October 10, 2021 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-dray/5261e26c5cd77eef58d9203805c1c7c9 to your computer and use it in GitHub Desktop.
Save matt-dray/5261e26c5cd77eef58d9203805c1c7c9 to your computer and use it in GitHub Desktop.
Use {itdepends} to count use of {lubridate} functions in CRAN packages that import {lubridate}
remotes::install_github("jimhester/itdepends")
# Info on CRAN packages
cran <- tools::CRAN_package_db()
# Extract packages that import {lubridate}
pkgs <- cran[grepl("lubridate", cran$Imports), c("Package", "Imports")]
# Install all the packages to non-default location
purrr::walk(
pkgs,
~install.packages(
.x, dependencies = FALSE, destdir = "~/Desktop/pkgs", Ncpus = 2
)
)
# Get names of all packages that were downloaded (could be different
# to the 'pkgs' object because some downloads may have failed)
pkgs <- sub("_.*", "", list.files("~/Desktop/pkgs"))
# Extract functions used in each package
# Following this method: https://stackoverflow.com/a/55937737
# Initiate empty list of known size
dep_list <- vector("list", length(pkgs)) |> setNames(pkgs)
# Each list element is a dataframe of functions in each package
# Failures captured as a single row of NAs
for (i in pkgs) {
cat(i, "\n")
skip <- FALSE
tryCatch(
{ dep_list[[i]] <- itdepends::dep_usage_pkg(i)
dep_list[[i]]$focus <- i },
error = function(e) {
dep_list[[i]] <- data.frame(
pkg = NA_character_,
fun = NA_character_,
focus = NA_character_
)
skip <<- TRUE
}
)
if (skip) { next }
}
# List to dataframe
dep_df <- do.call(rbind, dep_list)
# Frequency of named lubridate functions by package
pkg_fn_count <- dep_df |>
dplyr::filter(pkg == "lubridate") |>
dplyr::count(focus, fun)
# Frequency of all lubridate functions by package
fn_distinct_count <- dep_df |>
dplyr::filter(pkg == "lubridate") |>
dplyr::distinct(focus, fun) |>
dplyr::count(focus, sort = TRUE)
# Frequency of all lubridate functions by package
fn_nondistinct_count <- dep_df |>
dplyr::filter(pkg == "lubridate") |>
dplyr::count(focus, sort = TRUE)
# Histogram: distinct {lubridate}-function use by packages
hist(
fn_distinct_count$n,
breaks = 30,
main = "Distinct {lubridate} functions used by\npackages importing {lubridate}",
xlab = "Function count"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment