# The package development loop and R CMD check: builds a throwaway package in a # temporary folder, writes a function with an undeclared column name that triggers # a check NOTE, runs rcmdcheck::rcmdcheck() to inspect the issue, applies the # .data fix, and re-runs the check clean. # # Rscript check-demo.R planted_column <- "AGE" adults_code <- function(fixed = FALSE) { if (fixed) { c( "#' Filter for adult subjects", "#'", "#' @param data A data frame containing an AGE column.", "#' @return A filtered data frame.", "#' @importFrom rlang .data", "#' @export", "adults <- function(data) {", " dplyr::filter(data, .data$AGE >= 18)", "}" ) } else { c( "#' Filter for adult subjects", "#'", "#' @param data A data frame containing an AGE column.", "#' @return A filtered data frame.", "#' @export", "adults <- function(data) {", " dplyr::filter(data, AGE >= 18)", "}" ) } } make_demo_package <- function(path = file.path(tempdir(), "checkdemo")) { withr::local_options(usethis.quiet = TRUE) usethis::create_package( path, fields = list( Title = "Demo Package for R CMD Check", Description = "Filters subject-level data; used to show a check NOTE." ), rstudio = FALSE, open = FALSE ) usethis::with_project(path, { usethis::use_mit_license("Demo authors") usethis::use_package("dplyr") }) writeLines(adults_code(fixed = FALSE), file.path(path, "R", "adults.R")) devtools::document(path, quiet = TRUE) invisible(path) } fix_package <- function(path) { withr::local_options(usethis.quiet = TRUE) usethis::with_project(path, { usethis::use_package("rlang") }) writeLines(adults_code(fixed = TRUE), file.path(path, "R", "adults.R")) devtools::document(path, quiet = TRUE) invisible(path) } run_check <- function(path, quiet = TRUE) { rcmdcheck::rcmdcheck( path, args = "--no-manual", quiet = quiet, error_on = "never" ) } check_counts <- function(result) { c( errors = length(result$errors), warnings = length(result$warnings), notes = length(result$notes) ) } if (sys.nframe() == 0L) { demo_dir <- file.path(tempdir(), "checkdemo") if (dir.exists(demo_dir)) { unlink(demo_dir, recursive = TRUE) } pkg_path <- make_demo_package(demo_dir) message("Running initial check (expecting 1 NOTE)...") res1 <- run_check(pkg_path, quiet = TRUE) print(check_counts(res1)) cat("\nCheck output:\n", res1$notes, "\n\n", sep = "") message("Applying .data fix and re-checking...") fix_package(pkg_path) res2 <- run_check(pkg_path, quiet = TRUE) print(check_counts(res2)) unlink(demo_dir, recursive = TRUE) }