Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions R/gather_rda_text.R
Original file line number Diff line number Diff line change
@@ -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)

}
29 changes: 29 additions & 0 deletions man/gather_rda_text.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions tests/testthat/test-gather_rda_text.R
Original file line number Diff line number Diff line change
@@ -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.")

})
4 changes: 4 additions & 0 deletions vignettes/faqs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading