From c3204f2c5074ae4ae7aa905881d815d2d31bb03e Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 5 Mar 2026 16:48:55 -0500 Subject: [PATCH 01/23] Create first draft of key quantities template csv; start drafting pipeline to add kqs to it in landings plot --- R/plot_landings.R | 81 ++++++++++++++--- inst/resources/key_quantity_template.csv | 109 +++++++++++++++++++++++ 2 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 inst/resources/key_quantity_template.csv diff --git a/R/plot_landings.R b/R/plot_landings.R index 7fc9041e..047ca5d0 100644 --- a/R/plot_landings.R +++ b/R/plot_landings.R @@ -99,16 +99,77 @@ plot_landings <- function( ### Make RDA ---- if (make_rda) { - create_rda( - object = plt, - # get name of function and remove "plot_" from it - topic_label = gsub("plot_", "", as.character(sys.call()[[1]])), - fig_or_table = "figure", - dat = dat, - dir = figures_dir, - scale_amount = scale_amount, - unit_label = unit_label - ) + + if (file.exists(fs::path(dir, "key_quantities.csv"))) { + cli::cli_alert_info("Key quantities text file (key_quantities.csv) exists. Newly calculated key quantities will be added to it.", wrap = TRUE) + caps_alttext <- utils::read.csv( + file.path(dir, "key_quantities.csv") + ) + } else { + caps_alttext <- utils::read.csv( + system.file("resources", "key_quantity_template.csv", package = "stockplotr") + ) + } + + # Obtain relevant key quantities for captions/alt text + landings.end.year <- max(prepared_data$year) + landings.max <- max(prepared_data$estimate) + landings.min <- min(prepared_data$estimate) + landings.start.year <- min(prepared_data$year) + landings.units <- unit_label + + fill_in_kqs <- function(df, ...) { + + arg_names <- sapply(substitute(list(...))[-1], deparse) + arg_values <- list(...) + + lookup_df <- tibble::tibble( + key_quantity = arg_names, + value_new = purrr::map_chr(arg_values, as.character) + ) + + # TODO: Add message when certain values aren't overwritten (already present) + df <- df |> + dplyr::mutate(across(everything(), as.character)) |> + dplyr::left_join(lookup_df, by = "key_quantity") |> + dplyr::mutate(value = dplyr::if_else( + (is.na(value) | value == "" & !is.na(value_new)), + value_new, + value)) |> + dplyr::select(-value_new) + } + } + + caps_alttext_filled <- fill_in_kqs(caps_alttext, + landings.max, + landings.min, + landings.start.year, + landings.end.year, + landings.units) + + # next step: export caps_alttext_filled + + # export df with updated captions and alt text to csv + # utils::write.csv( + # x = caps_alttext, + # file = fs::path( + # dir, + # "key_quantities.csv" + # ), + # row.names = FALSE + # ) + + + # create_rda( + # object = plt, + # # get name of function and remove "plot_" from it + # topic_label = gsub("plot_", "", as.character(sys.call()[[1]])), + # fig_or_table = "figure", + # dat = dat, + # dir = figures_dir, + # scale_amount = scale_amount, + # unit_label = unit_label + # ) } # Output final plot plt diff --git a/inst/resources/key_quantity_template.csv b/inst/resources/key_quantity_template.csv new file mode 100644 index 00000000..f6a637a6 --- /dev/null +++ b/inst/resources/key_quantity_template.csv @@ -0,0 +1,109 @@ +key_quantity,value,meaning,dependent_on_other_kq,dependent_kq,dependent_on_other_fxn_args,fxn_with_other_args +B.BMSY.end.yr,,,,,, +B.start.year,,,,,, +Bend,,,,,, +Bmsy,,,,,, +Btarg,,,,,, +caa.age.max,,,,,, +caa.age.min,,,,,, +caa.end.year,,,,,, +caa.start.year,,,,,, +cal.length.max,,,,,, +cal.length.min,,,,,, +cpue.end.year,,,,,, +cpue.max,,,,,, +cpue.min,,,,,, +cpue.start.year,,,,,, +F.FMSY.end.yr,,,,,, +F.max,,,,,, +F.min,,,,,, +F.ref.pt,,,,,, +F.start.year,,,,,, +fecundity.length.max,,,,,, +fecundity.length.min,,,,,, +fecundity.length.units,,,,,, +fecundity.max,,,,,, +fecundity.min,,,,,, +fecundity.units,,,,,, +fork.length.max,,,,,, +fork.length.min,,,,,, +fork.length.units,,,,,, +Ftarg,,,,,, +kobe.end.year,,,,,, +landings.end.year,,,,,, +landings.max,,,,,, +landings.min,,,,,, +landings.start.year,,,,,, +landings.units,,,,,, +M.age.max,,,,,, +M.age.min,,,,,, +M.rate.max,,,,,, +M.rate.min,,,,,, +mod.fit.abun.end.year,,,,,, +mod.fit.abun.start.year,,,,,, +mod.fit.catch.end.year,,,,,, +mod.fit.catch.max,,,,,, +mod.fit.catch.min,,,,,, +mod.fit.catch.start.year,,,,,, +mod.fit.catch.units,,,,,, +mod.fit.discards.end.year,,,,,, +mod.fit.discards.max,,,,,, +mod.fit.discards.min,,,,,, +mod.fit.discards.start.year,,,,,, +mod.fit.discards.units,,,,,, +overfished.status.is.isnot,,,,,, +overfishing.status.is.isnot,,,,,, +pop.baa.age.max,,,,,, +pop.baa.age.min,,,,,, +pop.baa.end.year,,,,,, +pop.baa.fish.max,,,,,, +pop.baa.fish.min,,,,,, +pop.baa.start.year,,,,,, +pop.naa.age.max,,,,,, +pop.naa.age.min,,,,,, +pop.naa.end.year,,,,,, +pop.naa.fish.max,,,,,, +pop.naa.fish.min,,,,,, +pop.naa.start.year,,,,,, +proj.catch.end.year,,,,,, +proj.catch.min,,,,,, +proj.catch.start.year,,,,,, +proj.catch.units,,,,,, +prop.mat.length.max,,,,,, +prop.mat.length.min,,,,,, +prop.mat.length.units,,,,,, +R0,,,,,, +recruit.dev.max,,,,,, +recruit.dev.min,,,,,, +recruit.dev.start.year,,,,,, +recruitment.start.year,,,,,, +rel.B.max,,,,,, +rel.B.min,,,,,, +rel.recruitment.max,,,,,, +rel.recruitment.min,,,,,, +selectivity.end.year,,,,,, +selectivity.length.max,,,,,, +selectivity.length.min,,,,,, +selectivity.length.units,,,,,, +selectivity.start.year,,,,,, +spr.max,,,,,, +spr.min,,,,,, +spr.ref.pt,,,,,, +sr.age.min,,,,,, +ssb.start.year,,,,,, +tot.catch.max,,,,,, +tot.catch.min,,,,,, +total.length.max,,,,,, +total.length.min,,,,,, +total.length.units,,,,,, +vonb.age.max,,,,,, +vonb.age.min,,,,,, +vonb.length.max,,,,,, +vonb.length.min,,,,,, +vonb.length.units,,,,,, +wl.length.max,,,,,, +wl.length.min,,,,,, +wl.length.units,,,,,, +wl.weight.max,,,,,, +wl.weight.min,,,,,, +wl.weight.units,,,,,, \ No newline at end of file From a980f1220222b3db841203a7f94f7fb2d218eb9b Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 12 Mar 2026 13:26:36 -0400 Subject: [PATCH 02/23] Finished draft of key quantities pipeline for landings plot --- DESCRIPTION | 1 + R/plot_landings.R | 95 ++++++-------------- R/utils_rda.R | 219 +++++++++++++++++++++++++--------------------- 3 files changed, 147 insertions(+), 168 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3f5e946f..99d97f9d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -48,6 +48,7 @@ Imports: naniar, prodlim, quarto, + rlang, scales, stats, stringr, diff --git a/R/plot_landings.R b/R/plot_landings.R index 047ca5d0..bf7bdb64 100644 --- a/R/plot_landings.R +++ b/R/plot_landings.R @@ -100,76 +100,37 @@ plot_landings <- function( ### Make RDA ---- if (make_rda) { - if (file.exists(fs::path(dir, "key_quantities.csv"))) { - cli::cli_alert_info("Key quantities text file (key_quantities.csv) exists. Newly calculated key quantities will be added to it.", wrap = TRUE) - caps_alttext <- utils::read.csv( - file.path(dir, "key_quantities.csv") - ) - } else { - caps_alttext <- utils::read.csv( - system.file("resources", "key_quantity_template.csv", package = "stockplotr") - ) - } + # Obtain relevant key quantities for captions/alt text + landings.end.year <- max(prepared_data$year) + landings.max <- max(prepared_data$estimate) + landings.min <- min(prepared_data$estimate) + landings.start.year <- min(prepared_data$year) + landings.units <- unit_label - # Obtain relevant key quantities for captions/alt text - landings.end.year <- max(prepared_data$year) - landings.max <- max(prepared_data$estimate) - landings.min <- min(prepared_data$estimate) - landings.start.year <- min(prepared_data$year) - landings.units <- unit_label - - fill_in_kqs <- function(df, ...) { - - arg_names <- sapply(substitute(list(...))[-1], deparse) - arg_values <- list(...) - - lookup_df <- tibble::tibble( - key_quantity = arg_names, - value_new = purrr::map_chr(arg_values, as.character) - ) - - # TODO: Add message when certain values aren't overwritten (already present) - df <- df |> - dplyr::mutate(across(everything(), as.character)) |> - dplyr::left_join(lookup_df, by = "key_quantity") |> - dplyr::mutate(value = dplyr::if_else( - (is.na(value) | value == "" & !is.na(value_new)), - value_new, - value)) |> - dplyr::select(-value_new) - } - } - - caps_alttext_filled <- fill_in_kqs(caps_alttext, - landings.max, - landings.min, - landings.start.year, - landings.end.year, - landings.units) - - # next step: export caps_alttext_filled - - # export df with updated captions and alt text to csv - # utils::write.csv( - # x = caps_alttext, - # file = fs::path( - # dir, - # "key_quantities.csv" - # ), - # row.names = FALSE - # ) + # calculate & export key quantities + export_kqs(landings.end.year, + landings.max, + landings.min, + landings.start.year, + landings.units) + # Add key quantities to captions/alt text + insert_kqs(landings.end.year, + landings.max, + landings.min, + landings.start.year, + landings.units) - # create_rda( - # object = plt, - # # get name of function and remove "plot_" from it - # topic_label = gsub("plot_", "", as.character(sys.call()[[1]])), - # fig_or_table = "figure", - # dat = dat, - # dir = figures_dir, - # scale_amount = scale_amount, - # unit_label = unit_label - # ) + create_rda( + object = plt, + # get name of function and remove "plot_" from it + topic_label = gsub("plot_", "", as.character(sys.call()[[1]])), + fig_or_table = "figure", + dat = dat, + dir = figures_dir, + scale_amount = scale_amount, + unit_label = unit_label + ) } # Output final plot plt diff --git a/R/utils_rda.R b/R/utils_rda.R index 6d513427..453221ce 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -2,6 +2,123 @@ # RDA utility functions # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +# TODO: update new fxns to work with a specified 'dir' instead of default 'getwd()'? + +# Fill in key quantities in template +fill_in_kqs <- function(df, ...) { + + arg_names <- sapply(substitute(list(...))[-1], deparse) + arg_values <- list(...) + + lookup_df <- tibble::tibble( + key_quantity = arg_names, + value_new = purrr::map_chr(arg_values, as.character) + ) + + # TODO: Add message when certain values aren't overwritten (already present) + df <- df |> + dplyr::mutate(across(everything(), as.character)) |> + dplyr::left_join(lookup_df, by = "key_quantity") |> + dplyr::mutate(value = dplyr::if_else( + (is.na(value) | value == "" & !is.na(value_new)), + value_new, + value)) |> + dplyr::select(-value_new) +} + +# Calculate and export key quantities +## kqs (e.g., landings.end.year) are the ellipsis args +export_kqs <- function(...) { + + # Open new or existing key quantities csv + if (file.exists(fs::path(getwd(), "key_quantities.csv"))) { + cli::cli_alert_info("Key quantities text file (key_quantities.csv) exists. Newly calculated key quantities will be added to it.", wrap = TRUE) + kqs <- utils::read.csv(file.path(getwd(), "key_quantities.csv")) + } else { + kqs <- utils::read.csv( + system.file("resources", "key_quantity_template.csv", package = "stockplotr") + ) + } + + # kqs (e.g., landings.end.year) are the ellipsis args + kqs_filled <- fill_in_kqs(kqs, + ...) + + utils::write.csv( + x = kqs_filled, + file = fs::path(getwd(), "key_quantities.csv"), + row.names = FALSE + ) + +} + +# Add key quantities to captions/alt text csv +## kqs (e.g., landings.end.year) are the ellipsis args +insert_kqs <- function(...) { + if (file.exists(fs::path(getwd(), "captions_alt_text.csv"))) { + cli::cli_alert_info("Captions/alternative text file (captions_alt_text.csv) exists. Newly calculated key quantities will be added to it.", wrap = TRUE) + caps_alttext <- utils::read.csv(fs::path(getwd(), "captions_alt_text.csv")) + } else { + caps_alttext <- utils::read.csv( + system.file("resources", "captions_alt_text_template.csv", package = "stockplotr") + ) + } + + create_patterns <- function(...) { + # Capture the names from the dots without evaluating them yet + arg_names <- sapply(rlang::enexprs(...), as.character) + + # Get the actual values + vals <- list(...) + + # Combine them into a named character vector + stats::setNames(as.character(vals), arg_names) + } + + # insert new kqs into alt text/caps csv, where applicable + patterns_replacements <- create_patterns(...) |> + # If a value = NA, then make it "NA" to avoid errors + tidyr::replace_na("NA") + + # replace values in caption column + caps_alttext$caption <- stringr::str_replace_all( + caps_alttext$caption, + patterns_replacements + ) + + # replace values in alt text column + caps_alttext$alt_text <- stringr::str_replace_all( + caps_alttext$alt_text, + patterns_replacements + ) + + # export df with updated captions and alt text to csv + utils::write.csv( + x = caps_alttext, + file = fs::path(getwd(), "captions_alt_text.csv"), + row.names = FALSE + ) + + # message explaining the extracted and inserted key quantities + replaced_vals <- patterns_replacements |> + as.data.frame() |> + tibble::rownames_to_column() |> + dplyr::rename( + "name" = 1, + "key_quantity" = 2 + ) + + cli::cli_h3("The following key quantities were extracted and inserted into 'captions_alt_text.csv' and 'key_quantities.csv':") + for (i in 1:dim(replaced_vals)[1]) { + cli::cli_li(paste0( + replaced_vals[i, 1], + ": ", + replaced_vals[i, 2] + )) + } +} + + #' Create the rda package for a plot or table #' #' @param object Table or plot object @@ -46,38 +163,6 @@ create_rda <- function( unit_label = "mt", table_df = NULL ) { - # run write_captions.R if its output doesn't exist - if (!file.exists( - fs::path(getwd(), "captions_alt_text.csv") - ) - ) { - write_captions( - dat = dat, - dir = dir, - year = max(dat$year, na.rm = TRUE) # this is not right I think - ) - } - - # Remove non-numeric strings from year - year <- dat |> - dplyr::filter( - year %notin% c("Virg", "S/Rcurve", "Init", "selex"), - era == "time" - ) |> - dplyr::mutate(year = as.numeric(year)) - - # add more key quantities included as arguments in this fxn - add_more_key_quants( - dat, - topic = topic_label, - fig_or_table = fig_or_table, - dir = dir, - end_year = max(year$year, na.rm = TRUE), - units = unit_label, - ref_pt = ref_point, - ref_line = ref_line, - scaling = scale_amount - ) # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext( @@ -96,7 +181,7 @@ create_rda <- function( export_rda( object = object, - caps_alttext = caps_alttext, # Load in of this is missing I think + caps_alttext = caps_alttext, figures_tables_dir = dir, topic_label = topic_label, fig_or_table = fig_or_table, @@ -880,62 +965,6 @@ write_captions <- function(dat, # converted model output object # F.Ftarg : added with add_more_key_quants - ## landings plot - - # start year of landings plot - landings.start.year <- dat |> - dplyr::filter( - c(module_name == "t.series" & grepl("landings_observed", label)) | c(module_name == "CATCH" & grepl("ret_bio", label)), - # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output - !is.na(fleet) - ) |> - dplyr::slice(which.min(year)) |> - dplyr::select(year) |> - as.numeric() - - # end year of landings plot - landings.end.year <- dat |> - dplyr::filter( - c(module_name == "t.series" & grepl("landings_observed", label)) | c(module_name == "CATCH" & grepl("ret_bio", label)), - # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output - !is.na(fleet) - ) |> - dplyr::slice(which.max(year)) |> - dplyr::select(year) |> - as.numeric() - - # units of landings (plural) - # landings.units : added with add_more_key_quants - - # minimum landings - landings.min <- dat |> - dplyr::filter( - c(module_name == "t.series" & grepl("landings_observed", label)) | c(module_name == "CATCH" & grepl("ret_bio", label)), - # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output - !is.na(fleet) - ) |> - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - - # maximum landings - landings.max <- dat |> - dplyr::filter( - c(module_name == "t.series" & grepl("landings_observed", label)) | c(module_name == "CATCH" & grepl("ret_bio", label)), - # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output - !is.na(fleet) - ) |> - dplyr::group_by(fleet, year) |> - dplyr::summarise(max_est = max(estimate)) |> - dplyr::filter(!is.na(max_est)) |> - dplyr::group_by(year) |> - dplyr::summarise(max_est_yr = sum(max_est)) |> - dplyr::slice(which.max(max_est_yr)) |> - dplyr::select(max_est_yr) |> - as.numeric() |> - round(digits = 2) - ## natural mortality (M)- bam examples have label as natural_mortality ## but other formats don't (in input) # minimum age of M @@ -1492,9 +1521,6 @@ write_captions <- function(dat, # converted model output object ## catch # catch.fleet <- # fleet - ## landings - # landings.tbl.units <- # landings units; remove if units already in table - ## discards # discards.tbl.units <- # discards units @@ -1539,12 +1565,6 @@ write_captions <- function(dat, # converted model output object "F.max" = as.character(F.max), # 'Ftarg' = as.character(Ftarg), - ## landings plot - "landings.start.year" = as.character(landings.start.year), - "landings.end.year" = as.character(landings.end.year), - "landings.min" = as.character(landings.min), - "landings.max" = as.character(landings.max), - ## natural mortality (M) "M.age.min" = as.character(M.age.min), "M.age.max" = as.character(M.age.max), @@ -1687,9 +1707,6 @@ write_captions <- function(dat, # converted model output object # ## catch # 'catch.fleet' = as.character(catch.fleet), # - # ## landings - # 'landings.tbl.units' = as.character(landings.tbl.units), - # # ## discards # 'discards.tbl.units' = as.character(discards.tbl.units), # From 861ba15012b57054c59977301b85c0b74dbeea33 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Tue, 17 Mar 2026 09:53:06 -0400 Subject: [PATCH 03/23] Reorder KQs in plot_landings() by logical grouping; update plot_recruitment_deviations() to work with new KQ extraction/insertion design --- R/plot_landings.R | 16 +++---- R/plot_recruitment_deviations.R | 19 ++++++++ R/utils_rda.R | 60 ------------------------ inst/resources/key_quantity_template.csv | 1 + 4 files changed, 28 insertions(+), 68 deletions(-) diff --git a/R/plot_landings.R b/R/plot_landings.R index bf7bdb64..8f7e32a4 100644 --- a/R/plot_landings.R +++ b/R/plot_landings.R @@ -101,24 +101,24 @@ plot_landings <- function( if (make_rda) { # Obtain relevant key quantities for captions/alt text + landings.start.year <- min(prepared_data$year) landings.end.year <- max(prepared_data$year) - landings.max <- max(prepared_data$estimate) landings.min <- min(prepared_data$estimate) - landings.start.year <- min(prepared_data$year) + landings.max <- max(prepared_data$estimate) landings.units <- unit_label # calculate & export key quantities - export_kqs(landings.end.year, - landings.max, + export_kqs(landings.start.year, + landings.end.year, landings.min, - landings.start.year, + landings.max, landings.units) # Add key quantities to captions/alt text - insert_kqs(landings.end.year, - landings.max, + insert_kqs(landings.start.year, + landings.end.year, landings.min, - landings.start.year, + landings.max, landings.units) create_rda( diff --git a/R/plot_recruitment_deviations.R b/R/plot_recruitment_deviations.R index ca84f27b..e757cd68 100644 --- a/R/plot_recruitment_deviations.R +++ b/R/plot_recruitment_deviations.R @@ -100,6 +100,25 @@ plot_recruitment_deviations <- function( } else { selected_dat <- dat } + + # Obtain relevant key quantities for captions/alt text + recruit.dev.start.year <- min(filter_data$year) + recruit.dev.end.year <- max(filter_data$year) + recruit.dev.min <- min(filter_data$estimate) + recruit.dev.max <- max(filter_data$estimate) + + # calculate & export key quantities + export_kqs(recruit.dev.start.year, + recruit.dev.end.year, + recruit.dev.min, + recruit.dev.max) + + # Add key quantities to captions/alt text + insert_kqs(recruit.dev.start.year, + recruit.dev.end.year, + recruit.dev.min, + recruit.dev.max) + create_rda( object = final, # get name of function and remove "plot_" from it diff --git a/R/utils_rda.R b/R/utils_rda.R index 453221ce..7a31112c 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -1299,61 +1299,6 @@ write_captions <- function(dat, # converted model output object # rel.recruitment.max <- (sr.max / R0) |> # round(digits = 2) - - ## recruitment deviations - # start year of recruitment deviations plot - recruit.dev.start.year <- dat |> - dplyr::filter( - label == "recruitment_deviations" | label == "log_recruitment_deviations", - module_name == "SPAWN_RECRUIT" | module_name == "t.series", - !is.na(year), - is.na(fleet) | length(unique(fleet)) <= 1, - is.na(sex) | length(unique(sex)) <= 1, - is.na(area) | length(unique(area)) <= 1, - is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, - !year %in% year_exclusions - ) |> # SS3 and BAM target module names - dplyr::slice(which.min(year)) |> - dplyr::select(year) |> - as.numeric() - - # end year of recruitment deviations plot - # recruit.dev.end.year : added with add_more_key_quants - - # minimum recruitment deviation - recruit.dev.min <- dat |> - dplyr::filter( - label == "recruitment_deviations" | label == "log_recruitment_deviations", - module_name == "SPAWN_RECRUIT" | module_name == "t.series", - !is.na(year), - is.na(fleet) | length(unique(fleet)) <= 1, - is.na(sex) | length(unique(sex)) <= 1, - is.na(area) | length(unique(area)) <= 1, - is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, - !year %in% year_exclusions - ) |> # SS3 and BAM target module names - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - - # maximum recruitment deviation - recruit.dev.max <- dat |> - dplyr::filter( - label == "recruitment_deviations" | label == "log_recruitment_deviations", - module_name == "SPAWN_RECRUIT" | module_name == "t.series", - !is.na(year), - is.na(fleet) | length(unique(fleet)) <= 1, - is.na(sex) | length(unique(sex)) <= 1, - is.na(area) | length(unique(area)) <= 1, - is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, - !year %in% year_exclusions - ) |> # SS3 and BAM target module names - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - ## tot_b (total biomass): same as B plot above ## spawning_biomass (ssb) @@ -1673,11 +1618,6 @@ write_captions <- function(dat, # converted model output object ## recruitment ts "recruitment.start.year" = as.character(recruitment.start.year), - ## recruitment deviations - "recruit.dev.start.year" = as.character(recruit.dev.start.year), - "recruit.dev.min" = as.character(recruit.dev.min), - "recruit.dev.max" = as.character(recruit.dev.max), - ## spawning.biomass (ssb) "ssb.start.year" = as.character(ssb.start.year), diff --git a/inst/resources/key_quantity_template.csv b/inst/resources/key_quantity_template.csv index f6a637a6..b9a04e21 100644 --- a/inst/resources/key_quantity_template.csv +++ b/inst/resources/key_quantity_template.csv @@ -73,6 +73,7 @@ prop.mat.length.max,,,,,, prop.mat.length.min,,,,,, prop.mat.length.units,,,,,, R0,,,,,, +recruit.dev.end.year,,,,,, recruit.dev.max,,,,,, recruit.dev.min,,,,,, recruit.dev.start.year,,,,,, From a592458293ec5eb830731326b3991534553fffcd Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Tue, 17 Mar 2026 14:07:15 -0400 Subject: [PATCH 04/23] update plot_fishing_mortality() to work with new KQ extraction/insertion design --- R/plot_fishing_mortality.R | 22 +++ R/utils_rda.R | 132 ------------------ inst/resources/captions_alt_text_template.csv | 2 +- inst/resources/key_quantity_template.csv | 1 + 4 files changed, 24 insertions(+), 133 deletions(-) diff --git a/R/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index 3a48dd54..4ffa7542 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -95,6 +95,28 @@ plot_fishing_mortality <- function( ### Make RDA ---- if (make_rda) { + + # Obtain relevant key quantities for captions/alt text + F.ref.pt <- as.character(ref_line) + F.start.year <- min(prepared_data$year) + F.end.year <- max(prepared_data$year) + F.min <- min(prepared_data$estimate) + F.max <- max(prepared_data$estimate) + + # calculate & export key quantities + export_kqs(F.ref.pt, + F.start.year, + F.end.year, + F.min, + F.max) + + # Add key quantities to captions/alt text + insert_kqs(F.ref.pt, + F.start.year, + F.end.year, + F.min, + F.max) + create_rda( object = final, topic_label = ifelse(relative, "relative_fishing_mortality", "fishing_mortality"), diff --git a/R/utils_rda.R b/R/utils_rda.R index 7a31112c..6582553a 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -234,47 +234,6 @@ add_more_key_quants <- function( cli::cli_h3("Key quantities extracted and inserted from add_more_key_quants():") - # calculate key quantities that rely on end_year for calculation - ## terminal fishing mortality - # if (topic_cap_alt$label == "fishing_mortality") { - # if (is.null(dat)) { - # cli::cli_alert_warning("Some key quantities associated with fishing mortality were not extracted and added to captions_alt_text.csv due to missing data file (i.e., 'dat' argument).", wrap = TRUE) - # } else { - # F.end.year <- dat |> - # dplyr::filter( - # c(label == "fishing_mortality" & - # year == end_year) | - # c(label == "terminal_fishing_mortality" & is.na(year)) - # ) |> - # dplyr::pull(estimate) |> - # as.numeric() |> - # round(digits = 2) - # - # # COMMENTING OUT THESE LINES because the current alt text/captions csv - # # doesn't include Ftarg or F.Ftarg. If we alter them to include them, - # # then uncomment these lines and add code that would substitute the key - # # quantities into the df, like at the bottom of write_captions. - # # - # # # recalculate Ftarg for F.Ftarg, below - # # Ftarg <- dat |> - # # dplyr::filter(grepl('f_target', label) | - # # grepl('f_msy', label) | - # # c(grepl('fishing_mortality_msy', label) & - # # is.na(year))) |> - # # dplyr::pull(estimate) |> - # # as.numeric() |> - # # round(digits = 2) - # # - # # # Terminal year F respective to F target - # # F.Ftarg <- F.end.year / Ftarg - # - # if (!is.null(F.end.year)) { - # end_year <- as.character(F.end.year) - # } - # } - # } - - # calculate key quantities that rely on scaling for calculation # TODO: pull the relative forms of these three KQs (B, R, SSB) from write_captions, # write analogous code for each in this section, and remove placeholders from @@ -876,94 +835,6 @@ write_captions <- function(dat, # converted model output object # rel.B.max <- (B.max / Btarg) |> # round(digits = 2) - # TODO: uncomment and recode once we get clarity about how to extract this value properly - ## mortality (F) plot - # F reference point - # F.ref.pt <- dat |> - # dplyr::filter( - # label == stringr::str_to_lower("F_targ") | - # label == stringr::str_to_lower("F_proxy") | - # label == stringr::str_to_lower("F_msy") | - # label == "F_target" - # # label == "F40" - # # label == "F30" - # # label == "F50" - # # label == "F_initial" - # # label == "Fmsy" - # ) |> - # dplyr::filter(module_name == "DERIVED_QUANTITIES" | module_name == "parms") |> - # dplyr::pull(estimate) |> - # as.numeric() |> - # round(digits = 2) - - # start year of F plot - F.start.year <- dat |> - dplyr::filter(label == "fishing_mortality") |> - dplyr::slice(which.min(year)) |> - dplyr::select(year) |> - as.numeric() - - # terminal fishing mortality - # F.end.year : added with add_more_key_quants - - # minimum F - F.min <- dat |> - dplyr::filter(label == "fishing_mortality") - - if (length(unique(F.min$age)) == 1) { - if (is.na(unique(F.min$age))) { - F.min <- F.min |> - dplyr::filter(module_name %in% c("TIME_SERIES", "t.series")) |> - dplyr::slice(which.min(estimate)) |> - as.numeric() |> - round(digits = 2) - } - } else { - F.min <- F.min |> - dplyr::group_by(age) |> - dplyr::summarize(val = max(estimate)) |> - dplyr::slice(which.min(val)) |> - dplyr::select(val) |> - as.numeric() |> - round(digits = 2) - } - - - # maximum F - F.max <- dat |> - dplyr::filter(label == "fishing_mortality") - - if (length(unique(F.max$age)) == 1) { - if (is.na(unique(F.max$age))) { - F.min <- F.min |> - dplyr::filter(module_name %in% c("TIME_SERIES", "t.series")) |> - dplyr::slice(which.max(estimate)) |> - as.numeric() |> - round(digits = 2) - } - } else { - F.max <- F.max |> - dplyr::group_by(age) |> - dplyr::summarize(val = max(estimate)) |> - dplyr::slice(which.max(val)) |> - dplyr::select(val) |> - as.numeric() |> - round(digits = 2) - } - - # TODO: uncomment and recode once we get clarity about how to extract this value properly - # fishing mortality at msy - # Ftarg <- dat |> - # dplyr::filter(grepl('f_target', label) | - # grepl('f_msy', label) | - # c(grepl('fishing_mortality_msy', label) & is.na(year))) |> - # dplyr::pull(estimate) |> - # as.numeric() |> - # round(digits = 2) - - # Terminal year F respective to F target - # F.Ftarg : added with add_more_key_quants - ## natural mortality (M)- bam examples have label as natural_mortality ## but other formats don't (in input) @@ -1505,9 +1376,6 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot # 'F.ref.pt' = as.character(F.ref.pt), - "F.start.year" = as.character(F.start.year), - "F.min" = as.character(F.min), - "F.max" = as.character(F.max), # 'Ftarg' = as.character(Ftarg), ## natural mortality (M) diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index d5ea858c..a2b5c135 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -2,7 +2,7 @@ label,type,caption,alt_text kobe,figure,"Kobe plot showing stock status. Triangles delineate start and end years. Horizontal and vertical dashed lines delineate the proportion of F/F~MSY~ where F~MSY~ = B.ref.pt, and B/B~MSY~ where B~MSY~ = B.ref.pt. Overfishing is occurring when F/F~MSY~ > 1 and an overfished status is indicated where B/B~MSY~ > 1.","A Kobe plot showing stock status. The x axis shows overfished status (i.e., B/B~MSY~) and the y axis shows overfishing (i.e., F/F~MSY~). The B/B~MSY~ and F/F~MSY~ for kobe.end.year were B.BMSY.end.yr and F.FMSY.end.yr, respectively, indicating that the stock overfished.status.is.isnot overfished and overfishing.status.is.is.not experiencing overfishing." biomass,figure,Biomass (B) time series. The horizontal dashed line represents the limit reference point (B.ref.pt B.units).,"Line graph showing biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows biomass in B.units, which spans from B.min to B.max." relative_biomass,figure,Relative biomass (B) time series. The horizontal dashed line represents the limit reference point calculated as B/B(reference point) (B.ref.point B.units).,"Line graph showing relative biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows relative biomass (B/B~target~), which spans from rel.B.min to rel.B.max B.units ." -fishing_mortality,figure,Fishing mortality (F) over time. The horizontal dashed line represents the target reference point (F.ref.pt).,"Line graph showing fishing mortality over time . The x axis shows the year, which spans from F.start.year to F.end.year . The y axis shows fishing mortality in F.units, which spans from F.min to F.max." +fishing_mortality,figure,Fishing mortality (F) over time. The horizontal dashed line represents the target reference point (F.ref.pt).,"Line graph showing fishing mortality over time . The x axis shows the year, which spans from F.start.year to F.end.year . The y axis shows fishing mortality, which spans from F.min to F.max." relative_fishing_mortality,figure,Relative fishing mortality (F) over time. The horizontal dashed line represents the target reference point (F.ref.pt).,"Line graph showing fishing mortality over time . The x axis shows the year, which spans from F.start.year to F.end.year . The y axis shows relative fishing mortality (F/F~target~), which spans from rel.F.min to rel.F.max." landings,figure,Historical landings by fleet.,"Cumulative area plot showing historical landings. The x axis shows the year, which spans from landings.start.year to landings.end.year . The y axis shows landings in landings.units, which spans from landings.min to landings.max." natural_mortality,figure,Natural mortality (M) for each age. ,"Line graph showing natural mortality. The x axis shows age in years, which spans from M.age.min to M.age.max. The y axis shows the natural mortality rate per year, which spans from M.rate.min to M.rate.max." diff --git a/inst/resources/key_quantity_template.csv b/inst/resources/key_quantity_template.csv index b9a04e21..b40f8f62 100644 --- a/inst/resources/key_quantity_template.csv +++ b/inst/resources/key_quantity_template.csv @@ -15,6 +15,7 @@ cpue.max,,,,,, cpue.min,,,,,, cpue.start.year,,,,,, F.FMSY.end.yr,,,,,, +F.end.year,,,,,, F.max,,,,,, F.min,,,,,, F.ref.pt,,,,,, From ab3e6a7531228dac734474c6d2705be7184d4d63 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Tue, 17 Mar 2026 14:30:26 -0400 Subject: [PATCH 05/23] update plot_stock_recruitment() to work with new KQ extraction/insertion design --- R/plot_stock_recruitment.R | 32 ++++++++ R/utils_rda.R | 81 ------------------- inst/resources/captions_alt_text_template.csv | 2 +- inst/resources/key_quantity_template.csv | 6 ++ 4 files changed, 39 insertions(+), 82 deletions(-) diff --git a/R/plot_stock_recruitment.R b/R/plot_stock_recruitment.R index 65732a51..ca7eea6d 100644 --- a/R/plot_stock_recruitment.R +++ b/R/plot_stock_recruitment.R @@ -127,6 +127,38 @@ plot_stock_recruitment <- function( # Make RDA if (make_rda) { + + # Obtain relevant key quantities for captions/alt text + sr.age.min <- dat |> + dplyr::filter(!is.na(year) & !is.na(age)) |> + dplyr::slice(which.min(age)) |> + dplyr::select(age) |> + as.numeric() + sr.ssb.units <- spawning_biomass_label + sr.ssb.min <- min(sr$spawning_biomass, na.rm = TRUE) + sr.ssb.max <- max(sr$spawning_biomass, na.rm = TRUE) + sr.min <- min(sr$predicted_recruitment, na.rm = TRUE) + sr.max <- max(sr$predicted_recruitment, na.rm = TRUE) + sr.units <- recruitment_label + + # calculate & export key quantities + export_kqs(sr.age.min, + sr.ssb.units, + sr.ssb.min, + sr.ssb.max, + sr.min, + sr.max, + sr.units) + + # Add key quantities to captions/alt text + insert_kqs(sr.age.min, + sr.ssb.units, + sr.ssb.min, + sr.ssb.max, + sr.min, + sr.max, + sr.units) + create_rda( object = final, # get name of function and remove "plot_" from it diff --git a/R/utils_rda.R b/R/utils_rda.R index 6582553a..95c53cec 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -470,65 +470,6 @@ add_more_key_quants <- function( } } - ## recruitment - if (topic_cap_alt$label == "recruitment" | topic_cap_alt$label == "stock_recruitment") { - if (is.null(dat)) { - cli::cli_alert_warning("Some key quantities associated with recruitment were not extracted and added to captions_alt_text.csv due to missing data file (i.e., 'dat' argument).", wrap = TRUE) - } else { - # minimum recruitment - sr.min <- dat |> - dplyr::filter( - label == "recruitment", - module_name == "TIME_SERIES" | module_name == "t.series", - !is.na(year), - is.na(fleet) | length(unique(fleet)) <= 1, - is.na(sex) | length(unique(sex)) <= 1, - is.na(area) | length(unique(area)) <= 1, - is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, - !year %in% year_exclusions - ) |> # SS3 and BAM target module names - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - dplyr::mutate(estimate = estimate / scaling) |> - as.numeric() |> - round(digits = 2) - - # maximum recruitment - sr.max <- dat |> - dplyr::filter( - label == "recruitment", - module_name == "TIME_SERIES" | module_name == "t.series", - !is.na(year), - is.na(fleet) | length(unique(fleet)) <= 1, - is.na(sex) | length(unique(sex)) <= 1, - is.na(area) | length(unique(area)) <= 1, - is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, - !year %in% year_exclusions - ) |> # SS3 and BAM target module names - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - dplyr::mutate(estimate = estimate / scaling) |> - as.numeric() |> - round(digits = 2) - - # replace sr.min and sr.max placeholders within topic_cap_alt - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "sr.min", - as.character(sr.min) - )) |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "sr.max", - as.character(sr.max) - )) - - cli::cli_li("sr.min: {as.character(sr.min)}") - cli::cli_li("sr.max: {as.character(sr.max)}") - } - } - # replace placeholders (e.g., if "end.year" is found in topic_alt, replace it with end_year) ## end_year----- if (!is.null(end_year)) { @@ -1114,25 +1055,6 @@ write_captions <- function(dat, # converted model output object # selectivity.length.min <- # minimum length # selectivity.length.max <- # maximum length - - ## estimated stock recruitment - # youngest-age recruited fish (instead of age-0) - sr.age.min <- dat |> - dplyr::filter(!is.na(year) & !is.na(age)) |> - dplyr::slice(which.min(age)) |> - dplyr::select(age) |> - as.numeric() - - # ssb units (plural) - # sr.ssb.units : added with add_more_key_quants - - # minimum ssb : added with add_more_key_quants - - # maximum ssb : added with add_more_key_quants - - # recruitment units (plural) - # sr.units : added with add_more_key_quants - # minimum recruitment : added with add_more_key_quants # maximum recruitment: added with add_more_key_quants @@ -1474,9 +1396,6 @@ write_captions <- function(dat, # converted model output object # 'selectivity.length.min' = as.character(selectivity.length.min), # 'selectivity.length.max' = as.character(selectivity.length.max), - ## estimated stock recruitment (aka spawning stock biomass) - # 'sr.age.min' = as.character(sr.age.min), - # relative recruitment ts # NOTE: moving this above recruitment so rel.recruitment.min isn't changed # to "rel." + recruitment.min (etc.) diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index a2b5c135..717c1eed 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -19,7 +19,7 @@ mod.fit.catch,figure,Observed catch from the data input file (points) and model mod.fit.abun,figure,Assessment model fits to input catch per unit of effort index values over time calculated from fleet.or.survey.name.,"Point and line graph showing the assessment model fit, displayed as a line, to input catch per unit effort index values, displayed as points over time, for fleet.or.survey.name. The x axis shows years, which spans from mod.fit.abun.start.year to mod.fit.abun.end.year . The y axis shows catch per unit effort in cpue.units, which spans from cpue.min to cpue.max." mod.fit.discards,figure,Observed discards (points) and estimated discards (line).,"Point and line graph showing the assessment model fit to observed discards. Observed discards are represented as points while the model fitted estimates are represented as a line over time for fleet.or.survey.name. The x axis shows the year, which spans from mod.fit.discards.start.year to mod.fit.discards.end.year . The y axis shows discards in mod.fit.discards.units, which spans from mod.fit.discards.min to mod.fit.discards.max." selectivity,figure,Length-based selectivity for each fleet and survey estimated by the assessment model.,"Line graph showing length-based selectivity for fleet.or.survey.name from selectivity.start.year to selectivity.end.year . The x axis shows length in selectivity.length.units, which spans from selectivity.length.min to selectivity.length.max. The y axis shows the proportion of the stock that the gear selects for, which spans from 0--1." -stock_recruitment,figure,Stock recruitment relationship estimated by the assessment model. ,"Point and line graph showing the relationship between stock biomass and newly recruited sr.age.min fish as estimated by the assessment model. Points represent model estimates of recruitment each year as a function of stock biomass, after adjusted by annual recruitment deviations. The line represents the best fit through the points for the stock relationship selected for use in the assessment model. The x axis shows spawning stock biomass in sr.ssb.units, which spans from sr.ssb.min to sr.ssb.max. The y axis shows recruitment in sr.units, which spans from sr.min to sr.max." +stock_recruitment,figure,Stock recruitment relationship estimated by the assessment model. ,"Point and line graph showing the relationship between stock biomass and newly recruited age sr.age.min fish as estimated by the assessment model. Points represent model estimates of recruitment each year as a function of stock biomass, after adjusted by annual recruitment deviations. The line represents the best fit through the points for the stock relationship selected for use in the assessment model. The x axis shows spawning stock biomass in sr.ssb.units, which spans from sr.ssb.min to sr.ssb.max. The y axis shows recruitment in sr.units, which spans from sr.min to sr.max." recruitment,figure,Estimated recruitment by the assessment model each year in recruitment.units.,"Line graph showing the assessment model estimated recruitment in sr.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year . The y axis shows recruitment in recruitment.units, which spans from sr.min to sr.max." recruitment.comp,figure,Predicted (black points) and expected (red line) recruitment by the assessment model each year in recruitment.units.,"Scatter plot showing the assessment model estimated recruitment in sr.units for each year with a red line showing the predicted recruitment in sr.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year . The y axis shows recruitment in recruitment.units, which spans from sr.min to sr.max." relative.recruitment,figure,"Estimated relative recruitment by the assessment model each year in recruitment.units, calculated as R/R~0~ where R~0~ is R0.","Line graph showing the assessment model estimated relative recruitment in sr.units for each year. The x axis shows year, which spans from recruitment.start.year to recruitment.end.year . The y axis shows relative recruitment (R/R~0~), which spans from rel.recruitment.min to rel.recruitment.max recruitment.units ." diff --git a/inst/resources/key_quantity_template.csv b/inst/resources/key_quantity_template.csv index b40f8f62..67de2291 100644 --- a/inst/resources/key_quantity_template.csv +++ b/inst/resources/key_quantity_template.csv @@ -92,6 +92,12 @@ spr.max,,,,,, spr.min,,,,,, spr.ref.pt,,,,,, sr.age.min,,,,,, +sr.max,,,,,, +sr.min,,,,,, +sr.ssb.max,,,,,, +sr.ssb.min,,,,,, +sr.ssb.units,,,,,, +sr.units,,,,,, ssb.start.year,,,,,, tot.catch.max,,,,,, tot.catch.min,,,,,, From 29b25eb1c593e49ed389637181a9852b2e439691 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 18 Mar 2026 10:35:01 -0400 Subject: [PATCH 06/23] update plot_abundance_at_age() to work with new KQ extraction/insertion design --- R/plot_abundance_at_age.R | 27 ++++++++++++++++- R/utils_rda.R | 63 --------------------------------------- 2 files changed, 26 insertions(+), 64 deletions(-) diff --git a/R/plot_abundance_at_age.R b/R/plot_abundance_at_age.R index a6a89638..b08b0a41 100644 --- a/R/plot_abundance_at_age.R +++ b/R/plot_abundance_at_age.R @@ -53,7 +53,7 @@ plot_abundance_at_age <- function( ) { # Create label for abundance units in legend abundance_label <- label_magnitude( - label = "Abudance", + label = "Abundance", unit_label = unit_label, scale_amount = scale_amount ) @@ -111,6 +111,31 @@ plot_abundance_at_age <- function( ) # export figure to rda if argument = T if (make_rda == TRUE) { + + # Obtain relevant key quantities for captions/alt text + pop.naa.start.year <- min(data$year) + pop.naa.end.year <- max(data$year) + pop.naa.age.min <- min(data$age) + pop.naa.age.max <- max(data$age) + pop.naa.fish.min <- min(data$estimate) + pop.naa.fish.max <- max(data$estimate) + + # calculate & export key quantities + export_kqs(pop.naa.start.year, + pop.naa.end.year, + pop.naa.age.min, + pop.naa.age.max, + pop.naa.fish.min, + pop.naa.fish.max) + + # Add key quantities to captions/alt text + insert_kqs(pop.naa.start.year, + pop.naa.end.year, + pop.naa.age.min, + pop.naa.age.max, + pop.naa.fish.min, + pop.naa.fish.max) + create_rda( object = plot, # get name of function and remove "plot_" from it diff --git a/R/utils_rda.R b/R/utils_rda.R index 95c53cec..d032b4c7 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -963,61 +963,6 @@ write_captions <- function(dat, # converted model output object # cpue.min <- # minimum CPUE (SHARED with mod_fit_abun, below) # cpue.max <- # maximum CPUE (SHARED with mod_fit_abun, below) - - ## NAA (numbers at age) - # start year of NAA plot - pop.naa.start.year <- dat |> - dplyr::filter(label == "abundance" & !is.na(year)) |> - dplyr::slice(which.min(year)) |> - dplyr::select(year) |> - as.numeric() - - # end year of NAA plot - pop.naa.end.year <- dat |> - dplyr::filter( - label == "abundance" & !is.na(year), - era == "time" - ) |> - dplyr::slice(which.max(year)) |> - dplyr::select(year) |> - as.numeric() - - # minimum age - pop.naa.age.min <- dat |> - dplyr::filter(label == "abundance" & !is.na(year)) |> - dplyr::slice(which.min(age)) |> - dplyr::select(age) |> - as.numeric() - - # maximum age - pop.naa.age.max <- dat |> - dplyr::filter(label == "abundance" & !is.na(year)) |> - dplyr::slice(which.max(age)) |> - dplyr::select(age) |> - as.numeric() - - # minimum abundance (number) of fish - pop.naa.fish.min <- dat |> - dplyr::filter( - grepl("abundance", label) & !is.na(year), - era == "time" - ) |> - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - - # maximum abundance (number) of fish - pop.naa.fish.max <- dat |> - dplyr::filter( - grepl("abundance", label) & !is.na(year), - era == "time" - ) |> - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - ## mod_fit_catch (model fit to catch ts)- don't code quantities yet # mod.fit.catch.start.year <- # start year of model fit to catch ts plot # mod.fit.catch.end.year <- # end year of model fit to catch ts plot @@ -1363,14 +1308,6 @@ write_captions <- function(dat, # converted model output object # 'cpue.min' = as.character(cpue.min), # 'cpue.max' = as.character(cpue.max), - ## NAA (numbers at age) - "pop.naa.start.year" = as.character(pop.naa.start.year), - "pop.naa.end.year" = as.character(pop.naa.end.year), - "pop.naa.age.min" = as.character(pop.naa.age.min), - "pop.naa.age.max" = as.character(pop.naa.age.max), - "pop.naa.fish.min" = as.character(pop.naa.fish.min), - "pop.naa.fish.max" = as.character(pop.naa.fish.max), - ## mod_fit_catch (model fit to catch ts) # 'mod.fit.catch.start.year' = as.character(mod.fit.catch.start.year), # 'mod.fit.catch.end.year' = as.character(mod.fit.catch.end.year), From 9ffb77a46424aa0e20186518b62984f0223b1d6e Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 18 Mar 2026 10:38:55 -0400 Subject: [PATCH 07/23] update plot_biomass_at_age() to work with new KQ extraction/insertion design --- R/plot_biomass_at_age.R | 25 ++++++++++++++ R/utils_rda.R | 72 ----------------------------------------- 2 files changed, 25 insertions(+), 72 deletions(-) diff --git a/R/plot_biomass_at_age.R b/R/plot_biomass_at_age.R index 041dfac7..c6d74112 100644 --- a/R/plot_biomass_at_age.R +++ b/R/plot_biomass_at_age.R @@ -95,6 +95,31 @@ plot_biomass_at_age <- function( ) # export figure to rda if argument = T if (make_rda == TRUE) { + + # Obtain relevant key quantities for captions/alt text + pop.baa.start.year <- min(data$year) + pop.baa.end.year <- max(data$year) + pop.baa.age.min <- min(data$age) + pop.baa.age.max <- max(data$age) + pop.baa.fish.min <- min(data$estimate) + pop.baa.fish.max <- max(data$estimate) + + # calculate & export key quantities + export_kqs(pop.baa.start.year, + pop.baa.end.year, + pop.baa.age.min, + pop.baa.age.max, + pop.baa.fish.min, + pop.baa.fish.max) + + # Add key quantities to captions/alt text + insert_kqs(pop.baa.start.year, + pop.baa.end.year, + pop.baa.age.min, + pop.baa.age.max, + pop.baa.fish.min, + pop.baa.fish.max) + create_rda( object = plot, # get name of function and remove "plot_" from it diff --git a/R/utils_rda.R b/R/utils_rda.R index d032b4c7..de9cf377 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -1106,69 +1106,6 @@ write_captions <- function(dat, # converted model output object # round(digits = 2) - ## pop.baa (population biomass at age) - # start year of pop.baa plot - pop.baa.start.year <- dat |> - dplyr::filter(grepl("^biomass", label)) |> - dplyr::slice(which.min(year)) |> - dplyr::select(year) |> - as.numeric() - - # end year of pop.baa plot - pop.baa.end.year <- dat |> - dplyr::filter( - grepl("^biomass", label), - era == "time" - ) |> - dplyr::slice(which.max(year)) |> - dplyr::select(year) |> - as.numeric() - - # minimum biomass of fish - pop.baa.fish.min <- dat |> - dplyr::filter( - grepl("^biomass", label) & !is.na(year), - era == "time" - ) |> - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - - # maximum biomass of fish - pop.baa.fish.max <- dat |> - dplyr::filter( - grepl("^biomass", label) & !is.na(year), - era == "time" - ) |> - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - - # minimum age - pop.baa.age.min <- dat |> - dplyr::filter( - label == "biomass", - module_name == "BIOMASS_AT_AGE" | module_name == "B.age", # SS3 and BAM target module names - !is.na(age) - ) |> - dplyr::slice(which.min(age)) |> - dplyr::select(age) |> - as.numeric() - - # maximum age - pop.baa.age.max <- dat |> - dplyr::filter( - label == "biomass", - module_name == "BIOMASS_AT_AGE" | module_name == "B.age", # SS3 and BAM target module names - !is.na(age) - ) |> - dplyr::slice(which.max(age)) |> - dplyr::select(age) |> - as.numeric() - - ## proj_catch (projected catch) # projected catch units (plural) # proj.catch.units <- # probably mt, but wait until figure coded @@ -1350,15 +1287,6 @@ write_captions <- function(dat, # converted model output object "spr.max" = as.character(spr.max), # 'spr.ref.pt' = as.character(spr.ref.pt), - # ## pop.baa (population biomass at age) - "pop.baa.start.year" = as.character(pop.baa.start.year), - "pop.baa.end.year" = as.character(pop.baa.end.year), - "pop.baa.fish.min" = as.character(pop.baa.fish.min), - "pop.baa.fish.max" = as.character(pop.baa.fish.max), - "pop.baa.age.min" = as.character(pop.baa.age.min), - "pop.baa.age.max" = as.character(pop.baa.age.max), - - ## proj_catch (projected catch) # 'proj.catch.units' = as.character(proj.catch.units), "proj.catch.start.year" = as.character(proj.catch.start.year), From 824f885d7c5efbeeea69b4945f11063714e1f0ad Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 18 Mar 2026 11:17:10 -0400 Subject: [PATCH 08/23] update plot_indices() to work with new KQ extraction/insertion design --- R/plot_indices.R | 26 +++++++++++++++++++++++- R/utils_rda.R | 16 --------------- inst/resources/key_quantity_template.csv | 1 + 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/R/plot_indices.R b/R/plot_indices.R index e0ff78e7..bda2ab1d 100644 --- a/R/plot_indices.R +++ b/R/plot_indices.R @@ -28,7 +28,7 @@ plot_indices <- function( module = NULL, focus = NULL, make_rda = FALSE, - figures_dir = NULL, + figures_dir = getwd(), ... ) { # Set cpue unit label for plot @@ -114,6 +114,30 @@ plot_indices <- function( ### Make RDA ---- if (make_rda) { + + # Obtain relevant key quantities for captions/alt text + cpue.start.year <- min(prepared_data$year) + cpue.end.year <- max(prepared_data$year) + cpue.min <- min(prepared_data$estimate) + cpue.max <- max(prepared_data$estimate) + cpue.units <- ifelse(unit_label == "", + "an estimated index ratio", # default if empty + unit_label) + + # calculate & export key quantities + export_kqs(cpue.start.year, + cpue.end.year, + cpue.min, + cpue.max, + cpue.units) + + # Add key quantities to captions/alt text + insert_kqs(cpue.start.year, + cpue.end.year, + cpue.min, + cpue.max, + cpue.units) + create_rda( object = plt, # get name of function and remove "plot_" from it diff --git a/R/utils_rda.R b/R/utils_rda.R index de9cf377..ede5f931 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -953,16 +953,6 @@ write_captions <- function(dat, # converted model output object # cal.length.max <- # maximum length group # fleet.or.survey.name <- # fleet or survey name - ## CPUE indices plot- don't code quantities yet - # cpue.start.year <- # start year of CPUE indices plot - # cpue.end.year <- # end year of CPUE indices plot - - # CPUE units (plural) (SHARED with mod.fit.abun, below) - # cpue.units : added with add_more_key_quants - - # cpue.min <- # minimum CPUE (SHARED with mod_fit_abun, below) - # cpue.max <- # maximum CPUE (SHARED with mod_fit_abun, below) - ## mod_fit_catch (model fit to catch ts)- don't code quantities yet # mod.fit.catch.start.year <- # start year of model fit to catch ts plot # mod.fit.catch.end.year <- # end year of model fit to catch ts plot @@ -1239,12 +1229,6 @@ write_captions <- function(dat, # converted model output object # 'cal.length.min' = as.character(cal.length.min), # 'cal.length.max' = as.character(cal.length.max), - ## CPUE indices plot - # 'cpue.start.year' = as.character(cpue.start.year), - # 'cpue.end.year' = as.character(cpue.end.year), - # 'cpue.min' = as.character(cpue.min), - # 'cpue.max' = as.character(cpue.max), - ## mod_fit_catch (model fit to catch ts) # 'mod.fit.catch.start.year' = as.character(mod.fit.catch.start.year), # 'mod.fit.catch.end.year' = as.character(mod.fit.catch.end.year), diff --git a/inst/resources/key_quantity_template.csv b/inst/resources/key_quantity_template.csv index 67de2291..9f261da3 100644 --- a/inst/resources/key_quantity_template.csv +++ b/inst/resources/key_quantity_template.csv @@ -14,6 +14,7 @@ cpue.end.year,,,,,, cpue.max,,,,,, cpue.min,,,,,, cpue.start.year,,,,,, +cpue.units,,,,,, F.FMSY.end.yr,,,,,, F.end.year,,,,,, F.max,,,,,, From a2604b24a62e2045b9d2cc7ffc380bf791b04375 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 18 Mar 2026 13:53:09 -0400 Subject: [PATCH 09/23] update plot_natural_mortality() to work with new KQ extraction/insertion design --- R/plot_natural_mortality.R | 24 ++++++++++++++++- R/utils_rda.R | 54 -------------------------------------- 2 files changed, 23 insertions(+), 55 deletions(-) diff --git a/R/plot_natural_mortality.R b/R/plot_natural_mortality.R index 9e496123..42483084 100644 --- a/R/plot_natural_mortality.R +++ b/R/plot_natural_mortality.R @@ -87,8 +87,11 @@ plot_natural_mortality <- function( dplyr::mutate(group_var = .data[[group]]) } + processed_data <- processed_data |> + dplyr::mutate(age = as.numeric(age)) + plt <- plot_timeseries( - dat = processed_data |> dplyr::mutate(age = as.numeric(age)), + dat = processed_data, x = "age", y = "estimate", geom = "line", @@ -102,6 +105,25 @@ plot_natural_mortality <- function( ### Make RDA ---- if (make_rda) { + + # Obtain relevant key quantities for captions/alt text + M.age.min <- min(processed_data$age) + M.age.max <- max(processed_data$age) + M.rate.min <- min(processed_data$estimate) + M.rate.max <- max(processed_data$estimate) + + # calculate & export key quantities + export_kqs(M.age.min, + M.age.max, + M.rate.min, + M.rate.max) + + # Add key quantities to captions/alt text + insert_kqs(M.age.min, + M.age.max, + M.rate.min, + M.rate.max) + create_rda( object = final, # get name of function and remove "plot_" from it diff --git a/R/utils_rda.R b/R/utils_rda.R index ede5f931..79f34ad7 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -776,54 +776,6 @@ write_captions <- function(dat, # converted model output object # rel.B.max <- (B.max / Btarg) |> # round(digits = 2) - - ## natural mortality (M)- bam examples have label as natural_mortality - ## but other formats don't (in input) - # minimum age of M - if ("natural_mortality" %in% dat$label) { - M.age.min <- dat |> - dplyr::filter(label == "natural_mortality") |> - dplyr::select(age) |> - dplyr::filter(!is.na(age)) |> - dplyr::slice(which.min(age)) |> - as.numeric() - } else { - M.age.min <- dat |> - # dplyr::filter(label == "natural_mortality") |> - dplyr::select(age) |> - dplyr::filter(!is.na(age)) |> - dplyr::slice(which.min(age)) |> - as.numeric() - } - - # maximum age of M - if ("natural_mortality" %in% dat$label) { - M.age.max <- dat |> - dplyr::filter(label == "natural_mortality") |> - dplyr::select(age) |> - dplyr::filter(!is.na(age)) |> - dplyr::slice(which.max(age)) |> - as.numeric() - } else { - M.age.max <- dat |> - # dplyr::filter(label == "natural_mortality") |> - dplyr::select(age) |> - dplyr::filter(!is.na(age)) |> - dplyr::slice(which.max(age)) |> - as.numeric() - } - - # minimum M rate- don't code quantities yet (see how it's coded in future fig) - # M.rate.min <- dat |> - # dplyr::filter( - # grepl("natural_mortality", label)) |> - # -label = natural_mortality (min); est in est col - - # maximum M rate- don't code quantities yet (see how it's coded in future fig) - # M.rate.max <- - # -label = natural_mortality (min); est in est col - - ## vonB LAA (von Bertalanffy growth function + length at age)- don't code quantities yet # vonb.age.min <- # minimum vonB age # vonb.age.max <- # maximum vonB age @@ -1172,12 +1124,6 @@ write_captions <- function(dat, # converted model output object # 'F.ref.pt' = as.character(F.ref.pt), # 'Ftarg' = as.character(Ftarg), - ## natural mortality (M) - "M.age.min" = as.character(M.age.min), - "M.age.max" = as.character(M.age.max), - # 'M.rate.min' = as.character(M.rate.min), - # 'M.rate.max' = as.character(M.rate.max), - ## vonB LAA (von Bertalanffy growth function + length at age) # 'vonb.age.min' = as.character(vonb.age.min), # 'vonb.age.max' = as.character(vonb.age.max), From c2acd9d688d7e83e47801cbe540285814ee2eeb6 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 19 Mar 2026 11:05:20 -0400 Subject: [PATCH 10/23] update plot_biomass() to work with new KQ extraction/insertion design --- R/plot_biomass.R | 25 ++++++++ R/utils_rda.R | 78 ------------------------ inst/resources/key_quantity_template.csv | 5 ++ 3 files changed, 30 insertions(+), 78 deletions(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index b89fa29a..2212fb40 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -148,6 +148,31 @@ plot_biomass <- function( ### Make RDA ---- if (make_rda) { + + # Obtain relevant key quantities for captions/alt text + B.ref.pt <- as.character(ref_line) + B.units <- as.character(unit_label) + B.start.year <- min(prepared_data$year) + B.end.year <- max(prepared_data$year) + B.min <- min(prepared_data$estimate) + B.max <- max(prepared_data$estimate) + + # calculate & export key quantities + export_kqs(B.ref.pt, + B.units, + B.start.year, + B.end.year, + B.min, + B.max) + + # Add key quantities to captions/alt text + insert_kqs(B.ref.pt, + B.units, + B.start.year, + B.end.year, + B.min, + B.max) + create_rda( object = final, topic_label = ifelse(relative, "relative_biomass", "biomass"), diff --git a/R/utils_rda.R b/R/utils_rda.R index 79f34ad7..7224b41a 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -243,57 +243,6 @@ add_more_key_quants <- function( # TODO: When adding code extracting values for landings and/or indices figures/tables, be aware that both figure/table share the same label ("landings" and "indices"). If there's reason, add in extra conditional to check if value is from a figure or table - ## biomass - if (topic_cap_alt$label == "biomass") { - if (is.null(dat)) { - cli::cli_alert_warning("Some key quantities associated with biomass were not extracted and added to captions_alt_text.csv due to missing data file (i.e., 'dat' argument).", wrap = TRUE) - } else { - # minimum biomass - B.min <- dat |> - dplyr::filter( - label == "biomass", - module_name == "TIME_SERIES" | module_name == "t.series", # SS3 and BAM target module names - is.na(fleet), - is.na(age) - ) |> - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - dplyr::mutate(estimate = estimate / scaling) |> - as.numeric() |> - round(digits = 2) - - # maximum biomass - B.max <- dat |> - dplyr::filter( - label == "biomass", - module_name == "TIME_SERIES" | module_name == "t.series", # SS3 and BAM target module names - is.na(fleet), - is.na(age) - ) |> - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - dplyr::mutate(estimate = estimate / scaling) |> - as.numeric() |> - round(digits = 2) - - # replace B.min and B.max placeholders within topic_cap_alt - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "B.min", - as.character(B.min) - )) |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "B.max", - as.character(B.max) - )) - - cli::cli_li("B.min: {as.character(B.min)}") - cli::cli_li("B.max: {as.character(B.max)}") - } - } - ## relative spawning biomass if (topic_cap_alt$label == "relative_spawning_biomass") { if (is.null(dat)) { @@ -711,33 +660,6 @@ write_captions <- function(dat, # converted model output object # overfishing.status.is.isnot <- - ## Biomass plot - # biomass reference point - # B.ref.pt : added with add_more_key_quants - - # start year of biomass plot - B.start.year <- dat |> - dplyr::filter( - label == "biomass", - module_name == "TIME_SERIES" | module_name == "t.series", # SS3 and BAM target module names - is.na(fleet), - is.na(age) - ) |> - dplyr::slice(which.min(year)) |> - dplyr::select(year) |> - as.numeric() - - # end year of biomass plot - # B.end.year : added with add_more_key_quants - - # units of B (plural) - # B.units : added with add_more_key_quants - - # minimum B : added with add_more_key_quants - - # maximum B : added with add_more_key_quants - - # TODO: uncomment and recode once we get clarity about how to extract this value properly # R0 # R0 <- dat |> diff --git a/inst/resources/key_quantity_template.csv b/inst/resources/key_quantity_template.csv index 9f261da3..2fd1dc69 100644 --- a/inst/resources/key_quantity_template.csv +++ b/inst/resources/key_quantity_template.csv @@ -1,6 +1,11 @@ key_quantity,value,meaning,dependent_on_other_kq,dependent_kq,dependent_on_other_fxn_args,fxn_with_other_args B.BMSY.end.yr,,,,,, +B.end.year,,,,,, +B.max,,,,,, +B.min,,,,,, +B.ref.pt,,,,,, B.start.year,,,,,, +B.units,,,,,, Bend,,,,,, Bmsy,,,,,, Btarg,,,,,, From 3d2f7e8fe545074fb92946109544b3691c3aad60 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 19 Mar 2026 11:18:43 -0400 Subject: [PATCH 11/23] update plot_spawning_biomass() to work with new KQ extraction/insertion design --- R/plot_spawning_biomass.R | 25 +++++++ R/utils_rda.R | 93 +----------------------- inst/resources/key_quantity_template.csv | 5 ++ 3 files changed, 32 insertions(+), 91 deletions(-) diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 115c33f2..eaa4bab2 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -235,6 +235,31 @@ plot_spawning_biomass <- function( ### Make RDA ---- if (make_rda) { + + # Obtain relevant key quantities for captions/alt text + ssb.ref.pt <- as.character(ref_line) + ssb.units <- as.character(unit_label) + ssb.start.year <- min(plot_data$year) + ssb.end.year <- max(plot_data$year) + ssb.min <- min(plot_data$estimate) + ssb.max <- max(plot_data$estimate) + + # calculate & export key quantities + export_kqs(ssb.ref.pt, + ssb.units, + ssb.start.year, + ssb.end.year, + ssb.min, + ssb.max) + + # Add key quantities to captions/alt text + insert_kqs(ssb.ref.pt, + ssb.units, + ssb.start.year, + ssb.end.year, + ssb.min, + ssb.max) + create_rda( object = final, topic_label = ifelse(relative, "relative_spawning_biomass", "spawning_biomass"), diff --git a/R/utils_rda.R b/R/utils_rda.R index 7224b41a..44669eda 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -262,30 +262,7 @@ add_more_key_quants <- function( if (length(ssbtarg) > 0) { cli::cli_alert_warning("ssbtarg, rel.ssb.min, and rel.ssb.max were not calculated. Check your ref_line is accurate.", wrap = TRUE) } else { - # ssb.min and ssb.max can be calculated in write_captions, but these quants - # are needed for rel values below, so including them here instead - # minimum ssb - ssb.min <- dat |> - dplyr::filter( - label == "spawning_biomass", - module_name %in% c("DERIVED_QUANTITIES", "t.series") - ) |> - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - - # maximum ssb - ssb.max <- dat |> - dplyr::filter( - label == "spawning_biomass", - module_name %in% c("DERIVED_QUANTITIES", "t.series") - ) |> - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - + # ssb.min and ssb.max needed for rel values below # relative ssb ## relative ssb min rel.ssb.min <- (ssb.min / ssbtarg) |> @@ -363,30 +340,7 @@ add_more_key_quants <- function( as.numeric() |> round(digits = 2) - # ssb.min and ssb.max can be calculated in write_captions, but these quants - # are needed for rel values below, so including them here instead - # minimum ssb - ssb.min <- dat |> - dplyr::filter( - label == "spawning_biomass", - module_name %in% c("DERIVED_QUANTITIES", "t.series") - ) |> - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - - # maximum ssb - ssb.max <- dat |> - dplyr::filter( - label == "spawning_biomass", - module_name %in% c("DERIVED_QUANTITIES", "t.series") - ) |> - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - + # ssb.min and ssb.max needed for rel values below # replace sr.ssb.min, sr.ssb.max, ssbtarg, ssb.min, and ssb.max placeholders # within topic_cap_alt topic_cap_alt <- topic_cap_alt |> @@ -399,23 +353,10 @@ add_more_key_quants <- function( alt_text, "sr.ssb.max", as.character(sr.ssb.max) - )) |> - # putting these last so they won't sub in for rel.ssb.min/max - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "ssb.min", - as.character(ssb.min) - )) |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "ssb.max", - as.character(ssb.max) )) cli::cli_li("sr.ssb.min: {as.character(sr.ssb.min)}") cli::cli_li("sr.ssb.max: {as.character(sr.ssb.max)}") - cli::cli_li("ssb.min: {as.character(ssb.min)}") - cli::cli_li("ssb.max: {as.character(ssb.max)}") } } @@ -903,33 +844,6 @@ write_captions <- function(dat, # converted model output object ## tot_b (total biomass): same as B plot above - ## spawning_biomass (ssb) - # start year of ssb plot - ssb.start.year <- dat |> - dplyr::filter( - label == "spawning_biomass", - module_name %in% c("DERIVED_QUANTITIES", "t.series") - ) |> - dplyr::slice(which.min(year)) |> - dplyr::select(year) |> - as.numeric() |> - round(digits = 2) - - # end year of ssb plot - # ssb.end.year : added with add_more_key_quants - - # ssb units (plural) - # ssb.units : added with add_more_key_quants - - # minimum ssb - # ssb.min : added with add_more_key_quants - - # maximum ssb - # ssb.max : added with add_more_key_quants - - # ssb reference point - # ssb.ref.pt : added with add_more_key_quants - # ssbtarg : added with add_more_key_quants ## relative ssb @@ -1131,9 +1045,6 @@ write_captions <- function(dat, # converted model output object ## recruitment ts "recruitment.start.year" = as.character(recruitment.start.year), - ## spawning.biomass (ssb) - "ssb.start.year" = as.character(ssb.start.year), - ## spr (spawning potential ratio) "spr.min" = as.character(spr.min), "spr.max" = as.character(spr.max), diff --git a/inst/resources/key_quantity_template.csv b/inst/resources/key_quantity_template.csv index 2fd1dc69..cf035e83 100644 --- a/inst/resources/key_quantity_template.csv +++ b/inst/resources/key_quantity_template.csv @@ -104,7 +104,12 @@ sr.ssb.max,,,,,, sr.ssb.min,,,,,, sr.ssb.units,,,,,, sr.units,,,,,, +ssb.end.year,,,,,, +ssb.max,,,,,, +ssb.min,,,,,, +ssb.ref.pt,,,,,, ssb.start.year,,,,,, +ssb.units,,,,,, tot.catch.max,,,,,, tot.catch.min,,,,,, total.length.max,,,,,, From 6ae08162ea5852d4cbf5d67257e826f1e494881b Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 19 Mar 2026 12:10:45 -0400 Subject: [PATCH 12/23] update plot_catch_comp() to work with new KQ extraction/insertion design --- R/plot_catch_comp.R | 29 +++++++++++++++++ R/utils_rda.R | 77 --------------------------------------------- 2 files changed, 29 insertions(+), 77 deletions(-) diff --git a/R/plot_catch_comp.R b/R/plot_catch_comp.R index 13b3f6fe..2667216c 100644 --- a/R/plot_catch_comp.R +++ b/R/plot_catch_comp.R @@ -100,6 +100,10 @@ plot_catch_comp <- function( group <- processed_data[[2]] facet <- processed_data[[3]] } + + data <- data |> + dplyr::mutate(age = as.numeric(age)) + # Plot data plot <- plot_aa( dat = data, @@ -111,6 +115,31 @@ plot_catch_comp <- function( # export figure to rda if argument = T if (make_rda == TRUE) { + + # Obtain relevant key quantities for captions/alt text + caa.start.year <- min(data$year) + caa.end.year <- max(data$year) + caa.age.min <- min(data$age) + caa.age.max <- max(data$age) + tot.catch.min <- min(data$estimate) + tot.catch.max <- max(data$estimate) + + # calculate & export key quantities + export_kqs(caa.start.year, + caa.end.year, + caa.age.min, + caa.age.max, + tot.catch.min, + tot.catch.max) + + # Add key quantities to captions/alt text + insert_kqs(caa.start.year, + caa.end.year, + caa.age.min, + caa.age.max, + tot.catch.min, + tot.catch.max) + create_rda( object = plot, # get name of function and remove "plot_" from it diff --git a/R/utils_rda.R b/R/utils_rda.R index 44669eda..1c0cc488 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -698,71 +698,6 @@ write_captions <- function(dat, # converted model output object # fecundity.min <- # minimum fecundity # fecundity.max <- # maximum fecundity - - ## catch at age (CAA) - # start year of CAA plot - caa.start.year <- dat |> - dplyr::filter(label == "abundance" & !is.na(year)) |> - dplyr::slice(which.min(year)) |> - dplyr::select(year) |> - as.numeric() - - # end year of CAA plot - caa.end.year <- dat |> - dplyr::filter(label == "abundance" & !is.na(year)) |> - dplyr::slice(which.max(year)) |> - dplyr::select(year) |> - as.numeric() - - # minimum age - caa.age.min <- dat |> - dplyr::filter(label == "abundance" & !is.na(year)) |> - dplyr::slice(which.min(age)) |> - dplyr::select(age) |> - as.numeric() - - # maximum age - caa.age.max <- dat |> - dplyr::filter(label == "abundance" & !is.na(year)) |> - dplyr::slice(which.max(age)) |> - dplyr::select(age) |> - as.numeric() - - - ## catch composition - # minimum & maximum catch - if (dim(dat |> - dplyr::filter(label == "catch"))[1] > 1) { - catch <- dat |> - dplyr::filter( - label == "catch", - !is.na(fleet) - ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year), - fleet = as.factor(fleet) - ) |> - dplyr::group_by(label, year, fleet) |> - dplyr::summarise(estimate = sum(estimate)) |> - dplyr::ungroup() - - tot.catch.min <- catch |> - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - - tot.catch.max <- catch |> - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - } else { - tot.catch.min <- "NA" - tot.catch.max <- "NA" - } - ## CAL (catch at length)- don't code quantities yet # cal.length.min <- # minimum length group # cal.length.max <- # maximum length group @@ -950,7 +885,6 @@ write_captions <- function(dat, # converted model output object # 'rel.B.max' = as.character(rel.B.max), ## Biomass plot - "B.start.year" = as.character(B.start.year), # 'R0' = as.character(R0), # 'Bend' = as.character(Bend), # 'Btarg' = as.character(Btarg), @@ -996,17 +930,6 @@ write_captions <- function(dat, # converted model output object # 'fecundity.min' = as.character(fecundity.min), # 'fecundity.max' = as.character(fecundity.max), - ## CAA (catch at age) - "caa.start.year" = as.character(caa.start.year), - "caa.end.year" = as.character(caa.end.year), - "caa.age.min" = as.character(caa.age.min), - "caa.age.max" = as.character(caa.age.max), - - - ## catch comp - "tot.catch.min" = as.character(tot.catch.min), - "tot.catch.max" = as.character(tot.catch.max), - ## CAL (catch at length) # 'cal.length.min' = as.character(cal.length.min), # 'cal.length.max' = as.character(cal.length.max), From 0eb1e23e0c5dca1b71407b3816a87a710c0ace94 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 19 Mar 2026 14:00:22 -0400 Subject: [PATCH 13/23] update plot_recruitment() to work with new KQ extraction/insertion design --- R/plot_recruitment.R | 24 ++++++++++ R/plot_stock_recruitment.R | 18 +++---- R/utils_rda.R | 47 +------------------ inst/resources/captions_alt_text_template.csv | 8 ++-- inst/resources/key_quantity_template.csv | 9 ++-- 5 files changed, 44 insertions(+), 62 deletions(-) diff --git a/R/plot_recruitment.R b/R/plot_recruitment.R index 5728113c..fd091c67 100644 --- a/R/plot_recruitment.R +++ b/R/plot_recruitment.R @@ -93,6 +93,7 @@ plot_recruitment <- function( geom <- "line" } + # Plot final <- plot_timeseries( dat = recruitment, @@ -144,6 +145,29 @@ plot_recruitment <- function( # Make RDA if (make_rda) { + + # Obtain relevant key quantities for captions/alt text + recruitment.units <- as.character(unit_label) + recruitment.start.year <- min(recruitment$year) + recruitment.end.year <- max(recruitment$year) + recruitment.min <- min(recruitment$predicted_recruitment) + recruitment.max <- max(recruitment$predicted_recruitment) + + # calculate & export key quantities + export_kqs(recruitment.units, + recruitment.start.year, + recruitment.end.year, + recruitment.min, + recruitment.max) + + # Add key quantities to captions/alt text + insert_kqs(recruitment.units, + recruitment.start.year, + recruitment.end.year, + recruitment.min, + recruitment.max) + + create_rda( object = final, # get name of function and remove "plot_" from it diff --git a/R/plot_stock_recruitment.R b/R/plot_stock_recruitment.R index ca7eea6d..40195e15 100644 --- a/R/plot_stock_recruitment.R +++ b/R/plot_stock_recruitment.R @@ -137,27 +137,27 @@ plot_stock_recruitment <- function( sr.ssb.units <- spawning_biomass_label sr.ssb.min <- min(sr$spawning_biomass, na.rm = TRUE) sr.ssb.max <- max(sr$spawning_biomass, na.rm = TRUE) - sr.min <- min(sr$predicted_recruitment, na.rm = TRUE) - sr.max <- max(sr$predicted_recruitment, na.rm = TRUE) - sr.units <- recruitment_label + recruitment.min <- min(sr$predicted_recruitment, na.rm = TRUE) + recruitment.max <- max(sr$predicted_recruitment, na.rm = TRUE) + recruitment.units <- recruitment_label # calculate & export key quantities export_kqs(sr.age.min, sr.ssb.units, sr.ssb.min, sr.ssb.max, - sr.min, - sr.max, - sr.units) + recruitment.min, + recruitment.max, + recruitment.units) # Add key quantities to captions/alt text insert_kqs(sr.age.min, sr.ssb.units, sr.ssb.min, sr.ssb.max, - sr.min, - sr.max, - sr.units) + recruitment.min, + recruitment.max, + recruitment.units) create_rda( object = final, diff --git a/R/utils_rda.R b/R/utils_rda.R index 1c0cc488..aff65df9 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -454,19 +454,6 @@ add_more_key_quants <- function( cli::cli_li("sr.ssb.units: {as.character(sr_ssb_units)}") } - if (!is.null(sr_recruitment_units)) { - ### this is for plot_stock_recruitment, since there are two units - #### replace sr.units with sr_recruitment_units - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "sr.units", - as.character(sr_recruitment_units) - )) - - cli::cli_li("sr.units: {as.character(sr_recruitment_units)}") - } - if (!is.null(units)) { ### alt text ### this regex preserves the comma after the units @@ -740,41 +727,14 @@ write_captions <- function(dat, # converted model output object # selectivity.length.min <- # minimum length # selectivity.length.max <- # maximum length - # minimum recruitment : added with add_more_key_quants - - # maximum recruitment: added with add_more_key_quants - - ## recruitment ts - # recruitment units (plural) - numbers of fish, in thousands - # recruitment.units : added with add_more_key_quants - - # start year of recruitment ts plot - recruitment.start.year <- dat |> - dplyr::filter( - label == "recruitment", - module_name == "TIME_SERIES" | module_name == "t.series", - !is.na(year), - is.na(fleet) | length(unique(fleet)) <= 1, - is.na(sex) | length(unique(sex)) <= 1, - is.na(area) | length(unique(area)) <= 1, - is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, - !year %in% year_exclusions - ) |> # SS3 and BAM target module names - dplyr::slice(which.min(year)) |> - dplyr::select(year) |> - as.numeric() - - # end year of recruitment ts plot - # recruitment.end.year : added with add_more_key_quants - # TODO: uncomment and recode once we get clarity about how to extract R0 properly ## relative recruitment # # minimum relative recruitment - # rel.recruitment.min <- (sr.min / R0) |> + # rel.recruitment.min <- (recruitment.min / R0) |> # round(digits = 2) # # # maximum relative recruitment - # rel.recruitment.max <- (sr.max / R0) |> + # rel.recruitment.max <- (recruitment.max / R0) |> # round(digits = 2) ## tot_b (total biomass): same as B plot above @@ -965,9 +925,6 @@ write_captions <- function(dat, # converted model output object # 'rel.recruitment.min' = as.character(rel.recruitment.min), # 'rel.recruitment.max' = as.character(rel.recruitment.max), - ## recruitment ts - "recruitment.start.year" = as.character(recruitment.start.year), - ## spr (spawning potential ratio) "spr.min" = as.character(spr.min), "spr.max" = as.character(spr.max), diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index 717c1eed..f9a80059 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -19,10 +19,10 @@ mod.fit.catch,figure,Observed catch from the data input file (points) and model mod.fit.abun,figure,Assessment model fits to input catch per unit of effort index values over time calculated from fleet.or.survey.name.,"Point and line graph showing the assessment model fit, displayed as a line, to input catch per unit effort index values, displayed as points over time, for fleet.or.survey.name. The x axis shows years, which spans from mod.fit.abun.start.year to mod.fit.abun.end.year . The y axis shows catch per unit effort in cpue.units, which spans from cpue.min to cpue.max." mod.fit.discards,figure,Observed discards (points) and estimated discards (line).,"Point and line graph showing the assessment model fit to observed discards. Observed discards are represented as points while the model fitted estimates are represented as a line over time for fleet.or.survey.name. The x axis shows the year, which spans from mod.fit.discards.start.year to mod.fit.discards.end.year . The y axis shows discards in mod.fit.discards.units, which spans from mod.fit.discards.min to mod.fit.discards.max." selectivity,figure,Length-based selectivity for each fleet and survey estimated by the assessment model.,"Line graph showing length-based selectivity for fleet.or.survey.name from selectivity.start.year to selectivity.end.year . The x axis shows length in selectivity.length.units, which spans from selectivity.length.min to selectivity.length.max. The y axis shows the proportion of the stock that the gear selects for, which spans from 0--1." -stock_recruitment,figure,Stock recruitment relationship estimated by the assessment model. ,"Point and line graph showing the relationship between stock biomass and newly recruited age sr.age.min fish as estimated by the assessment model. Points represent model estimates of recruitment each year as a function of stock biomass, after adjusted by annual recruitment deviations. The line represents the best fit through the points for the stock relationship selected for use in the assessment model. The x axis shows spawning stock biomass in sr.ssb.units, which spans from sr.ssb.min to sr.ssb.max. The y axis shows recruitment in sr.units, which spans from sr.min to sr.max." -recruitment,figure,Estimated recruitment by the assessment model each year in recruitment.units.,"Line graph showing the assessment model estimated recruitment in sr.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year . The y axis shows recruitment in recruitment.units, which spans from sr.min to sr.max." -recruitment.comp,figure,Predicted (black points) and expected (red line) recruitment by the assessment model each year in recruitment.units.,"Scatter plot showing the assessment model estimated recruitment in sr.units for each year with a red line showing the predicted recruitment in sr.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year . The y axis shows recruitment in recruitment.units, which spans from sr.min to sr.max." -relative.recruitment,figure,"Estimated relative recruitment by the assessment model each year in recruitment.units, calculated as R/R~0~ where R~0~ is R0.","Line graph showing the assessment model estimated relative recruitment in sr.units for each year. The x axis shows year, which spans from recruitment.start.year to recruitment.end.year . The y axis shows relative recruitment (R/R~0~), which spans from rel.recruitment.min to rel.recruitment.max recruitment.units ." +stock_recruitment,figure,Stock recruitment relationship estimated by the assessment model. ,"Point and line graph showing the relationship between stock biomass and newly recruited age sr.age.min fish as estimated by the assessment model. Points represent model estimates of recruitment each year as a function of stock biomass, after adjusted by annual recruitment deviations. The line represents the best fit through the points for the stock relationship selected for use in the assessment model. The x axis shows spawning stock biomass in sr.ssb.units, which spans from sr.ssb.min to sr.ssb.max. The y axis shows recruitment in recruitment.units, which spans from recruitment.min to recruitment.max." +recruitment,figure,Estimated recruitment by the assessment model each year in recruitment.units.,"Line graph showing the assessment model estimated recruitment in recruitment.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year . The y axis shows recruitment in recruitment.units, which spans from recruitment.min to recruitment.max." +recruitment.comp,figure,Predicted (black points) and expected (red line) recruitment by the assessment model each year in recruitment.units.,"Scatter plot showing the assessment model estimated recruitment in recruitment.units for each year with a red line showing the predicted recruitment in recruitment.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year . The y axis shows recruitment in recruitment.units, which spans from recruitment.min to recruitment.max." +relative.recruitment,figure,"Estimated relative recruitment by the assessment model each year in recruitment.units, calculated as R/R~0~ where R~0~ is R0.","Line graph showing the assessment model estimated relative recruitment in recruitment.units for each year. The x axis shows year, which spans from recruitment.start.year to recruitment.end.year . The y axis shows relative recruitment (R/R~0~), which spans from rel.recruitment.min to rel.recruitment.max recruitment.units ." recruitment_deviations,figure,Annual deviations (on natural log scale) in the number of newly recruited fish the model estimates each year. ,"Scatterplot showing annual deviations in recruitment. Points have error bars and the dashed horizontal line at 0 represents no deviation from what would be estimated by the stock-recruit relationship. Positive values represent an increase in recruitment that year while negative values represent a decrease. The x axis shows year, which spans from recruit.dev.start.year to recruit.dev.end.year . The y axis shows the recruitment deviation, which spans from recruit.dev.min to recruit.dev.max on a natural log scale." tot.b,figure,Estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing estimated biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows estimated biomass in B.units, which spans from B.min to B.max." spawning_biomass,figure,Estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point (ssb.ref.pt ssb.units).,"Line graph showing estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." diff --git a/inst/resources/key_quantity_template.csv b/inst/resources/key_quantity_template.csv index cf035e83..f3468b7e 100644 --- a/inst/resources/key_quantity_template.csv +++ b/inst/resources/key_quantity_template.csv @@ -84,7 +84,11 @@ recruit.dev.end.year,,,,,, recruit.dev.max,,,,,, recruit.dev.min,,,,,, recruit.dev.start.year,,,,,, +recruitment.end.year,,,,,, +recruitment.max,,,,,, +recruitment.min,,,,,, recruitment.start.year,,,,,, +recruitment.units,,,,,, rel.B.max,,,,,, rel.B.min,,,,,, rel.recruitment.max,,,,,, @@ -98,12 +102,9 @@ spr.max,,,,,, spr.min,,,,,, spr.ref.pt,,,,,, sr.age.min,,,,,, -sr.max,,,,,, -sr.min,,,,,, sr.ssb.max,,,,,, sr.ssb.min,,,,,, sr.ssb.units,,,,,, -sr.units,,,,,, ssb.end.year,,,,,, ssb.max,,,,,, ssb.min,,,,,, @@ -125,4 +126,4 @@ wl.length.min,,,,,, wl.length.units,,,,,, wl.weight.max,,,,,, wl.weight.min,,,,,, -wl.weight.units,,,,,, \ No newline at end of file +wl.weight.units,,,,,, From 9311468c6a7d68dd0c13a0a5d626ac43a9fc6d55 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 19 Mar 2026 14:57:15 -0400 Subject: [PATCH 14/23] update table_landings() to work with new KQ extraction/insertion design --- R/table_landings.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/R/table_landings.R b/R/table_landings.R index f968bc93..40d90a59 100644 --- a/R/table_landings.R +++ b/R/table_landings.R @@ -112,6 +112,16 @@ table_landings <- function( # export figure to rda if argument = T if (make_rda == TRUE) { if (length(df_list) == 1) { + + # Obtain relevant key quantities for captions/alt text + landings.units <- unit_label + + # calculate & export key quantities + export_kqs(landings.units) + + # Add key quantities to captions/alt text + insert_kqs(landings.units) + create_rda( object = final$label, # get name of function and remove "table_" from it From 5d9d79d5cbe445668722f74f8a94f2f5d738fd56 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 19 Mar 2026 17:00:20 -0400 Subject: [PATCH 15/23] Update plot_biomass() and plot_spawning_biomass() so that values are accurate when relative = TRUE; update plot_indices() documentation --- R/plot_biomass.R | 40 ++++--- R/plot_spawning_biomass.R | 35 ++++-- R/utils_rda.R | 100 +++++------------- inst/resources/captions_alt_text_template.csv | 44 ++++---- inst/resources/key_quantity_template.csv | 2 + man/plot_indices.Rd | 2 +- 6 files changed, 105 insertions(+), 118 deletions(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 2212fb40..ae01312b 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -148,30 +148,46 @@ plot_biomass <- function( ### Make RDA ---- if (make_rda) { + + if (relative){ + # pulling out the 2nd df in 'data' works for several datasets + rel.B.min <- ggplot2::ggplot_build(final)@data[[2]] |> + as.data.frame() |> + dplyr::pull(y) |> + min() + rel.B.max <- ggplot2::ggplot_build(final)@data[[2]] |> + as.data.frame() |> + dplyr::pull(y) |> + max() + + # calculate & export key quantities + export_kqs(rel.B.min, rel.B.max) + + # Add key quantities to captions/alt text + insert_kqs(rel.B.min, rel.B.max) + + } else { + B.min <- min(prepared_data$estimate) + B.max <- max(prepared_data$estimate) + + export_kqs(B.min, B.max) + insert_kqs(B.min, B.max) + } - # Obtain relevant key quantities for captions/alt text B.ref.pt <- as.character(ref_line) B.units <- as.character(unit_label) B.start.year <- min(prepared_data$year) - B.end.year <- max(prepared_data$year) - B.min <- min(prepared_data$estimate) - B.max <- max(prepared_data$estimate) + B.end.year <- max(prepared_data$year) - # calculate & export key quantities export_kqs(B.ref.pt, B.units, B.start.year, - B.end.year, - B.min, - B.max) + B.end.year) - # Add key quantities to captions/alt text insert_kqs(B.ref.pt, B.units, B.start.year, - B.end.year, - B.min, - B.max) + B.end.year) create_rda( object = final, diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index eaa4bab2..f7978f3f 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -236,29 +236,48 @@ plot_spawning_biomass <- function( ### Make RDA ---- if (make_rda) { + if (relative){ + # pulling out the 2nd df in 'data' works for several datasets + rel.ssb.min <- ggplot2::ggplot_build(final)@data[[2]] |> + as.data.frame() |> + dplyr::pull(y) |> + min() + rel.ssb.max <- ggplot2::ggplot_build(final)@data[[2]] |> + as.data.frame() |> + dplyr::pull(y) |> + max() + + # calculate & export key quantities + export_kqs(rel.ssb.min, rel.ssb.max) + + # Add key quantities to captions/alt text + insert_kqs(rel.ssb.min, rel.ssb.max) + + } else { + ssb.min <- min(plot_data$estimate) + ssb.max <- max(plot_data$estimate) + + export_kqs(ssb.min, ssb.max) + insert_kqs(ssb.min, ssb.max) + } + # Obtain relevant key quantities for captions/alt text ssb.ref.pt <- as.character(ref_line) ssb.units <- as.character(unit_label) ssb.start.year <- min(plot_data$year) ssb.end.year <- max(plot_data$year) - ssb.min <- min(plot_data$estimate) - ssb.max <- max(plot_data$estimate) # calculate & export key quantities export_kqs(ssb.ref.pt, ssb.units, ssb.start.year, - ssb.end.year, - ssb.min, - ssb.max) + ssb.end.year) # Add key quantities to captions/alt text insert_kqs(ssb.ref.pt, ssb.units, ssb.start.year, - ssb.end.year, - ssb.min, - ssb.max) + ssb.end.year) create_rda( object = final, diff --git a/R/utils_rda.R b/R/utils_rda.R index aff65df9..ce755586 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -65,14 +65,30 @@ insert_kqs <- function(...) { } create_patterns <- function(...) { - # Capture the names from the dots without evaluating them yet + # Capture the names from the ellipsis arg_names <- sapply(rlang::enexprs(...), as.character) # Get the actual values - vals <- list(...) + vals <- as.character(list(...)) - # Combine them into a named character vector - stats::setNames(as.character(vals), arg_names) + # 1. Escape literal dots (e.g., "B.min" -> "B\\.min") + # This ensures the dot is treated as a period, not a "match-anything" wildcard. + escaped_names <- stringr::str_replace_all(arg_names, "\\.", "\\\\.") + + # 2. Wrap in lookarounds to enforce "whole word" logic for dots/alphanumerics + # 2. Refined Lookarounds: + # (? PRECEDER: Not a letter, digit, or dot. + # (?!([a-zA-Z0-9])) -> FOLLOWER: Not a letter or digit. + # We REMOVED the dot from the follower check so "caa.age.max." matches. + patterns <- paste0("(? + # remove the added regex characters + dplyr::mutate( + name = stringr::str_remove_all(name, "^\\(\\?\\ - dplyr::filter(c(grepl(glue::glue("^spawning_biomass_{ref_line}$"), label) | - grepl(glue::glue("^spawning_biomass_msy$"), label))) |> - dplyr::pull(estimate) |> - as.numeric() |> - round(digits = 2) - - if (length(ssbtarg) > 0) { - cli::cli_alert_warning("ssbtarg, rel.ssb.min, and rel.ssb.max were not calculated. Check your ref_line is accurate.", wrap = TRUE) - } else { - # ssb.min and ssb.max needed for rel values below - # relative ssb - ## relative ssb min - rel.ssb.min <- (ssb.min / ssbtarg) |> - round(digits = 2) - - ## relative ssb max - rel.ssb.max <- (ssb.max / ssbtarg) |> - round(digits = 2) - - # replace rel.ssb.min, max placeholders within topic_cap_alt - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "rel.ssb.min", - as.character(rel.ssb.min) - )) |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "rel.ssb.max", - as.character(rel.ssb.max) - )) - - cli::cli_li("rel.ssb.min: {as.character(rel.ssb.min)}") - cli::cli_li("rel.ssb.max: {as.character(rel.ssb.max)}") - } - } - } - ## spawning biomass if (topic_cap_alt$label == "spawning_biomass" | topic_cap_alt$label == "stock_recruitment") { if (is.null(dat)) { @@ -615,17 +588,6 @@ write_captions <- function(dat, # converted model output object # Bmsy <- - - # TODO: uncomment and recode once we get clarity about how to extract Btarg properly - ## relative B - # relative B min - # rel.B.min <- (B.min / Btarg) |> - # round(digits = 2) - # - # # relative B max - # rel.B.max <- (B.max / Btarg) |> - # round(digits = 2) - ## vonB LAA (von Bertalanffy growth function + length at age)- don't code quantities yet # vonb.age.min <- # minimum vonB age # vonb.age.max <- # maximum vonB age @@ -741,14 +703,6 @@ write_captions <- function(dat, # converted model output object # ssbtarg : added with add_more_key_quants - ## relative ssb - # relative ssb min - # rel.ssb.min : added with add_more_key_quants - - # relative ssb max - # rel.ssb.max : added with add_more_key_quants - - ## spr (spawning potential ratio) # minimum spr spr.min <- dat |> @@ -839,10 +793,6 @@ write_captions <- function(dat, # converted model output object # 'overfished.status.is.isnot' = as.character(overfished.status.is.isnot), # 'overfishing.status.is.isnot' = as.character(overfishing.status.is.isnot), - ## Relative biomass plot - # NOTE: moving this above biomass so rel.B.min isn't changed to "rel." + B.min (etc.) - # 'rel.B.min' = as.character(rel.B.min), - # 'rel.B.max' = as.character(rel.B.max), ## Biomass plot # 'R0' = as.character(R0), diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index f9a80059..11f564a4 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -1,10 +1,10 @@ label,type,caption,alt_text kobe,figure,"Kobe plot showing stock status. Triangles delineate start and end years. Horizontal and vertical dashed lines delineate the proportion of F/F~MSY~ where F~MSY~ = B.ref.pt, and B/B~MSY~ where B~MSY~ = B.ref.pt. Overfishing is occurring when F/F~MSY~ > 1 and an overfished status is indicated where B/B~MSY~ > 1.","A Kobe plot showing stock status. The x axis shows overfished status (i.e., B/B~MSY~) and the y axis shows overfishing (i.e., F/F~MSY~). The B/B~MSY~ and F/F~MSY~ for kobe.end.year were B.BMSY.end.yr and F.FMSY.end.yr, respectively, indicating that the stock overfished.status.is.isnot overfished and overfishing.status.is.is.not experiencing overfishing." -biomass,figure,Biomass (B) time series. The horizontal dashed line represents the limit reference point (B.ref.pt B.units).,"Line graph showing biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows biomass in B.units, which spans from B.min to B.max." -relative_biomass,figure,Relative biomass (B) time series. The horizontal dashed line represents the limit reference point calculated as B/B(reference point) (B.ref.point B.units).,"Line graph showing relative biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows relative biomass (B/B~target~), which spans from rel.B.min to rel.B.max B.units ." -fishing_mortality,figure,Fishing mortality (F) over time. The horizontal dashed line represents the target reference point (F.ref.pt).,"Line graph showing fishing mortality over time . The x axis shows the year, which spans from F.start.year to F.end.year . The y axis shows fishing mortality, which spans from F.min to F.max." -relative_fishing_mortality,figure,Relative fishing mortality (F) over time. The horizontal dashed line represents the target reference point (F.ref.pt).,"Line graph showing fishing mortality over time . The x axis shows the year, which spans from F.start.year to F.end.year . The y axis shows relative fishing mortality (F/F~target~), which spans from rel.F.min to rel.F.max." -landings,figure,Historical landings by fleet.,"Cumulative area plot showing historical landings. The x axis shows the year, which spans from landings.start.year to landings.end.year . The y axis shows landings in landings.units, which spans from landings.min to landings.max." +biomass,figure,Biomass (B) time series. The horizontal dashed line represents the limit reference point (B.ref.pt B.units).,"Line graph showing biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year. The y axis shows biomass in B.units, which spans from B.min to B.max." +relative_biomass,figure,Relative biomass (B) time series. The horizontal dashed line represents the limit reference point calculated as B/B(reference point) (B.ref.point B.units).,"Line graph showing relative biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year. The y axis shows relative biomass (B/B~target~), which spans from rel.B.min to rel.B.max B.units." +fishing_mortality,figure,Fishing mortality (F) over time. The horizontal dashed line represents the target reference point (F.ref.pt).,"Line graph showing fishing mortality over time. The x axis shows the year, which spans from F.start.year to F.end.year. The y axis shows fishing mortality, which spans from F.min to F.max." +relative_fishing_mortality,figure,Relative fishing mortality (F) over time. The horizontal dashed line represents the target reference point (F.ref.pt).,"Line graph showing fishing mortality over time. The x axis shows the year, which spans from F.start.year to F.end.year. The y axis shows relative fishing mortality (F/F~target~), which spans from rel.F.min to rel.F.max." +landings,figure,Historical landings by fleet.,"Cumulative area plot showing historical landings. The x axis shows the year, which spans from landings.start.year to landings.end.year. The y axis shows landings in landings.units, which spans from landings.min to landings.max." natural_mortality,figure,Natural mortality (M) for each age. ,"Line graph showing natural mortality. The x axis shows age in years, which spans from M.age.min to M.age.max. The y axis shows the natural mortality rate per year, which spans from M.rate.min to M.rate.max." vonb.laa,figure,Aged and measured fish (points) and von Bertalanffy growth function (line) fit to the data.,"Point and line graph showing observations of measured age and length data as points and the von Bertalanffy relationship fit to this data as a line with 95\\% confidence intervals. The x axis shows age in years, which spans from vonb.age.min to vonb.age.max. The y axis shows length in vonb.length.units, which spans from vonb.length.min to vonb.length.max." length.type.conversion,figure,Length-type conversion relationship between total and fork length where points represent individual fish length measurement observations and the line is the fitted relationship.,"Point and line graph showing observations of measured fork length and measured total length from the same fish for conversion purposes as points, and a linear fit through those points as a line. The x axis shows total length in total.length.units, which spans from total.length.min to total.length.max. The y axis shows fork length in fork.length.units, which spans from fork.length.min to fork.length.max." @@ -13,23 +13,23 @@ maturity.schedule,figure,The relationship between the proportion mature and fish fecundity.at.length,figure,Fecundity (in number of eggs) as a function of fish length. ,"Point and line graph showing observations of measured fecundity and measured length from the same fish and an exponential fit through those points displayed as a line. The x axis shows length in fecundity.length.units, which spans from fecundity.length.min to fecundity.length.max. The y axis shows fecundity in fecundity.units, which spans from fecundity.min to fecundity.max." catch,figure,"Historical catch for each year in which there is composition data, for each fleet or survey.","Cumulative area plot showing the catch for each year and fleet or survey. The x axis shows the year, which spans from catch.year.min to catch.year.max. The y axis shows the proportion of the total catch by each fleet or survey and spans from tot.catch.min to tot.catch.max." CAL,figure,"Histograms showing the proportion of the catch in each length group, each year for which there is composition data, for fleet or survey fleet.or.survey.name.","Histograms for each year and fleet for which there is data (one per year grouped by fleet or survey), which show the proportion of the catch in each length group. The x axis of each histogram contains length intervals, which span from cal.length.min to cal.length.max. The y axis of each histogram contains the proportion of the catch and spans from 0--1." -indices,figure,Catch per unit effort (CPUE) over time for fleet or survey. 95\\% confidence intervals are shown for each survey/fleet.,"Line graph showing catch per unit effort (CPUE) over time, stratified by survey/fleet. The x axis shows the year, which spans from cpue.start.year to cpue.end.year . The y axis shows CPUE in cpue.units, which spans from cpue.min to cpue.max." -abundance_at_age,figure,Model estimate of population numbers at age over time. The relative size of each bubble for a given year and age indicates the relative abundance in that category compared with others. ,"Bubble plot showing relative age proportions over time. The x axis shows the year, which spans from pop.naa.start.year to pop.naa.end.year . The y axis shows age in years, which spans from pop.naa.age.min to pop.naa.age.max. The size of the bubbles range from pop.naa.fish.min to pop.naa.fish.max." -mod.fit.catch,figure,Observed catch from the data input file (points) and model estimated catch (line) over time for fleet.or.survey.name. ,"Point and line graph showing observed catch from the data input file as points and model estimated catch as a line over time for fleet.or.survey.name. The x axis shows years, which spans from mod.fit.catch.start.year to mod.fit.catch.end.year . The y axis shows catch in mod.fit.catch.units, which spans from mod.fit.catch.min to mod.fit.catch.max." -mod.fit.abun,figure,Assessment model fits to input catch per unit of effort index values over time calculated from fleet.or.survey.name.,"Point and line graph showing the assessment model fit, displayed as a line, to input catch per unit effort index values, displayed as points over time, for fleet.or.survey.name. The x axis shows years, which spans from mod.fit.abun.start.year to mod.fit.abun.end.year . The y axis shows catch per unit effort in cpue.units, which spans from cpue.min to cpue.max." -mod.fit.discards,figure,Observed discards (points) and estimated discards (line).,"Point and line graph showing the assessment model fit to observed discards. Observed discards are represented as points while the model fitted estimates are represented as a line over time for fleet.or.survey.name. The x axis shows the year, which spans from mod.fit.discards.start.year to mod.fit.discards.end.year . The y axis shows discards in mod.fit.discards.units, which spans from mod.fit.discards.min to mod.fit.discards.max." -selectivity,figure,Length-based selectivity for each fleet and survey estimated by the assessment model.,"Line graph showing length-based selectivity for fleet.or.survey.name from selectivity.start.year to selectivity.end.year . The x axis shows length in selectivity.length.units, which spans from selectivity.length.min to selectivity.length.max. The y axis shows the proportion of the stock that the gear selects for, which spans from 0--1." +indices,figure,Catch per unit effort (CPUE) over time for fleet or survey. 95\\% confidence intervals are shown for each survey/fleet.,"Line graph showing catch per unit effort (CPUE) over time, stratified by survey/fleet. The x axis shows the year, which spans from cpue.start.year to cpue.end.year. The y axis shows CPUE in cpue.units, which spans from cpue.min to cpue.max." +abundance_at_age,figure,Model estimate of population numbers at age over time. The relative size of each bubble for a given year and age indicates the relative abundance in that category compared with others. ,"Bubble plot showing relative age proportions over time. The x axis shows the year, which spans from pop.naa.start.year to pop.naa.end.year. The y axis shows age in years, which spans from pop.naa.age.min to pop.naa.age.max. The size of the bubbles range from pop.naa.fish.min to pop.naa.fish.max." +mod.fit.catch,figure,Observed catch from the data input file (points) and model estimated catch (line) over time for fleet.or.survey.name. ,"Point and line graph showing observed catch from the data input file as points and model estimated catch as a line over time for fleet.or.survey.name. The x axis shows years, which spans from mod.fit.catch.start.year to mod.fit.catch.end.year. The y axis shows catch in mod.fit.catch.units, which spans from mod.fit.catch.min to mod.fit.catch.max." +mod.fit.abun,figure,Assessment model fits to input catch per unit of effort index values over time calculated from fleet.or.survey.name.,"Point and line graph showing the assessment model fit, displayed as a line, to input catch per unit effort index values, displayed as points over time, for fleet.or.survey.name. The x axis shows years, which spans from mod.fit.abun.start.year to mod.fit.abun.end.year. The y axis shows catch per unit effort in cpue.units, which spans from cpue.min to cpue.max." +mod.fit.discards,figure,Observed discards (points) and estimated discards (line).,"Point and line graph showing the assessment model fit to observed discards. Observed discards are represented as points while the model fitted estimates are represented as a line over time for fleet.or.survey.name. The x axis shows the year, which spans from mod.fit.discards.start.year to mod.fit.discards.end.year. The y axis shows discards in mod.fit.discards.units, which spans from mod.fit.discards.min to mod.fit.discards.max." +selectivity,figure,Length-based selectivity for each fleet and survey estimated by the assessment model.,"Line graph showing length-based selectivity for fleet.or.survey.name from selectivity.start.year to selectivity.end.year. The x axis shows length in selectivity.length.units, which spans from selectivity.length.min to selectivity.length.max. The y axis shows the proportion of the stock that the gear selects for, which spans from 0--1." stock_recruitment,figure,Stock recruitment relationship estimated by the assessment model. ,"Point and line graph showing the relationship between stock biomass and newly recruited age sr.age.min fish as estimated by the assessment model. Points represent model estimates of recruitment each year as a function of stock biomass, after adjusted by annual recruitment deviations. The line represents the best fit through the points for the stock relationship selected for use in the assessment model. The x axis shows spawning stock biomass in sr.ssb.units, which spans from sr.ssb.min to sr.ssb.max. The y axis shows recruitment in recruitment.units, which spans from recruitment.min to recruitment.max." -recruitment,figure,Estimated recruitment by the assessment model each year in recruitment.units.,"Line graph showing the assessment model estimated recruitment in recruitment.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year . The y axis shows recruitment in recruitment.units, which spans from recruitment.min to recruitment.max." -recruitment.comp,figure,Predicted (black points) and expected (red line) recruitment by the assessment model each year in recruitment.units.,"Scatter plot showing the assessment model estimated recruitment in recruitment.units for each year with a red line showing the predicted recruitment in recruitment.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year . The y axis shows recruitment in recruitment.units, which spans from recruitment.min to recruitment.max." -relative.recruitment,figure,"Estimated relative recruitment by the assessment model each year in recruitment.units, calculated as R/R~0~ where R~0~ is R0.","Line graph showing the assessment model estimated relative recruitment in recruitment.units for each year. The x axis shows year, which spans from recruitment.start.year to recruitment.end.year . The y axis shows relative recruitment (R/R~0~), which spans from rel.recruitment.min to rel.recruitment.max recruitment.units ." -recruitment_deviations,figure,Annual deviations (on natural log scale) in the number of newly recruited fish the model estimates each year. ,"Scatterplot showing annual deviations in recruitment. Points have error bars and the dashed horizontal line at 0 represents no deviation from what would be estimated by the stock-recruit relationship. Positive values represent an increase in recruitment that year while negative values represent a decrease. The x axis shows year, which spans from recruit.dev.start.year to recruit.dev.end.year . The y axis shows the recruitment deviation, which spans from recruit.dev.min to recruit.dev.max on a natural log scale." -tot.b,figure,Estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing estimated biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows estimated biomass in B.units, which spans from B.min to B.max." -spawning_biomass,figure,Estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point (ssb.ref.pt ssb.units).,"Line graph showing estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." -relative.spawning.biomass,figure,Estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point) (ssb.ref.pt ssb.units).,"Line graph showing estimated relative spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max ssb.units." -spr,figure,Estimated spawning potential ratio (SPR) (SSB~current~/SSB~target~) time series. The horizontal dashed line represents the spawning potential ratio of the limit reference point at spr.ref.pt.,"Line graph showing estimated spawning potential ratio over time. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." -biomass_at_age,figure,Estimated population numbers at age and population biomass at age over time. The relative size of each bubble for a given year and age indicates the relative abundance or biomass in that category compared with others. ,"Bubble plot showing estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.baa.start.year to pop.baa.end.year . The y axis shows age, which spans from pop.baa.age.min to pop.baa.age.max. The size of the bubbles range from pop.baa.fish.min to pop.baa.fish.max." -proj.catch,figure,Forecasted catch in proj.catch.units over future years for different fishing mortality scenarios as indicated in the legend.,"Time series line graph showing forecasted catch over future years for different fishing mortality scenarios. The x axis shows the year, which spans from proj.catch.start.year to proj.catch.end.year . The y axis shows catch in proj.catch.units, which spans from proj.catch.min to proj.catch.max." +recruitment,figure,Estimated recruitment by the assessment model each year in recruitment.units.,"Line graph showing the assessment model estimated recruitment in recruitment.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year. The y axis shows recruitment in recruitment.units, which spans from recruitment.min to recruitment.max." +recruitment.comp,figure,Predicted (black points) and expected (red line) recruitment by the assessment model each year in recruitment.units.,"Scatter plot showing the assessment model estimated recruitment in recruitment.units for each year with a red line showing the predicted recruitment in recruitment.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year. The y axis shows recruitment in recruitment.units, which spans from recruitment.min to recruitment.max." +relative.recruitment,figure,"Estimated relative recruitment by the assessment model each year in recruitment.units, calculated as R/R~0~ where R~0~ is R0.","Line graph showing the assessment model estimated relative recruitment in recruitment.units for each year. The x axis shows year, which spans from recruitment.start.year to recruitment.end.year. The y axis shows relative recruitment (R/R~0~), which spans from rel.recruitment.min to rel.recruitment.max recruitment.units." +recruitment_deviations,figure,Annual deviations (on natural log scale) in the number of newly recruited fish the model estimates each year. ,"Scatterplot showing annual deviations in recruitment. Points have error bars and the dashed horizontal line at 0 represents no deviation from what would be estimated by the stock-recruit relationship. Positive values represent an increase in recruitment that year while negative values represent a decrease. The x axis shows year, which spans from recruit.dev.start.year to recruit.dev.end.year. The y axis shows the recruitment deviation, which spans from recruit.dev.min to recruit.dev.max on a natural log scale." +tot.b,figure,Estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing estimated biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year. The y axis shows estimated biomass in B.units, which spans from B.min to B.max." +spawning_biomass,figure,Estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point (ssb.ref.pt ssb.units).,"Line graph showing estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year. The y axis shows estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." +relative.spawning.biomass,figure,Estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point) (ssb.ref.pt ssb.units).,"Line graph showing estimated relative spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year. The y axis shows estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max ssb.units." +spr,figure,Estimated spawning potential ratio (SPR) (SSB~current~/SSB~target~) time series. The horizontal dashed line represents the spawning potential ratio of the limit reference point at spr.ref.pt.,"Line graph showing estimated spawning potential ratio over time. The x axis shows the year, which spans from ssb.start.year to ssb.end.year. The y axis shows estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." +biomass_at_age,figure,Estimated population numbers at age and population biomass at age over time. The relative size of each bubble for a given year and age indicates the relative abundance or biomass in that category compared with others. ,"Bubble plot showing estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.baa.start.year to pop.baa.end.year. The y axis shows age, which spans from pop.baa.age.min to pop.baa.age.max. The size of the bubbles range from pop.baa.fish.min to pop.baa.fish.max." +proj.catch,figure,Forecasted catch in proj.catch.units over future years for different fishing mortality scenarios as indicated in the legend.,"Time series line graph showing forecasted catch over future years for different fishing mortality scenarios. The x axis shows the year, which spans from proj.catch.start.year to proj.catch.end.year. The y axis shows catch in proj.catch.units, which spans from proj.catch.min to proj.catch.max." proj.biomass,table,Forecasted biomass over future years for different fishing mortality scenarios., report.version.model.changes,table,Document version history briefly describing when major changes or updates are made to each version of the report., projection.ts,table,Forecasted catch and biomass over future years for different fishing mortality scenarios. , @@ -47,4 +47,4 @@ bnc,table,"Historical biomass, spawning biomass, abundance, and catch time ser naa,table,Historical population abundance (numbers) estimated by the base model for each age group., catchability,table,Catchability over time for fleet catchability.fleet., sensitivity.runs,table,"Forecasted catch, biomass, spawning biomass, recruitment, fishing mortality, and stock status as projected from the base model configuration.", -catch_comp,figure,Fishery age composition (caa.start.year-caa.end.year). The area of the circle is proportional to the catch. Diagonal lines indicated the top 5\\% strongest year classes.,"Bubble plot showing estimated total catch at age. The x axis shows the year, which spans from caa.start.year to caa.end.year . The y axis shows age, which spans from caa.age.min to caa.age.max. The size of the bubbles range from tot.catch.min to tot.catch.max." +catch_comp,figure,Fishery age composition (caa.start.year-caa.end.year). The area of the circle is proportional to the catch. Diagonal lines indicated the top 5\\% strongest year classes.,"Bubble plot showing estimated total catch at age. The x axis shows the year, which spans from caa.start.year to caa.end.year. The y axis shows age, which spans from caa.age.min to caa.age.max. The size of the bubbles range from tot.catch.min to tot.catch.max." diff --git a/inst/resources/key_quantity_template.csv b/inst/resources/key_quantity_template.csv index f3468b7e..9fea1ebd 100644 --- a/inst/resources/key_quantity_template.csv +++ b/inst/resources/key_quantity_template.csv @@ -93,6 +93,8 @@ rel.B.max,,,,,, rel.B.min,,,,,, rel.recruitment.max,,,,,, rel.recruitment.min,,,,,, +rel.ssb.max,,,,,, +rel.ssb.min,,,,,, selectivity.end.year,,,,,, selectivity.length.max,,,,,, selectivity.length.min,,,,,, diff --git a/man/plot_indices.Rd b/man/plot_indices.Rd index aa749285..15790df6 100644 --- a/man/plot_indices.Rd +++ b/man/plot_indices.Rd @@ -13,7 +13,7 @@ plot_indices( module = NULL, focus = NULL, make_rda = FALSE, - figures_dir = NULL, + figures_dir = getwd(), ... ) } From 34e6c6c6bc0bde3d8e47838f8f96e76f840eca3d Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 20 Mar 2026 10:14:50 -0400 Subject: [PATCH 16/23] Update plot_fishing_mortality() so that values are accurate when relative = TRUE --- R/plot_fishing_mortality.R | 37 +++++++++++++++++------- inst/resources/key_quantity_template.csv | 2 ++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/R/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index 4ffa7542..0d41262d 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -96,26 +96,43 @@ plot_fishing_mortality <- function( ### Make RDA ---- if (make_rda) { + if (relative){ # Obtain relevant key quantities for captions/alt text + # pulling out the 2nd df in 'data' works for several datasets + rel.F.min <- ggplot2::ggplot_build(final)@data[[2]] |> + as.data.frame() |> + dplyr::pull(y) |> + min() + rel.F.max <- ggplot2::ggplot_build(final)@data[[2]] |> + as.data.frame() |> + dplyr::pull(y) |> + max() + + # calculate & export key quantities + export_kqs(rel.F.min, rel.F.max) + + # Add key quantities to captions/alt text + insert_kqs(rel.F.min, rel.F.max) + + } else { + F.min <- min(prepared_data$estimate) + F.max <- max(prepared_data$estimate) + + export_kqs(F.min, F.max) + insert_kqs(F.min, F.max) + } + F.ref.pt <- as.character(ref_line) F.start.year <- min(prepared_data$year) F.end.year <- max(prepared_data$year) - F.min <- min(prepared_data$estimate) - F.max <- max(prepared_data$estimate) - # calculate & export key quantities export_kqs(F.ref.pt, F.start.year, - F.end.year, - F.min, - F.max) + F.end.year) - # Add key quantities to captions/alt text insert_kqs(F.ref.pt, F.start.year, - F.end.year, - F.min, - F.max) + F.end.year) create_rda( object = final, diff --git a/inst/resources/key_quantity_template.csv b/inst/resources/key_quantity_template.csv index 9fea1ebd..e2f545bd 100644 --- a/inst/resources/key_quantity_template.csv +++ b/inst/resources/key_quantity_template.csv @@ -91,6 +91,8 @@ recruitment.start.year,,,,,, recruitment.units,,,,,, rel.B.max,,,,,, rel.B.min,,,,,, +rel.F.max,,,,,, +rel.F.min,,,,,, rel.recruitment.max,,,,,, rel.recruitment.min,,,,,, rel.ssb.max,,,,,, From 918de27c36c6abb26bca819f4ffb4d435f188476 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 20 Mar 2026 10:38:39 -0400 Subject: [PATCH 17/23] Remove write_captions() and add_more_key_quants() functions and update documentation --- NAMESPACE | 1 - R/plot_natural_mortality.R | 5 - R/table_afsc_tier.R | 20 +- R/utils_rda.R | 611 +++---------------------------------- man/write_captions.Rd | 38 --- 5 files changed, 59 insertions(+), 616 deletions(-) delete mode 100644 man/write_captions.Rd diff --git a/NAMESPACE b/NAMESPACE index 4b9e32c5..c81267fd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -28,4 +28,3 @@ export(reference_line) export(save_all_plots) export(table_landings) export(theme_noaa) -export(write_captions) diff --git a/R/plot_natural_mortality.R b/R/plot_natural_mortality.R index 42483084..aac0745f 100644 --- a/R/plot_natural_mortality.R +++ b/R/plot_natural_mortality.R @@ -23,11 +23,6 @@ plot_natural_mortality <- function( figures_dir = getwd(), ... ) { - # TODO: - # -update M.rate.min, max in write_captions once prev point done - # -Make test - # -add to exp_all_figs_tables - # Extract natural mortality # if (is.null(group)) group <- "age" diff --git a/R/table_afsc_tier.R b/R/table_afsc_tier.R index bb036a6f..6ed6cd39 100644 --- a/R/table_afsc_tier.R +++ b/R/table_afsc_tier.R @@ -11,16 +11,16 @@ table_afsc_tier <- function() { fig_or_table <- "table" # run write_captions.R if its output doesn't exist - if (!file.exists( - fs::path(getwd(), "captions_alt_text.csv") - ) - ) { - stockplotr::write_captions( - dat = dat, - dir = tables_dir, - year = NULL - ) - } + # if (!file.exists( + # fs::path(getwd(), "captions_alt_text.csv") + # ) + # ) { + # stockplotr::write_captions( + # dat = dat, + # dir = tables_dir, + # year = NULL + # ) + # } level <- c( "Level 1: Normal", "Level 2: Substantially Increased Concerns", diff --git a/R/utils_rda.R b/R/utils_rda.R index ce755586..2c4e097e 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -211,333 +211,10 @@ create_rda <- function( } #------------------------------------------------------------------------------ - -# substitute in more key quantities (units, end_years, reference points, and more) -# to captions/alt text -add_more_key_quants <- function( - dat, - topic, - fig_or_table, - dir = getwd(), - end_year = NULL, - units = NULL, - sr_ssb_units = NULL, - sr_recruitment_units = NULL, - ref_line = NULL, - ref_pt = NULL, - scaling = 1 -) { - # import csv - caps_alt_df <- utils::read.csv(fs::path(dir, "captions_alt_text.csv")) - - # make year character if not null - if (!is.null(end_year)) { - end_year <- as.character(end_year) - } else { - end_year <- format(Sys.Date(), "%Y") - } - - # select specific fig/table's caption/alt text - topic_cap_alt <- caps_alt_df |> - dplyr::filter( - label == topic, - type == fig_or_table - ) - - if (!is.null(dat)) { - dat <- dat |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year), - age = as.numeric(age) - ) - } - - cli::cli_h3("Key quantities extracted and inserted from add_more_key_quants():") - - # calculate key quantities that rely on scaling for calculation - # TODO: pull the relative forms of these three KQs (B, R, SSB) from write_captions, - # write analogous code for each in this section, and remove placeholders from - # the end of write_captions (once we get clarity about how to extract Btarg, - # ssbtarg, and R0) - # - - # TODO: When adding code extracting values for landings and/or indices figures/tables, be aware that both figure/table share the same label ("landings" and "indices"). If there's reason, add in extra conditional to check if value is from a figure or table - - ## spawning biomass - if (topic_cap_alt$label == "spawning_biomass" | topic_cap_alt$label == "stock_recruitment") { - if (is.null(dat)) { - cli::cli_alert_warning("Some key quantities associated with spawning biomass were not extracted and added to captions_alt_text.csv due to missing data file (i.e., 'dat' argument).", wrap = TRUE) - } else { - # minimum ssb - sr.ssb.min <- dat |> - dplyr::filter( - label == "spawning_biomass", - module_name == "TIME_SERIES" | module_name == "t.series", - !is.na(year), - is.na(fleet) | length(unique(fleet)) <= 1, - is.na(sex) | length(unique(sex)) <= 1, - is.na(area) | length(unique(area)) <= 1, - is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, - !year %in% year_exclusions - ) |> # SS3 and BAM target module names - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - dplyr::mutate(estimate = estimate / scaling) |> - as.numeric() |> - round(digits = 2) - - # maximum ssb - sr.ssb.max <- dat |> - dplyr::filter( - label == "spawning_biomass", - module_name == "TIME_SERIES" | module_name == "t.series", - !is.na(year), - is.na(fleet) | length(unique(fleet)) <= 1, - is.na(sex) | length(unique(sex)) <= 1, - is.na(area) | length(unique(area)) <= 1, - is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, - !year %in% year_exclusions - ) |> # SS3 and BAM target module names - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - dplyr::mutate(estimate = estimate / scaling) |> - as.numeric() |> - round(digits = 2) - - # ssbtarg - ssbtarg <- dat |> - dplyr::filter(c(grepl(glue::glue("^spawning_biomass_{ref_line}$"), label) | - grepl(glue::glue("^spawning_biomass_msy$"), label))) |> - dplyr::pull(estimate) |> - as.numeric() |> - round(digits = 2) - - # ssb.min and ssb.max needed for rel values below - # replace sr.ssb.min, sr.ssb.max, ssbtarg, ssb.min, and ssb.max placeholders - # within topic_cap_alt - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "sr.ssb.min", - as.character(sr.ssb.min) - )) |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "sr.ssb.max", - as.character(sr.ssb.max) - )) - - cli::cli_li("sr.ssb.min: {as.character(sr.ssb.min)}") - cli::cli_li("sr.ssb.max: {as.character(sr.ssb.max)}") - } - } - - # replace placeholders (e.g., if "end.year" is found in topic_alt, replace it with end_year) - ## end_year----- - if (!is.null(end_year)) { - ### alt text - ### this regex preserves the comma after the end year - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - stringr::regex("(\\S*end\\.year\\S*)(?=\\s?,)"), - end_year - )) - - ### this regex removes a potential trailing space after the end year - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - stringr::regex("\\S*end\\.year\\S*\\s*"), - end_year - )) - - cli::cli_li("end_year: {as.character(end_year)}") - } - ## units----- - - if (is.null(scaling)) { - scale_label <- FALSE - } else { - scale_label <- TRUE - magnitude <- floor(log10(scaling)) - if (magnitude == 0) { - units <- units - unit_mag <- "" - } else if (magnitude > 0 & magnitude < 10) { - scale_unit <- c( - "tens of ", - "hundreds of ", - "thousands of", - "tens of thousands of ", - "hundreds of thousands of ", - "millions of ", - "tens of millions of ", - "hundreds of millions of ", - "billions of " - ) - unit_mag <- paste(scale_unit[magnitude]) - } else { - cli::cli_abort("Scaling out of bounds. Please choose a value ranging from 1-1000000000 (one billion) in orders of magnitude (e.g., 1, 10, 100, 1000, etc.)", wrap = TRUE) - } - } - - - if (!is.null(units)) { - ### caption - ### this regex preserves the closing ) after the units - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(caption = stringr::str_replace_all( - caption, - stringr::regex("(\\S*units\\S*)(?=\\s?\\))"), - as.character(units) - )) - - ### this regex preserves the period after the units - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(caption = stringr::str_replace_all( - caption, - stringr::regex("(\\S*units\\S*)(?=\\s?.)"), - as.character(units) - )) - - ### this regex replaces the units if it's not found with the previous two commands - ### (i.e., there's no parenthesis or period adjacent to the units variable) - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(caption = stringr::str_replace_all( - caption, - stringr::regex("\\S*units\\S*"), - as.character(units) - )) - - cli::cli_li("units: {as.character(units)}") - } - - if (!is.null(sr_ssb_units)) { - ### this is for plot_stock_recruitment, since there are two units - #### replace sr.ssb.units with sr_ssb_units - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - "sr.ssb.units", - as.character(sr_ssb_units) - )) - - cli::cli_li("sr.ssb.units: {as.character(sr_ssb_units)}") - } - - if (!is.null(units)) { - ### alt text - ### this regex preserves the comma after the units - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - stringr::regex("(\\S*units\\S*)(?=\\s?,)"), - ifelse(scale_label, - paste0(unit_mag, as.character(units)), - as.character(units) - ) - )) - - ### this regex replaces the units if it's not found with the previous command - ### (i.e., there's no comma adjacent to the units variable) - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(alt_text = stringr::str_replace_all( - alt_text, - stringr::regex("\\S*units\\S*"), - ifelse(scale_label, - paste0(unit_mag, as.character(units)), - as.character(units) - ) - )) - } - ## reference points----- - if (!is.null(ref_pt)) { - ### caption - ### this regex preserves the opening ( before the ref pt - topic_cap_alt <- topic_cap_alt |> - dplyr::mutate(caption = stringr::str_replace_all( - caption, - stringr::regex("\\(\\S*ref\\.pt*\\S*"), - paste0("(", as.character(ref_pt)) - )) - - cli::cli_li("plot-specific reference point: {as.character(ref_pt)}") - } - - # remove row with old caption/alt text, then add new row - replaced_df <- dplyr::anti_join(caps_alt_df, - topic_cap_alt, - by = c("label", "type") - ) |> - dplyr::full_join(topic_cap_alt) - - # export df with updated captions and alt text to csv - utils::write.csv( - x = replaced_df, - file = fs::path( - dir, - "captions_alt_text.csv" - ), - row.names = FALSE - ) -} - -#------------------------------------------------------------------------------ - -#' Write captions and alternative text -#' -#' Function to create captions and alternative text that contain -#' key quantities from the model results file. -#' -#' @inheritParams plot_spawning_biomass -#' @param dir Directory where the output captions and alt text file should be -#' saved. Defaults to working directory. -#' @param year the last year of the data or the current year this function is -#' being performed. Defaults to the current year. -#' -#' @return Exports .csv with captions and alt text for figures and tables -#' that contain key quantities (e.g., an assessment's start year) that -#' are automatically extracted from the converted model results file. -#' -#' @export -#' -#' @examples -#' \dontrun{ -#' write_captions(dat, -#' dir = getwd(), -#' year = 2025 -#' ) -#' } -write_captions <- function(dat, # converted model output object - dir = getwd(), - year = format(Sys.Date(), "%Y")) { - # only extract key quantities/export new csv if not present - if (file.exists(fs::path(dir, "captions_alt_text.csv"))) { - cli::cli_alert_danger("Captions and alternative text file (captions_alt_text.csv) already exists; write_captions() will not run.", wrap = TRUE) - cli::cli_alert_info("To extract new key quantities and make a new captions_alt_text.csv file, delete existing captions_alt_text.csv and rerun write_captions().", wrap = TRUE) - } else { - # import pre-written captions and alt text that include placeholders - # for key quantities (e.g., 'start_year' is the placeholder for the - # assessment's start year) - caps_alttext <- utils::read.csv( - system.file("resources", "captions_alt_text_template.csv", package = "stockplotr") - ) - - dat <- dat |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year), - age = as.numeric(age) - ) - - # extract key quantities - # REMINDERS: - # -the variable names must exactly match those in the captions/alt text csv. - - # suppress warnings - options(warn = -1) +# NOTE: this code was taken from the former write_captions() function +# Leaving it here to pull from as we develop more figure and table functions +# which will have associated captions/alt text containing key quantities +# with the following object names and calculations # FIGURES----- @@ -593,7 +270,7 @@ write_captions <- function(dat, # converted model output object # vonb.age.max <- # maximum vonB age # vonB length units (plural) - # vonb.length.units : added with add_more_key_quants + # vonb.length.units # vonb.length.min <- # minimum vonB length # vonb.length.max <- # minimum vonB length @@ -601,12 +278,12 @@ write_captions <- function(dat, # converted model output object ## length-type conversion plot- don't code quantities yet # total length units (plural) - # total.length.units : added with add_more_key_quants + # total.length.units # total.length.min <- # minimum total length # total.length.max <- # maximum total length # fork length units (plural) - # fork.length.units : added with add_more_key_quants + # fork.length.units # fork.length.min <- # minimum fork length # fork.length.max <- # maximum fork length @@ -614,13 +291,13 @@ write_captions <- function(dat, # converted model output object ## weight-length conversion plot- don't code quantities yet # length units (plural) - # wl.length.units : added with add_more_key_quants + # wl.length.units # wl.length.min <- # minimum length # wl.length.max <- # maximum length # weight units (plural) - # wl.weight.units : added with add_more_key_quants + # wl.weight.units # wl.weight.min <- # minimum weight # wl.weight.max <- # maximum weight @@ -628,7 +305,7 @@ write_captions <- function(dat, # converted model output object ## maturity schedule (proportion mature)- don't code quantities yet # length units (plural) - # prop.mat.length.units : added with add_more_key_quants + # prop.mat.length.units # prop.mat.length.min <- # minimum length # prop.mat.length.max <- # maximum length @@ -636,13 +313,13 @@ write_captions <- function(dat, # converted model output object ## fecundity at length- don't code quantities yet # length units (plural) - # fecundity.length.units : added with add_more_key_quants + # fecundity.length.units # fecundity.length.min <- # minimum length # fecundity.length.max <- # maximum length # fecundity units (plural) - # fecundity.units : added with add_more_key_quants + # fecundity.units # fecundity.min <- # minimum fecundity # fecundity.max <- # maximum fecundity @@ -657,7 +334,7 @@ write_captions <- function(dat, # converted model output object # mod.fit.catch.end.year <- # end year of model fit to catch ts plot # catch units (plural) - # mod.fit.catch.units : added with add_more_key_quants + # mod.fit.catch.units # mod.fit.catch.min <- # minimum catch # mod.fit.catch.max <- # maximum catch @@ -689,40 +366,30 @@ write_captions <- function(dat, # converted model output object # selectivity.length.min <- # minimum length # selectivity.length.max <- # maximum length - # TODO: uncomment and recode once we get clarity about how to extract R0 properly - ## relative recruitment - # # minimum relative recruitment - # rel.recruitment.min <- (recruitment.min / R0) |> - # round(digits = 2) - # - # # maximum relative recruitment - # rel.recruitment.max <- (recruitment.max / R0) |> - # round(digits = 2) - ## tot_b (total biomass): same as B plot above - # ssbtarg : added with add_more_key_quants + # ssbtarg ## spr (spawning potential ratio) # minimum spr - spr.min <- dat |> - dplyr::filter(c(grepl("spr", label) | - label == "spr") & - !is.na(year) & - !is.na(estimate)) |> - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) - - # maximum spr - spr.max <- dat |> - dplyr::filter(c(grepl("spr", label) | - label == "spr") & !is.na(year) & !is.na(estimate)) |> - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - as.numeric() |> - round(digits = 2) + # spr.min <- dat |> + # dplyr::filter(c(grepl("spr", label) | + # label == "spr") & + # !is.na(year) & + # !is.na(estimate)) |> + # dplyr::slice(which.min(estimate)) |> + # dplyr::select(estimate) |> + # as.numeric() |> + # round(digits = 2) + # + # # maximum spr + # spr.max <- dat |> + # dplyr::filter(c(grepl("spr", label) | + # label == "spr") & !is.na(year) & !is.na(estimate)) |> + # dplyr::slice(which.max(estimate)) |> + # dplyr::select(estimate) |> + # as.numeric() |> + # round(digits = 2) # TODO: uncomment and recode once we get clarity about how to extract this value properly # spr reference point @@ -736,32 +403,31 @@ write_captions <- function(dat, # converted model output object ## proj_catch (projected catch) # projected catch units (plural) # proj.catch.units <- # probably mt, but wait until figure coded - # --then add into add_more_key_quants() # start year of projected catch plot - proj.catch.start.year <- landings.end.year + 1 + # proj.catch.start.year <- landings.end.year + 1 # end year of projected catch plot - proj.catch.end.year <- dat |> - dplyr::filter(label == "catch" & module_name == "DERIVED_QUANTITIES") |> - dplyr::slice(which.max(year)) |> - dplyr::select(year) |> - as.numeric() + # proj.catch.end.year <- dat |> + # dplyr::filter(label == "catch" & module_name == "DERIVED_QUANTITIES") |> + # dplyr::slice(which.max(year)) |> + # dplyr::select(year) |> + # as.numeric() # minimum projected catch - proj.catch.min <- dat |> - # no BAM file has catch; will be NA - dplyr::filter(label == "catch" & module_name == "DERIVED_QUANTITIES") |> - dplyr::slice(which.min(estimate)) |> - dplyr::select(estimate) |> - as.numeric() + # proj.catch.min <- dat |> + # # no BAM file has catch; will be NA + # dplyr::filter(label == "catch" & module_name == "DERIVED_QUANTITIES") |> + # dplyr::slice(which.min(estimate)) |> + # dplyr::select(estimate) |> + # as.numeric() # maximum projected catch - proj.catch.max <- dat |> - dplyr::filter(label == "catch" & module_name == "DERIVED_QUANTITIES") |> - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> - as.numeric() + # proj.catch.max <- dat |> + # dplyr::filter(label == "catch" & module_name == "DERIVED_QUANTITIES") |> + # dplyr::slice(which.max(estimate)) |> + # dplyr::select(estimate) |> + # as.numeric() # TABLES----- @@ -775,185 +441,6 @@ write_captions <- function(dat, # converted model output object # catchability.fleet <- # fleet - # add in more quantities here, and update the quantities above - - # substitute quantity placeholders in the captions/alt text with - # the real values, extracted above - - - # make list with all placeholders - # uncomment placeholders once uncommented, above - patterns_replacements <- c( - # FIGURES----- - - ## kobe plot - # 'kobe.end.year' = as.character(kobe.end.year), - # 'B.BMSY.end.yr' = as.character(B.BMSY.end.yr), - # 'F.FMSY.end.yr' = as.character(F.FMSY.end.yr), - # 'overfished.status.is.isnot' = as.character(overfished.status.is.isnot), - # 'overfishing.status.is.isnot' = as.character(overfishing.status.is.isnot), - - - ## Biomass plot - # 'R0' = as.character(R0), - # 'Bend' = as.character(Bend), - # 'Btarg' = as.character(Btarg), - # 'Bmsy' = as.character(Bmsy), - - ## mortality (F) plot - # 'F.ref.pt' = as.character(F.ref.pt), - # 'Ftarg' = as.character(Ftarg), - - ## vonB LAA (von Bertalanffy growth function + length at age) - # 'vonb.age.min' = as.character(vonb.age.min), - # 'vonb.age.max' = as.character(vonb.age.max), - # 'vonb.length.units' = as.character(vonb.length.units), - # 'vonb.length.min' = as.character(vonb.length.min), - # 'vonb.length.max' = as.character(vonb.length.max), - - ## length-type conversion plot - # 'total.length.units' = as.character(total.length.units), - # 'total.length.min' = as.character(total.length.min), - # 'total.length.max' = as.character(total.length.max), - # 'fork.length.units' = as.character(fork.length.units), - # 'fork.length.min' = as.character(fork.length.min), - # 'fork.length.max' = as.character(fork.length.max), - - ## weight-length conversion plot - # 'wl.length.units' = as.character(wl.length.units), - # 'wl.length.min' = as.character(wl.length.min), - # 'wl.length.max' = as.character(wl.length.max), - # 'wl.weight.units' = as.character(wl.weight.units), - # 'wl.weight.min' = as.character(wl.weight.min), - # 'wl.weight.max' = as.character(wl.weight.max), - - ## maturity schedule (proportion mature) - # 'prop.mat.length.units' = as.character(prop.mat.length.units), - # 'prop.mat.length.min' = as.character(prop.mat.length.min), - # 'prop.mat.length.max' = as.character(prop.mat.length.max), - - ## fecundity at length - # 'fecundity.length.units' = as.character(fecundity.length.units), - # 'fecundity.length.min' = as.character(fecundity.length.min), - # 'fecundity.length.max' = as.character(fecundity.length.max), - # 'fecundity.units' = as.character(fecundity.units), - # 'fecundity.min' = as.character(fecundity.min), - # 'fecundity.max' = as.character(fecundity.max), - - ## CAL (catch at length) - # 'cal.length.min' = as.character(cal.length.min), - # 'cal.length.max' = as.character(cal.length.max), - - ## mod_fit_catch (model fit to catch ts) - # 'mod.fit.catch.start.year' = as.character(mod.fit.catch.start.year), - # 'mod.fit.catch.end.year' = as.character(mod.fit.catch.end.year), - # 'mod.fit.catch.units' = as.character(mod.fit.catch.units), - # 'mod.fit.catch.min' = as.character(mod.fit.catch.min), - # 'mod.fit.catch.max' = as.character(mod.fit.catch.max), - - ## mod_fit_abun (model fit to abundance indices plot) - # 'mod.fit.abun.start.year' = as.character(mod.fit.abun.start.year), - # 'mod.fit.abun.end.year' = as.character(mod.fit.abun.end.year), - - ## mod_fit_discards - # 'mod.fit.discards.start.year' = as.character(mod.fit.discards.start.year), - # 'mod.fit.discards.end.year' = as.character(mod.fit.discards.end.year), - # 'mod.fit.discards.units' = as.character(mod.fit.discards.units), - # 'mod.fit.discards.min' = as.character(mod.fit.discards.min), - # 'mod.fit.discards.max' = as.character(mod.fit.discards.max), - - ## selectivity - # 'selectivity.start.year' = as.character(selectivity.start.year), - # 'selectivity.end.year' = as.character(selectivity.end.year), - # 'selectivity.length.units' = as.character(selectivity.length.units), - # 'selectivity.length.min' = as.character(selectivity.length.min), - # 'selectivity.length.max' = as.character(selectivity.length.max), - - # relative recruitment ts - # NOTE: moving this above recruitment so rel.recruitment.min isn't changed - # to "rel." + recruitment.min (etc.) - # 'rel.recruitment.min' = as.character(rel.recruitment.min), - # 'rel.recruitment.max' = as.character(rel.recruitment.max), - - ## spr (spawning potential ratio) - "spr.min" = as.character(spr.min), - "spr.max" = as.character(spr.max), - # 'spr.ref.pt' = as.character(spr.ref.pt), - - ## proj_catch (projected catch) - # 'proj.catch.units' = as.character(proj.catch.units), - "proj.catch.start.year" = as.character(proj.catch.start.year), - "proj.catch.end.year" = as.character(proj.catch.end.year), - "proj.catch.min" = as.character(proj.catch.min), - "proj.catch.max" = as.character(proj.catch.max) # , - - # # TABLES----- - # - # ## catch - # 'catch.fleet' = as.character(catch.fleet), - # - # ## discards - # 'discards.tbl.units' = as.character(discards.tbl.units), - # - # ## catchability - # 'catchability.fleet' = as.character(catchability.fleet) - ) - - # If a value in patterns_replacements = NA, then make it "NA" - # to avoid errors - patterns_replacements <- tidyr::replace_na(patterns_replacements, "NA") - - # take the values associated with the quantities and replace the df's - # placeholders with them. For example, if ssb_min = 10, this will replace - # "the minimum ssb = ssb_min" with "the minimum ssb = 10". - - # replace values in caption column - caps_alttext$caption <- stringr::str_replace_all( - caps_alttext$caption, - patterns_replacements - ) - - # replace values in alt text column - caps_alttext$alt_text <- stringr::str_replace_all( - caps_alttext$alt_text, - patterns_replacements - ) - - # export df with updated captions and alt text to csv - utils::write.csv( - x = caps_alttext, - file = fs::path( - dir, - "captions_alt_text.csv" - ), - row.names = FALSE - ) - - - # message explaining the extracted and inserted key quantities - replaced_vals <- patterns_replacements |> - as.data.frame() |> - tibble::rownames_to_column() |> - dplyr::rename( - "name" = 1, - "key_quantity" = 2 - ) - - cli::cli_h3("Key quantities extracted and inserted from write_captions().") - cli::cli_alert_info("NA values signify key quantities that were not extracted and inserted.", wrap = TRUE) - for (i in 1:dim(replaced_vals)[1]) { - cli::cli_li(paste0( - replaced_vals[i, 1], - ": ", - replaced_vals[i, 2] - )) - } - - # enable warnings again - options(warn = 0) - } -} - #------------------------------------------------------------------------------ #' Extract captions and alternative texts diff --git a/man/write_captions.Rd b/man/write_captions.Rd deleted file mode 100644 index 503119f4..00000000 --- a/man/write_captions.Rd +++ /dev/null @@ -1,38 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils_rda.R -\name{write_captions} -\alias{write_captions} -\title{Write captions and alternative text} -\usage{ -write_captions(dat, dir = getwd(), year = format(Sys.Date(), "\%Y")) -} -\arguments{ -\item{dat}{A tibble or named list of tibbles (input as `list()`) -returned from \link[stockplotr]{convert_output}. - -If inputting a list of tibbles, the first tibble's reference point defined -in `ref_line` is used to plot a reference line or calculate relative spawning biomass.} - -\item{dir}{Directory where the output captions and alt text file should be -saved. Defaults to working directory.} - -\item{year}{the last year of the data or the current year this function is -being performed. Defaults to the current year.} -} -\value{ -Exports .csv with captions and alt text for figures and tables -that contain key quantities (e.g., an assessment's start year) that -are automatically extracted from the converted model results file. -} -\description{ -Function to create captions and alternative text that contain -key quantities from the model results file. -} -\examples{ -\dontrun{ -write_captions(dat, - dir = getwd(), - year = 2025 -) -} -} From 45abb4c66e3713eb4510fecc0a2870e02362c23c Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 20 Mar 2026 11:37:17 -0400 Subject: [PATCH 18/23] Add documentation for new functions --- R/utils_rda.R | 51 +++++++++++++++++++++++++++++++++++++++++----- man/export_kqs.Rd | 27 ++++++++++++++++++++++++ man/fill_in_kqs.Rd | 32 +++++++++++++++++++++++++++++ man/insert_kqs.Rd | 27 ++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 man/export_kqs.Rd create mode 100644 man/fill_in_kqs.Rd create mode 100644 man/insert_kqs.Rd diff --git a/R/utils_rda.R b/R/utils_rda.R index 2c4e097e..2f8a6c81 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -4,7 +4,24 @@ # TODO: update new fxns to work with a specified 'dir' instead of default 'getwd()'? -# Fill in key quantities in template +#' Substitute key quantities' values into template +#' +#' @param df Dataframe created by importing "key_quantity_template.csv" or +#' "key_quantities.csv", empty or partially-filled templates with key +#' quantity names and other associated information +#' @param ... Key quantity objects whose values will be added to the output +#' dataframe +#' +#' @returns Dataframe based on key quantities template that contains +#' newly-added values of key quantities indicated via ellipsis. The dataframe +#' is input for [fill_in_kqs()]. +#' +#' @examples \dontrun{ +#' fill_in_kqs( +#' df = data, +#' F.min, +#' F.max) +#' } fill_in_kqs <- function(df, ...) { arg_names <- sapply(substitute(list(...))[-1], deparse) @@ -26,8 +43,20 @@ fill_in_kqs <- function(df, ...) { dplyr::select(-value_new) } -# Calculate and export key quantities -## kqs (e.g., landings.end.year) are the ellipsis args +#' Export updated key quantities template +#' +#' @param ... Key quantities whose values should be added to the +#' exported "key_quantities.csv" +#' +#' @returns Exports a file based on key quantities template, with values +#' added next to the names of the key quantities specified as ellipsis +#' arguments. File is saved as "key_quantities.csv" to the working directory. +#' +#' @examples \dontrun{ +#' export_kqs( +#' F.min, +#' F.max) +#' } export_kqs <- function(...) { # Open new or existing key quantities csv @@ -52,8 +81,20 @@ export_kqs <- function(...) { } -# Add key quantities to captions/alt text csv -## kqs (e.g., landings.end.year) are the ellipsis args +#' Insert key quantities into the captions and alternative text file +#' +#' @param ... Key quantities whose values should be added to the +#' exported "captions_alt_text.csv" +#' +#' @returns Exports a file ("captions_alt_text.csv") containing captions +#' and alternative text for figures and tables, with key quantities inserted +#' into the "captions_alt_text_template.csv" template's placeholders. +#' +#' @examples \dontrun{ +#' insert_kqs( +#' F.min, +#' F.max) +#' } insert_kqs <- function(...) { if (file.exists(fs::path(getwd(), "captions_alt_text.csv"))) { cli::cli_alert_info("Captions/alternative text file (captions_alt_text.csv) exists. Newly calculated key quantities will be added to it.", wrap = TRUE) diff --git a/man/export_kqs.Rd b/man/export_kqs.Rd new file mode 100644 index 00000000..0df5d304 --- /dev/null +++ b/man/export_kqs.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils_rda.R +\name{export_kqs} +\alias{export_kqs} +\title{Export updated key quantities template} +\usage{ +export_kqs(...) +} +\arguments{ +\item{...}{Key quantities whose values should be added to the +exported "key_quantities.csv"} +} +\value{ +Exports a file based on key quantities template, with values +added next to the names of the key quantities specified as ellipsis +arguments. File is saved as "key_quantities.csv" to the working directory. +} +\description{ +Export updated key quantities template +} +\examples{ +\dontrun{ +export_kqs( + F.min, + F.max) +} +} diff --git a/man/fill_in_kqs.Rd b/man/fill_in_kqs.Rd new file mode 100644 index 00000000..dbccc2f4 --- /dev/null +++ b/man/fill_in_kqs.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils_rda.R +\name{fill_in_kqs} +\alias{fill_in_kqs} +\title{Substitute key quantities' values into template} +\usage{ +fill_in_kqs(df, ...) +} +\arguments{ +\item{df}{Dataframe created by importing "key_quantity_template.csv" or +"key_quantities.csv", empty or partially-filled templates with key +quantity names and other associated information} + +\item{...}{Key quantity objects whose values will be added to the output +dataframe} +} +\value{ +Dataframe based on key quantities template that contains +newly-added values of key quantities indicated via ellipsis. The dataframe +is input for [fill_in_kqs()]. +} +\description{ +Substitute key quantities' values into template +} +\examples{ +\dontrun{ +fill_in_kqs( + df = data, + F.min, + F.max) +} +} diff --git a/man/insert_kqs.Rd b/man/insert_kqs.Rd new file mode 100644 index 00000000..22c53495 --- /dev/null +++ b/man/insert_kqs.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils_rda.R +\name{insert_kqs} +\alias{insert_kqs} +\title{Insert key quantities into the captions and alternative text file} +\usage{ +insert_kqs(...) +} +\arguments{ +\item{...}{Key quantities whose values should be added to the +exported "captions_alt_text.csv"} +} +\value{ +Exports a file ("captions_alt_text.csv") containing captions +and alternative text for figures and tables, with key quantities inserted +into the "captions_alt_text_template.csv" template's placeholders. +} +\description{ +Insert key quantities into the captions and alternative text file +} +\examples{ +\dontrun{ +insert_kqs( + F.min, + F.max) +} +} From 6eb5fb53647e8c248b6fcb9b6b4869dd5ec6b232 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 20 Mar 2026 11:57:06 -0400 Subject: [PATCH 19/23] Round numeric key quantities to 3 digits --- R/plot_abundance_at_age.R | 4 ++-- R/plot_biomass.R | 4 ++-- R/plot_biomass_at_age.R | 4 ++-- R/plot_catch_comp.R | 4 ++-- R/plot_fishing_mortality.R | 10 ++++++---- R/plot_indices.R | 4 ++-- R/plot_landings.R | 4 ++-- R/plot_natural_mortality.R | 4 ++-- R/plot_recruitment.R | 4 ++-- R/plot_recruitment_deviations.R | 6 +++--- R/plot_spawning_biomass.R | 10 ++++++---- R/plot_stock_recruitment.R | 8 ++++---- 12 files changed, 35 insertions(+), 31 deletions(-) diff --git a/R/plot_abundance_at_age.R b/R/plot_abundance_at_age.R index b08b0a41..a6cd47ea 100644 --- a/R/plot_abundance_at_age.R +++ b/R/plot_abundance_at_age.R @@ -117,8 +117,8 @@ plot_abundance_at_age <- function( pop.naa.end.year <- max(data$year) pop.naa.age.min <- min(data$age) pop.naa.age.max <- max(data$age) - pop.naa.fish.min <- min(data$estimate) - pop.naa.fish.max <- max(data$estimate) + pop.naa.fish.min <- min(data$estimate) |> round(digits = 3) + pop.naa.fish.max <- max(data$estimate) |> round(digits = 3) # calculate & export key quantities export_kqs(pop.naa.start.year, diff --git a/R/plot_biomass.R b/R/plot_biomass.R index ae01312b..9619f10c 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -176,8 +176,8 @@ plot_biomass <- function( B.ref.pt <- as.character(ref_line) B.units <- as.character(unit_label) - B.start.year <- min(prepared_data$year) - B.end.year <- max(prepared_data$year) + B.start.year <- min(prepared_data$year) |> round(digits = 3) + B.end.year <- max(prepared_data$year) |> round(digits = 3) export_kqs(B.ref.pt, B.units, diff --git a/R/plot_biomass_at_age.R b/R/plot_biomass_at_age.R index c6d74112..024574c8 100644 --- a/R/plot_biomass_at_age.R +++ b/R/plot_biomass_at_age.R @@ -101,8 +101,8 @@ plot_biomass_at_age <- function( pop.baa.end.year <- max(data$year) pop.baa.age.min <- min(data$age) pop.baa.age.max <- max(data$age) - pop.baa.fish.min <- min(data$estimate) - pop.baa.fish.max <- max(data$estimate) + pop.baa.fish.min <- min(data$estimate) |> round(digits = 3) + pop.baa.fish.max <- max(data$estimate) |> round(digits = 3) # calculate & export key quantities export_kqs(pop.baa.start.year, diff --git a/R/plot_catch_comp.R b/R/plot_catch_comp.R index 2667216c..4a5b08d3 100644 --- a/R/plot_catch_comp.R +++ b/R/plot_catch_comp.R @@ -121,8 +121,8 @@ plot_catch_comp <- function( caa.end.year <- max(data$year) caa.age.min <- min(data$age) caa.age.max <- max(data$age) - tot.catch.min <- min(data$estimate) - tot.catch.max <- max(data$estimate) + tot.catch.min <- min(data$estimate) |> round(digits = 3) + tot.catch.max <- max(data$estimate) |> round(digits = 3) # calculate & export key quantities export_kqs(caa.start.year, diff --git a/R/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index 0d41262d..8791455d 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -102,11 +102,13 @@ plot_fishing_mortality <- function( rel.F.min <- ggplot2::ggplot_build(final)@data[[2]] |> as.data.frame() |> dplyr::pull(y) |> - min() + min() |> + round(digits = 2) rel.F.max <- ggplot2::ggplot_build(final)@data[[2]] |> as.data.frame() |> dplyr::pull(y) |> - max() + max() |> + round(digits = 2) # calculate & export key quantities export_kqs(rel.F.min, rel.F.max) @@ -115,8 +117,8 @@ plot_fishing_mortality <- function( insert_kqs(rel.F.min, rel.F.max) } else { - F.min <- min(prepared_data$estimate) - F.max <- max(prepared_data$estimate) + F.min <- min(prepared_data$estimate) |> round(digits = 3) + F.max <- max(prepared_data$estimate) |> round(digits = 3) export_kqs(F.min, F.max) insert_kqs(F.min, F.max) diff --git a/R/plot_indices.R b/R/plot_indices.R index bda2ab1d..645bde3c 100644 --- a/R/plot_indices.R +++ b/R/plot_indices.R @@ -118,8 +118,8 @@ plot_indices <- function( # Obtain relevant key quantities for captions/alt text cpue.start.year <- min(prepared_data$year) cpue.end.year <- max(prepared_data$year) - cpue.min <- min(prepared_data$estimate) - cpue.max <- max(prepared_data$estimate) + cpue.min <- min(prepared_data$estimate) |> round(digits = 3) + cpue.max <- max(prepared_data$estimate) |> round(digits = 3) cpue.units <- ifelse(unit_label == "", "an estimated index ratio", # default if empty unit_label) diff --git a/R/plot_landings.R b/R/plot_landings.R index 8f7e32a4..aed3e76b 100644 --- a/R/plot_landings.R +++ b/R/plot_landings.R @@ -103,8 +103,8 @@ plot_landings <- function( # Obtain relevant key quantities for captions/alt text landings.start.year <- min(prepared_data$year) landings.end.year <- max(prepared_data$year) - landings.min <- min(prepared_data$estimate) - landings.max <- max(prepared_data$estimate) + landings.min <- min(prepared_data$estimate) |> round(digits = 3) + landings.max <- max(prepared_data$estimate) |> round(digits = 3) landings.units <- unit_label # calculate & export key quantities diff --git a/R/plot_natural_mortality.R b/R/plot_natural_mortality.R index aac0745f..ebf2fe96 100644 --- a/R/plot_natural_mortality.R +++ b/R/plot_natural_mortality.R @@ -104,8 +104,8 @@ plot_natural_mortality <- function( # Obtain relevant key quantities for captions/alt text M.age.min <- min(processed_data$age) M.age.max <- max(processed_data$age) - M.rate.min <- min(processed_data$estimate) - M.rate.max <- max(processed_data$estimate) + M.rate.min <- min(processed_data$estimate) |> round(digits = 3) + M.rate.max <- max(processed_data$estimate) |> round(digits = 3) # calculate & export key quantities export_kqs(M.age.min, diff --git a/R/plot_recruitment.R b/R/plot_recruitment.R index fd091c67..80bf4fc5 100644 --- a/R/plot_recruitment.R +++ b/R/plot_recruitment.R @@ -150,8 +150,8 @@ plot_recruitment <- function( recruitment.units <- as.character(unit_label) recruitment.start.year <- min(recruitment$year) recruitment.end.year <- max(recruitment$year) - recruitment.min <- min(recruitment$predicted_recruitment) - recruitment.max <- max(recruitment$predicted_recruitment) + recruitment.min <- min(recruitment$predicted_recruitment) |> round(digits = 3) + recruitment.max <- max(recruitment$predicted_recruitment) |> round(digits = 3) # calculate & export key quantities export_kqs(recruitment.units, diff --git a/R/plot_recruitment_deviations.R b/R/plot_recruitment_deviations.R index e757cd68..ed6e90ae 100644 --- a/R/plot_recruitment_deviations.R +++ b/R/plot_recruitment_deviations.R @@ -104,9 +104,9 @@ plot_recruitment_deviations <- function( # Obtain relevant key quantities for captions/alt text recruit.dev.start.year <- min(filter_data$year) recruit.dev.end.year <- max(filter_data$year) - recruit.dev.min <- min(filter_data$estimate) - recruit.dev.max <- max(filter_data$estimate) - + recruit.dev.min <- min(filter_data$estimate) |> round(digits = 3) + recruit.dev.max <- max(filter_data$estimate) |> round(digits = 3) + # calculate & export key quantities export_kqs(recruit.dev.start.year, recruit.dev.end.year, diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index f7978f3f..9448f129 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -241,11 +241,13 @@ plot_spawning_biomass <- function( rel.ssb.min <- ggplot2::ggplot_build(final)@data[[2]] |> as.data.frame() |> dplyr::pull(y) |> - min() + min() |> + round(digits = 2) rel.ssb.max <- ggplot2::ggplot_build(final)@data[[2]] |> as.data.frame() |> dplyr::pull(y) |> - max() + max() |> + round(digits = 2) # calculate & export key quantities export_kqs(rel.ssb.min, rel.ssb.max) @@ -254,8 +256,8 @@ plot_spawning_biomass <- function( insert_kqs(rel.ssb.min, rel.ssb.max) } else { - ssb.min <- min(plot_data$estimate) - ssb.max <- max(plot_data$estimate) + ssb.min <- min(plot_data$estimate) |> round(digits = 3) + ssb.max <- max(plot_data$estimate) |> round(digits = 3) export_kqs(ssb.min, ssb.max) insert_kqs(ssb.min, ssb.max) diff --git a/R/plot_stock_recruitment.R b/R/plot_stock_recruitment.R index 40195e15..39b2bf40 100644 --- a/R/plot_stock_recruitment.R +++ b/R/plot_stock_recruitment.R @@ -135,10 +135,10 @@ plot_stock_recruitment <- function( dplyr::select(age) |> as.numeric() sr.ssb.units <- spawning_biomass_label - sr.ssb.min <- min(sr$spawning_biomass, na.rm = TRUE) - sr.ssb.max <- max(sr$spawning_biomass, na.rm = TRUE) - recruitment.min <- min(sr$predicted_recruitment, na.rm = TRUE) - recruitment.max <- max(sr$predicted_recruitment, na.rm = TRUE) + sr.ssb.min <- min(sr$spawning_biomass, na.rm = TRUE) |> round(digits = 3) + sr.ssb.max <- max(sr$spawning_biomass, na.rm = TRUE) |> round(digits = 3) + recruitment.min <- min(sr$predicted_recruitment, na.rm = TRUE) |> round(digits = 3) + recruitment.max <- max(sr$predicted_recruitment, na.rm = TRUE) |> round(digits = 3) recruitment.units <- recruitment_label # calculate & export key quantities From d712ee092f4c319ef024c9aafd0509af8201357e Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 20 Mar 2026 13:39:47 -0400 Subject: [PATCH 20/23] Update tests --- tests/testthat/test-export_rda.R | 22 +++++------ tests/testthat/test-extract_caps_alttext.R | 22 +++++------ tests/testthat/test-write_captions.R | 43 ---------------------- 3 files changed, 20 insertions(+), 67 deletions(-) delete mode 100644 tests/testthat/test-write_captions.R diff --git a/tests/testthat/test-export_rda.R b/tests/testthat/test-export_rda.R index 7ebc9e26..cd543048 100644 --- a/tests/testthat/test-export_rda.R +++ b/tests/testthat/test-export_rda.R @@ -8,12 +8,11 @@ test_that("export_rda works for figures", { topic_label <- "biomass" fig_or_table <- "figure" - # run write_captions.R - write_captions( - dat = out_new, - dir = getwd(), - year = 2022 - ) + B.min <- 100 + B.max <- 200 + + # add KQs to caps/alt text csv + insert_kqs(B.min, B.max) # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext( @@ -51,12 +50,11 @@ test_that("export_rda works for tables", { topic_label <- "bnc" fig_or_table <- "table" - # run write_captions.R - write_captions( - dat = out_new, - dir = getwd(), - year = 2022 - ) + B.min <- 100 + B.max <- 200 + + # add KQs to caps/alt text csv + insert_kqs(B.min, B.max) # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext( diff --git a/tests/testthat/test-extract_caps_alttext.R b/tests/testthat/test-extract_caps_alttext.R index b2b1c522..df29d97a 100644 --- a/tests/testthat/test-extract_caps_alttext.R +++ b/tests/testthat/test-extract_caps_alttext.R @@ -8,12 +8,11 @@ test_that("extract_caps_alttext works for figures", { topic_label <- "biomass" fig_or_table <- "figure" - # run write_captions.R - write_captions( - dat = out_new, - dir = getwd(), - year = 2022 - ) + B.min <- 100 + B.max <- 200 + + # add KQs to caps/alt text csv + insert_kqs(B.min, B.max) # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext( @@ -40,12 +39,11 @@ test_that("extract_caps_alttext works for tables", { topic_label <- "bnc" fig_or_table <- "table" - # run write_captions.R - write_captions( - dat = out_new, - dir = getwd(), - year = 2022 - ) + B.min <- 100 + B.max <- 200 + + # add KQs to caps/alt text csv + insert_kqs(B.min, B.max) # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext( diff --git a/tests/testthat/test-write_captions.R b/tests/testthat/test-write_captions.R deleted file mode 100644 index 2198706c..00000000 --- a/tests/testthat/test-write_captions.R +++ /dev/null @@ -1,43 +0,0 @@ -test_that("write_captions() function imports alt text/captions template - as a data frame", { - # import pre-written captions and alt text template - caps_alttext <- utils::read.csv(system.file("resources", "captions_alt_text_template.csv", package = "stockplotr")) - - expect_s3_class(caps_alttext, "data.frame") - - expect_no_error(caps_alttext) -}) - -test_that("write_captions() function replaces placeholder text with key quantities as expected", { - # load sample dataset - load(file.path( - "fixtures", "ss3_models_converted", "Hake_2018", - "std_output.rda" - )) - - # make captions/alt text csv - write_captions(out_new, dir = here::here(), year = 2022) - - # import csv - caps <- read.csv(here::here("captions_alt_text.csv")) - - # extract alt text from biomass figure - B_alt_text <- caps |> - dplyr::filter( - label == "biomass", - type == "figure" - ) |> - dplyr::select(alt_text) |> - as.character() - - # expected alt text from end of biomass figure - expected_alt_text_substring <- "spans from 1964 to B.end.year" - - # test expected alt text within B alt text - expect_true( - grepl(expected_alt_text_substring, B_alt_text, fixed = T) - ) - - # erase files placed in here::here() - on.exit(unlink(fs::path(here::here(), "captions_alt_text.csv"), recursive = TRUE)) -}) From fd1b5f435da855ec839e83b50500150f5c303b1e Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 20 Mar 2026 14:12:32 -0400 Subject: [PATCH 21/23] Update vignettes with new KQs workflow; change package syntax from `` to {} --- CODE_OF_CONDUCT.md | 4 +-- CONTRIBUTING.md | 8 ++--- DESCRIPTION | 2 +- README.md | 10 +++--- inst/WORDLIST | 1 + man/stockplotr-package.Rd | 2 +- vignettes/convert_output_details.Rmd | 2 +- vignettes/how-caps-alttext-are-made.Rmd | 42 ++++++++----------------- vignettes/manual.Rmd | 6 ++-- 9 files changed, 31 insertions(+), 46 deletions(-) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 10babddf..42995ae8 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -47,7 +47,7 @@ Examples of representing our community include using an official e-mail address, Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement anonymously using this [form](https://forms.gle/MVNesUbP15bHei1J7). -Reports will be reviewed by a member of the NOAA Fisheries Office of Science and Technology who is not participating in the `stockplotr` project [Patrick Lynch] but has the full support of `stockplotr` Community Leaders. All reports will be reviewed promptly and fairly. +Reports will be reviewed by a member of the NOAA Fisheries Office of Science and Technology who is not participating in the {stockplotr} project [Patrick Lynch] but has the full support of {stockplotr} Community Leaders. All reports will be reviewed promptly and fairly. All community leaders are obligated to respect the privacy and security of the reporter of any incident whenever possible; however, please note behaviors that meet the official criteria for harrassment must be reported by supervisors under NOAA policy. @@ -103,4 +103,4 @@ Community Impact Guidelines were inspired by For answers to common questions about this code of conduct, see the FAQ at . Translations are available at . -[homepage]: https://www.contributor-covenant.org \ No newline at end of file +[homepage]: https://www.contributor-covenant.org diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 23cbec70..d322b3ab 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,6 @@ -# Contributing to `stockplotr` +# Contributing to {stockplotr} -Do you have an idea that would improve `stockplotr`? *Consider making a contribution!* We welcome ideas for improving not only our code, but also our documentation, tutorial, and any other material associated with `stockplotr`. Here are some options and tips for doing so. +Do you have an idea that would improve {stockplotr}? *Consider making a contribution!* We welcome ideas for improving not only our code, but also our documentation, tutorial, and any other material associated with {stockplotr}. Here are some options and tips for doing so. Note: To make any contribution, you must agree to abide by the [Code of Conduct](https://github.com/nmfs-ost/stockplotr/blob/main/CODE_OF_CONDUCT.md). @@ -8,7 +8,7 @@ Note: To make any contribution, you must agree to abide by the [Code of Conduct] ### Recommended workflow: fork & submit a pull request -The most efficient way to contribute an idea is to fork `stockplotr`, make your suggested changes on a local branch, and then submit a pull request. This will allow the developers to easily evaluate your suggested changes. Please see the [GitHub Docs' "Contributing to a project" page](https://docs.github.com/en/get-started/exploring-projects-on-github/contributing-to-a-project) for step-by-step guidance in using this workflow. +The most efficient way to contribute an idea is to fork {stockplotr}, make your suggested changes on a local branch, and then submit a pull request. This will allow the developers to easily evaluate your suggested changes. Please see the [GitHub Docs' "Contributing to a project" page](https://docs.github.com/en/get-started/exploring-projects-on-github/contributing-to-a-project) for step-by-step guidance in using this workflow. ### Recommended practices @@ -26,4 +26,4 @@ If possible, please submit a reproducible example ([reprex](https://reprex.tidyv ## Contributing questions -Have a question? Ask it in our [Discussions page](https://github.com/nmfs-ost/stockplotr/discussions). You can categorize it under General, Ideas, Q&A, and more. \ No newline at end of file +Have a question? Ask it in our [Discussions page](https://github.com/nmfs-ost/stockplotr/discussions). You can categorize it under General, Ideas, Q&A, and more. diff --git a/DESCRIPTION b/DESCRIPTION index 99d97f9d..e1eb071b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,7 +20,7 @@ Description: Creates exploratory and finished tables and figures for stock interprets outputs of stock assessment models as well as allows the analyst to create report ready tables and figures, reducing the need to create their own and format then when adding into a report. This - package is intended to be used in conjuction with `asar`, a partially + package is intended to be used in conjuction with {asar}, a partially automated template for writing various stock assessment reports. Throughout development, we will be creating a set of standardized figures and tables for a stock assessment report, developing functions diff --git a/README.md b/README.md index 323bfbd8..18229e96 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ***Previously named 'satf'*** -The goal of `stockplotr` is to create a centralized package that contains all of +The goal of {stockplotr} is to create a centralized package that contains all of the figures and tables for a stock assessment report used to inform management. There are multiple current packages that perform a similar function, but they are typically region and/or model dependent. Across @@ -51,12 +51,12 @@ install.packages("stockplotr", repos = c("https://noaa-fisheries-integrated-tool ## Usage -Please refer to the [`asar` tutorial](https://connect.fisheries.noaa.gov/asar_tutorial/#section-preparing-to-run-create_template) to learn how to produce the input file necessary to create figures and tables with `stockplotr`. +Please refer to the [{asar} tutorial](https://connect.fisheries.noaa.gov/asar_tutorial/#section-preparing-to-run-create_template) to learn how to produce the input file necessary to create figures and tables with {stockplotr}. -Then, once your converted model results are saved as an object in your R environment, you can use `stockplotr` functions to create plots from the object. +Then, once your converted model results are saved as an object in your R environment, you can use {stockplotr} functions to create plots from the object. > [!TIP] -> `stockplotr` plots or tables are exported as `ggplot2` or `flextable` objects +> {stockplotr} plots or tables are exported as `ggplot2` or `flextable` objects > meaning that users can use the same process to add new data, formatting, or > other customizations to the plot beyond the baseline made by this package @@ -79,7 +79,7 @@ Example Plot | Example Table | ## Contributions -Have you identified any suggestions for improvement, bugs, or questions? Please see our [Contributing page](https://github.com/nmfs-ost/stockplotr/blob/main/CONTRIBUTING.md) for more information on how to make effective contributions to `stockplotr`. +Have you identified any suggestions for improvement, bugs, or questions? Please see our [Contributing page](https://github.com/nmfs-ost/stockplotr/blob/main/CONTRIBUTING.md) for more information on how to make effective contributions to {stockplotr}. Thank you for helping us improve this package! diff --git a/inst/WORDLIST b/inst/WORDLIST index 51214637..5859f26b 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -37,6 +37,7 @@ is’ kable kableExtra kobe +kqs len msy natively diff --git a/man/stockplotr-package.Rd b/man/stockplotr-package.Rd index a330555b..796b81d9 100644 --- a/man/stockplotr-package.Rd +++ b/man/stockplotr-package.Rd @@ -6,7 +6,7 @@ \alias{stockplotr-package} \title{stockplotr: Tables and Figures for Stock Assessments} \description{ -Creates exploratory and finished tables and figures for stock assessment documents from U.S. stock assessment model outputs. This packages addresses parts of the stock assessment workflow that interprets outputs of stock assessment models as well as allows the analyst to create report ready tables and figures, reducing the need to create their own and format then when adding into a report. This package is intended to be used in conjuction with `asar`, a partially automated template for writing various stock assessment reports. Throughout development, we will be creating a set of standardized figures and tables for a stock assessment report, developing functions to produce a variety of diagonostic plots, and other helpful materials. The advantage of using this package over others is that it applies to a range of stock assessment model outputs and standardizes them. +Creates exploratory and finished tables and figures for stock assessment documents from U.S. stock assessment model outputs. This packages addresses parts of the stock assessment workflow that interprets outputs of stock assessment models as well as allows the analyst to create report ready tables and figures, reducing the need to create their own and format then when adding into a report. This package is intended to be used in conjuction with {asar}, a partially automated template for writing various stock assessment reports. Throughout development, we will be creating a set of standardized figures and tables for a stock assessment report, developing functions to produce a variety of diagonostic plots, and other helpful materials. The advantage of using this package over others is that it applies to a range of stock assessment model outputs and standardizes them. } \seealso{ Useful links: diff --git a/vignettes/convert_output_details.Rmd b/vignettes/convert_output_details.Rmd index 64d48924..3bcd9d0c 100644 --- a/vignettes/convert_output_details.Rmd +++ b/vignettes/convert_output_details.Rmd @@ -26,7 +26,7 @@ knitr::opts_chunk$set(echo = TRUE) ## Background -This article describes the process behind developing the `stockplotr::convert_output` function and its setup so users who want to use `asar` or `stockplotr` are still able until [`convert_output.R`](https://github.com/nmfs-ost/stockplotr/blob/main/R/convert_output.R) includes functionality for other models. +This article describes the process behind developing the `stockplotr::convert_output` function and its setup so users who want to use {asar} or {stockplotr} are still able until [`convert_output.R`](https://github.com/nmfs-ost/stockplotr/blob/main/R/convert_output.R) includes functionality for other models.