R version 4.6.0 alpha (2026-04-05 r89794 ucrt) Copyright (C) 2026 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > #!/usr/bin/env Rscript > > # Visual Test Runner for tf_ggplot > # This script helps run visual tests and organize the output plots > > # Load required packages > library(testthat) > if (!require(ggplot2, quietly = TRUE)) { + stop("ggplot2 is required for visual tests") + } > > # Source the package functions (assuming we're in package root) > if (file.exists("R")) { + source_files <- list.files("R", pattern = "\\.R$", full.names = TRUE) + invisible(lapply(source_files, source)) + } else { + cat( + "Warning: R/ directory not found. Make sure tf_ggplot functions are loaded.\n" + ) + } Warning: R/ directory not found. Make sure tf_ggplot functions are loaded. > > # Create output directory structure > setup_visual_test_environment <- function() { + base_dir <- file.path("tests", "visual-output") + + # Create subdirectories for organization + dirs <- c( + file.path(base_dir, "plots"), # Individual plot files + file.path(base_dir, "comparisons"), # Side-by-side comparisons + file.path(base_dir, "reports") # HTML reports + ) + + for (dir in dirs) { + if (!dir.exists(dir)) { + dir.create(dir, recursive = TRUE) + cat("Created directory:", dir, "\n") + } + } + + return(base_dir) + } > > # Enhanced plot saving with metadata > save_visual_test_plot <- function( + plot, + name, + category = "general", + width = 8, + height = 6, + dpi = 150, + description = NULL + ) { + base_dir <- setup_visual_test_environment() + + # Save the plot + filename <- paste0( + sprintf( + "%02d", + match( + category, + c("basic", "advanced", "edge-cases", "comparisons", "general") + ) + ), + "_", + name, + ".png" + ) + filepath <- file.path(base_dir, "plots", filename) + + tryCatch( + { + ggsave(filepath, plot, width = width, height = height, dpi = dpi) + + # Save metadata + metadata <- list( + filename = filename, + category = category, + description = description %||% name, + created = Sys.time(), + dimensions = c(width = width, height = height), + dpi = dpi + ) + + metadata_file <- file.path( + base_dir, + "plots", + paste0(tools::file_path_sans_ext(filename), "_meta.rds") + ) + saveRDS(metadata, metadata_file) + + cat("✓ Saved:", filename, "\n") + return(filepath) + }, + error = function(e) { + cat("✗ Failed to save", filename, ":", e$message, "\n") + return(NULL) + } + ) + } > > # Create HTML report with all plots > create_visual_report <- function() { + base_dir <- setup_visual_test_environment() + plots_dir <- file.path(base_dir, "plots") + + # Get all plot files + plot_files <- list.files(plots_dir, pattern = "\\.png$", full.names = FALSE) + plot_files <- sort(plot_files) + + if (length(plot_files) == 0) { + cat("No plots found to include in report.\n") + return(NULL) + } + + # Create HTML content + html_content <- c( + "", + "", + "
", + "Generated on: ", Sys.time(), "
"), + paste0("Total plots: ", length(plot_files), "
") + ) + + # Group plots by category + categories <- list() + for (file in plot_files) { + # Extract category from filename prefix + category_num <- substr(file, 1, 2) + category_name <- switch( + category_num, + "01" = "Basic Functionality", + "02" = "Basic Functionality", + "03" = "Advanced Features", + "04" = "Advanced Features", + "05" = "Advanced Features", + "06" = "Advanced Features", + "07" = "Comparisons", + "08" = "Comparisons", + "09" = "Geom Types", + "10" = "Geom Types", + "11" = "Geom Types", + "12" = "Geom Types", + "13" = "Geom Types", + "14" = "Edge Cases", + "15" = "Edge Cases", + "16" = "Edge Cases", + "17" = "Edge Cases", + "General" + ) + + if (is.null(categories[[category_name]])) { + categories[[category_name]] <- character(0) + } + categories[[category_name]] <- c(categories[[category_name]], file) + } + + # Add plots to HTML, grouped by category + for (category in names(categories)) { + html_content <- c( + html_content, + paste0('