# Demonstrate function documentation with roxygen2 in a minimal package. # Run from the project root: # Rscript posts/roxygen2-documentation/scripts/roxygen-demo.R demo_source <- c( "#' Format a count and percentage", "#'", "#' Formats `n` out of `denom` as `\"n (pct%)\"`, the way counts appear", "#' in a summary table.", "#'", "#' @param n Numeric vector of counts.", "#' @param denom Denominator: a single positive finite number.", "#' @param digits Non-negative whole number of decimal places.", "#' @returns A character vector the same length as `n`.", "#' @family formatting helpers", "#' @export", "#' @examples", "#' format_n_pct(c(12, 30), denom = 86)", "#' format_n_pct(5, denom = 86, digits = 0)", "format_n_pct <- function(n, denom, digits = 1) {", " if (!is.numeric(denom) || length(denom) != 1L ||", " !is.finite(denom) || denom <= 0) {", " stop(\"`denom` must be a single positive finite number.\", call. = FALSE)", " }", " check_digits(digits)", " pct <- formatC(100 * n / denom, format = \"f\", digits = digits)", " paste0(n, \" (\", pct, \"%)\")", "}", "", "#' Format a mean and standard deviation", "#'", "#' @param x Numeric vector.", "#' @inheritParams format_n_pct", "#' @returns A character string of the form `\"mean (sd)\"`.", "#' @family formatting helpers", "#' @seealso [formatC()] for the underlying number formatting.", "#' @export", "#' @examplesIf interactive()", "#' format_mean_sd(c(64, 71, 80))", "format_mean_sd <- function(x, digits = 1) {", " check_digits(digits)", " m <- formatC(mean(x, na.rm = TRUE), format = \"f\", digits = digits)", " s <- formatC(stats::sd(x, na.rm = TRUE), format = \"f\", digits = digits)", " paste0(m, \" (\", s, \")\")", "}", "", "#' Validate digits argument", "#' @noRd", "check_digits <- function(digits) {", " if (!is.numeric(digits) || length(digits) != 1L ||", " !is.finite(digits) || digits < 0 || digits != floor(digits)) {", " stop(\"`digits` must be a single non-negative whole number.\", call. = FALSE)", " }", " invisible(TRUE)", "}" ) make_demo_package <- function(path = file.path(tempdir(), "tflformat")) { usethis::create_package(path, rstudio = FALSE, open = FALSE) writeLines(demo_source, file.path(path, "R", "format.R")) path } document_package <- function(path) { roxygen2::roxygenise(path) invisible(list.files(file.path(path, "man"), full.names = FALSE)) } rd_file <- function(path, topic) { file.path(path, "man", paste0(topic, ".Rd")) } rd_arguments <- function(file) { parsed <- tools::parse_Rd(file) args_tag <- Filter(function(x) attr(x, "Rd_tag") == "\\arguments", parsed) if (length(args_tag) == 0L) { return(character()) } items <- Filter(function(x) attr(x, "Rd_tag") == "\\item", args_tag[[1]]) vapply(items, function(it) as.character(it[[1]]), character(1)) } namespace_exports <- function(path) { parseNamespaceFile(basename(path), dirname(path))$exports } example_code <- function(file) { lines <- capture.output(tools::Rd2ex(file)) lines[!grepl("^###", lines) & nzchar(trimws(lines))] } undocumented_arguments <- function(path) { tools::checkDocFiles(dir = path) } drop_param <- function(path, param) { format_file <- file.path(path, "R", "format.R") code <- readLines(format_file, warn = FALSE) pattern <- paste0("#'\\s+@param\\s+", param, "\\b") writeLines(code[!grepl(pattern, code)], format_file) } run_demo <- function() { demo_dir <- file.path(tempdir(), "tflformatdemo") if (dir.exists(demo_dir)) { unlink(demo_dir, recursive = TRUE) } on.exit(unlink(demo_dir, recursive = TRUE), add = TRUE) message("1. Creating demo package...") make_demo_package(demo_dir) message("2. Running roxygen2::roxygenise()...") document_package(demo_dir) message("3. Generated Rd files in man/:") print(list.files(file.path(demo_dir, "man"))) message("4. NAMESPACE exports:") print(namespace_exports(demo_dir)) message("5. Arguments documented in format_mean_sd.Rd:") rd_path <- rd_file(demo_dir, "format_mean_sd") print(rd_arguments(rd_path)) message("6. Example code in format_mean_sd.Rd:") print(example_code(rd_path)) message("7. Dropping @param denom and re-documenting...") drop_param(demo_dir, "denom") document_package(demo_dir) message("8. Undocumented arguments reported by tools::checkDocFiles():") print(undocumented_arguments(demo_dir)) } if (sys.nframe() == 0L) { run_demo() }