From 3a759ce959dce3d82d9b827a763031a4c21fc3a8 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 2 Dec 2026 13:25:32 -0500 Subject: [PATCH 1/4] Create function that gathers all captions and alt text from existing rdas into csv --- R/gather_rda_text.R | 63 ++++++++++++++++++++++++++++++++++++++++++ man/gather_rda_text.Rd | 29 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 R/gather_rda_text.R create mode 100644 man/gather_rda_text.Rd diff --git a/R/gather_rda_text.R b/R/gather_rda_text.R new file mode 100644 index 00000000..7b1605bc --- /dev/null +++ b/R/gather_rda_text.R @@ -0,0 +1,63 @@ +#' Gather captions and alternative text from `stockplotr` figure and table rda files +#' +#' @inheritParams extract_sis_data +#' +#' @return A csv with existing rda plot names, captions, and alternative text exported to the `figures_tables_dir`. +#' +#' @examples +#' \dontrun{ +#' save_all_plots(example_data) +#' +#' gather_rda_text( +#' figures_tables_dir = getwd() +#' ) +#' } +gather_rda_text <- function( + figures_tables_dir = getwd() + ){ + + figures_rda_files <- list.files( + file.path(figures_tables_dir, "figures"), + pattern = "\\.rda$", + recursive = TRUE, + full.names = TRUE) + + tables_rda_files <- list.files( + file.path(figures_tables_dir, "tables"), + pattern = "\\.rda$", + recursive = TRUE, + full.names = TRUE) + + rda_files <- c(figures_rda_files, tables_rda_files) + + captions_alttext_df <- data.frame( + file = character(), + caption = character(), + alt_text = character(), + stringsAsFactors = FALSE + ) + + # Loop through each .rda file + for (file in 1:length(rda_files)) { + load(rda_files[file]) + plot_name <- gsub(".rda", "", basename(rda_files[file])) + ifelse(length(rda$caption) > 0, caption <- rda$caption, caption <- NA) + ifelse(length(rda$alt_text) > 0 && rda_files[file] %in% figures_rda_files, alt_text <- rda$alt_text, alt_text <- NA) + + row <- data.frame( + file = plot_name, + caption = caption, + alt_text = alt_text, + stringsAsFactors = FALSE + ) + + captions_alttext_df <- rbind(captions_alttext_df, row) + cli::cli_alert_success("Processed {plot_name}") + } + + write.csv(captions_alttext_df, + file.path(figures_tables_dir, + "all_captions_alttext.csv"), + row.names = FALSE) + +} diff --git a/man/gather_rda_text.Rd b/man/gather_rda_text.Rd new file mode 100644 index 00000000..c1bf311c --- /dev/null +++ b/man/gather_rda_text.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/gather_rda_text.R +\name{gather_rda_text} +\alias{gather_rda_text} +\title{Gather captions and alternative text from \code{stockplotr} figure and table rda files} +\usage{ +gather_rda_text(figures_tables_dir = getwd()) +} +\arguments{ +\item{figures_tables_dir}{Path. Location of the existing 'figures' and +'tables' directories. + +Default: The working directory.} +} +\value{ +A csv with existing rda plot names, captions, and alternative text exported to the \code{figures_tables_dir}. +} +\description{ +Gather captions and alternative text from \code{stockplotr} figure and table rda files +} +\examples{ +\dontrun{ +save_all_plots(example_data) + +gather_rda_text( + figures_tables_dir = getwd() +) +} +} From e9724e05cf5b9e859cc9f8eb94a6fc3278e548dd Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 2 Dec 2026 13:30:14 -0500 Subject: [PATCH 2/4] Update FAQs vignette to mention new function --- vignettes/faqs.Rmd | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vignettes/faqs.Rmd b/vignettes/faqs.Rmd index 51155a14..7bc621f0 100644 --- a/vignettes/faqs.Rmd +++ b/vignettes/faqs.Rmd @@ -29,6 +29,10 @@ As stated in the [Accessibility vignette's "Your to-do list" section](https://nm **2. Write the final component of each figure’s alt text.** +## How can I view all existing captions and alt text? + +Use the `gather_caps_alttext()` function to view all existing rda files' captions and alt text. + ## How can I alter the captions and/or alt text? Directly edit the rda's caption or alt_text objects. To do so, assign a new value to the text you wish to change. For example: From d4a3b4295e44698a0cbf0d3dc6cfb8805f052771 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 2 Dec 2026 13:44:10 -0500 Subject: [PATCH 3/4] Make test --- tests/testthat/test-gather_rda_text.R | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/testthat/test-gather_rda_text.R diff --git a/tests/testthat/test-gather_rda_text.R b/tests/testthat/test-gather_rda_text.R new file mode 100644 index 00000000..9299ab51 --- /dev/null +++ b/tests/testthat/test-gather_rda_text.R @@ -0,0 +1,21 @@ +test_that("gather_rda_text extracts data from figure and table", { + + plot_biomass(example_data, + module = "TIME_SERIES", + make_rda = T, + figures_dir = tempdir()) + table_landings(example_data, + make_rda = T, + tables_dir = tempdir()) + + gather_rda_text(tempdir()) + + exported_text <- file.path(tempdir(), "all_captions_alttext.csv") + expect_true(file.exists(exported_text)) + + exp_text <- read.csv(exported_text) + + expect_equal(exp_text[1,3], "Line graph showing biomass time series. The x axis shows the year, which spans from 1874 to 2034. The y axis shows biomass in kg, which spans from 5420.45 to 42648.7.") + expect_equal(exp_text[2,2], "Landed catch by fleet and year in kg.") + +}) From 8f01dcc2d87544236c22ded903303fc4287dae1f Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 2 Dec 2026 14:17:39 -0500 Subject: [PATCH 4/4] Fix test --- tests/testthat/test-gather_rda_text.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-gather_rda_text.R b/tests/testthat/test-gather_rda_text.R index 9299ab51..d51b2d47 100644 --- a/tests/testthat/test-gather_rda_text.R +++ b/tests/testthat/test-gather_rda_text.R @@ -3,9 +3,11 @@ test_that("gather_rda_text extracts data from figure and table", { plot_biomass(example_data, module = "TIME_SERIES", make_rda = T, + unit_label = "kg", figures_dir = tempdir()) table_landings(example_data, - make_rda = T, + make_rda = T, + unit_label = "kg", tables_dir = tempdir()) gather_rda_text(tempdir()) @@ -15,7 +17,7 @@ test_that("gather_rda_text extracts data from figure and table", { exp_text <- read.csv(exported_text) - expect_equal(exp_text[1,3], "Line graph showing biomass time series. The x axis shows the year, which spans from 1874 to 2034. The y axis shows biomass in kg, which spans from 5420.45 to 42648.7.") + expect_equal(exp_text[1,3], "Line graph showing biomass time series. The x axis shows the year, which spans from 1874 to 2034. The y axis shows biomass in kg, which spans from 0 to 2339.74.") expect_equal(exp_text[2,2], "Landed catch by fleet and year in kg.") })