Skip to content

Instantly share code, notes, and snippets.

@JosiahParry
Last active December 16, 2022 15:36
Show Gist options
  • Save JosiahParry/aa7ef4871438c8a160ac9d25f75b9c18 to your computer and use it in GitHub Desktop.
Save JosiahParry/aa7ef4871438c8a160ac9d25f75b9c18 to your computer and use it in GitHub Desktop.
Checks if there are missing value tags in any man pages
# Function to run to check if you have any missing `\value{}` tags
check_missing_value <- function(pkg = ".") {
pkg_root <- rprojroot::find_package_root_file(path = pkg)
# find exported functions
ns <- readLines(file.path(pkg_root, "NAMESPACE"))
exports <- ns[which(grepl("export", ns))]
# https://stackoverflow.com/questions/8613237/extract-info-inside-all-parenthesis-in-r
exported_fxs <- stringr::str_extract(exports, "(?<=\\().+?(?=\\))")
# parse all man pages
all_mans <- fs::dir_ls(file.path(pkg_root, "man"), type = "file")
man_checks <- purrr::map(all_mans, has_value_tag, exported_fxs)
man_checks |>
tibble::enframe() |>
tidyr::unnest_wider(value) |>
tidyr::unnest_longer(functions) |>
dplyr::arrange(value)
}
# Helper function to use per man page
has_value_tag <- function(help, exported_fxs) {
man <- tools::parse_Rd(help)
get_tags <- purrr::attr_getter("Rd_tag")
man_tags <- purrr::map_chr(man, get_tags)
fx_names <- unlist(man[man_tags == "\\alias"])
fxs <- fx_names[fx_names %in% exported_fxs]
if (length(fxs) == 0) {
return(list(functions = NA, value = NA))
}
has_value <- any(man_tags == "\\value")
if (!has_value) cli::cli_alert_info("{fx_names} require `@return` tag")
list(functions = fx_names, value = has_value)
}
# check your package
check_missing_value()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment