Skip to content

Instantly share code, notes, and snippets.

@DarwinAwardWinner
Last active July 8, 2023 10:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarwinAwardWinner/ed75d6d845975a577c8533ac060d6d34 to your computer and use it in GitHub Desktop.
Save DarwinAwardWinner/ed75d6d845975a577c8533ac060d6d34 to your computer and use it in GitHub Desktop.
Script for finding loaded but unused packages in R scripts
#!/usr/bin/env Rscript
suppressPackageStartupMessages({
library(globals)
library(readr)
library(stringr)
library(rex)
library(magrittr)
library(rlang)
library(knitr)
library(assertthat)
library(withr)
library(glue)
library(BiocParallel)
register(MulticoreParam())
})
match_identical <- function(x, table, nomatch=NA_integer_) {
assert_that(is_list(table))
for (i in seq_along(table)) {
if (identical(x, table[[i]])) {
return(i)
}
}
return(nomatch)
}
get_Rmd_source <- function(fname) {
with_tempfile("temp", {
purl(fname, output=temp, quiet=TRUE)
read_file(temp)
})
}
analyze_pkg_usage <- function(fname) {
if (str_detect(fname, regex(rex(".Rmd", end), ignore_case=TRUE))) {
## Rmd file
file_text <- get_Rmd_source(fname)
} else {
# R file
file_text <- read_file(fname)
}
lib_regex <- rex(
"library",
zero_or_more(space),
"(",
zero_or_more(space),
capture(one_or_more(any, type="lazy")),
zero_or_more(space),
one_of(",", ")"))
loaded_pkgs <- unique(do.call(rbind, str_match_all(file_text, lib_regex))[,2])
missing_pkgs <- character(0)
for (pkg in loaded_pkgs) {
if (!suppressMessages(suppressWarnings(
library(package=pkg, character.only=TRUE, logical.return = TRUE)))) {
missing_pkgs <- c(missing_pkgs, pkg)
}
}
loaded_pkgs %<>% setdiff(missing_pkgs)
package_envs <- loaded_pkgs %>% set_names %>%
lapply(. %>% str_c("package:", .) %>% as.environment)
package_namespaces <- loaded_pkgs %>% set_names %>% lapply(asNamespace)
file_exprs <- parse(text=file_text)
expr_globals <- lapply(file_exprs, globalsOf, mustExist=FALSE)
used_envs <- unique(do.call(c, lapply(expr_globals, attr, "where")))
used_pkgs <- character(0)
for (env in used_envs) {
for (envlist in list(package_envs, package_namespaces)) {
found <- match_identical(env, envlist)
if (!is.na(found)) {
used_pkgs <- c(used_pkgs, names(envlist)[found])
break()
}
}
}
pkg_usage <- list(Used = used_pkgs,
Unused = setdiff(loaded_pkgs, used_pkgs))
if (length(missing_pkgs) > 0) {
pkg_usage$Missing <- missing_pkgs
}
pkg_usage
}
fnames <- commandArgs(TRUE)
pkg_usage <- bplapply(set_names(fnames), function(fname) {
message("Analyzing ", deparse(fname))
try(analyze_pkg_usage(fname))
})
message("Package usage:")
print(pkg_usage)
saveRDS(pkg_usage, "pkg_usage.RDS")
@Kabouik
Copy link

Kabouik commented Nov 27, 2020

Thanks for this. Unfortunately it seems BiocParallel cannot be installed in R 3.6.3:

> install.packages('BiocParallel')
Installing package into/home/kabouik/R/x86_64-solus-linux-gnu-library/3.6’
(aslibis unspecified)
Warning message:
packageBiocParallelis not available (for R version 3.6.3) 

@DarwinAwardWinner
Copy link
Author

BiocParallel is part of Bioconductor, which has its own installer. However, you can also just replace bplapply with lapply. It'll be a bit slower, but no other problem.

@Kabouik
Copy link

Kabouik commented Nov 27, 2020

Thanks, I don't know how I missed that! I could install BiocParallel using the Bioconductor manager. The script is missing some of the packages that I know for sure are mandatory in my script, but it can still be helpful to find some of the useless ones. Thank you.

@DarwinAwardWinner
Copy link
Author

If you think my script is missing some cases, I'd like to know about it so I can fix it.

@Kabouik
Copy link

Kabouik commented Nov 27, 2020

My script depends on functions from the packages:

  • pacman (function p_load())
  • ggspatial (layer_spatial())
  • data.table (fread())
  • dplyr (group_by())
  • ggrepel (geom_text_repel())
  • rgdal
  • raster (shapefile())
  • lubridate (ymd())

It fails if I don't require one of those packages. The analysis script found that sf is unused and this is correct (in fact, the line library(sf) was commented out in the R script, but the analysis script found it). However, it only listed one used package. I can't remember which one now but I am happy to check later when I'm back at that computer.

It is entirely possible that I just didn't use the script correctly or didn't quite understand what it aims to output, I'm sorry if that is the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment