Skip to content

Instantly share code, notes, and snippets.

@bryanhanson
Created February 9, 2022 23:23
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 bryanhanson/4c8ac99903df65af9c7e11a972447bc5 to your computer and use it in GitHub Desktop.
Save bryanhanson/4c8ac99903df65af9c7e11a972447bc5 to your computer and use it in GitHub Desktop.
Check an R package for stale imports and suggests
# run from the package top level
check_stale_imports_suggests <- function() {
# helper function: removes extra characters
# from strings read by read.dcf
clean_up <- function(string) {
string <- gsub("\n", "", string)
string <- gsub("\\(.+\\)", "", string)
string <- unlist(strsplit(string, ","))
string <- trimws(string)
}
desc <- read.dcf("DESCRIPTION", all = TRUE)
# look for use of imported packages
imp <- clean_up(desc$Imports)
if (length(imp) == 0L) message("No Imports: entries found")
if (length(imp) >= 1) {
imp_res <- rep("FALSE", length(imp))
for (i in 1:length(imp)) {
args <- paste("-r -e '", imp[i], "' *", sep = "")
g_imp <- system2("grep", args, stdout = TRUE)
# always found once in DESCRIPTION, hence > 1
if (length(g_imp) > 1L) imp_res[i] <- TRUE
}
}
# look for use of suggested packages
sug <- clean_up(desc$Suggests)
if (length(sug) == 0L) message("No Suggests: entries found")
if (length(sug) >= 1) {
sug_res <- rep("FALSE", length(sug))
for (i in 1:length(sug)) {
args <- paste("-r -e '", sug[i], "' *", sep = "")
g_sug <- system2("grep", args, stdout = TRUE)
# always found once in DESCRIPTION, hence > 1
if (length(g_sug) > 1L) sug_res[i] <- TRUE
}
}
# arrange output in easy to read format
role <- c(rep("Imports", length(imp)), rep("Suggests", length(sug)))
return(data.frame(
pkg = c(imp, sug),
role = role,
found = c(imp_res, sug_res)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment