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() +) +} +} diff --git a/tests/testthat/test-gather_rda_text.R b/tests/testthat/test-gather_rda_text.R new file mode 100644 index 00000000..d51b2d47 --- /dev/null +++ b/tests/testthat/test-gather_rda_text.R @@ -0,0 +1,23 @@ +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, + unit_label = "kg", + 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 0 to 2339.74.") + expect_equal(exp_text[2,2], "Landed catch by fleet and year in kg.") + +}) 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: