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 3f5e946f..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 @@ -48,6 +48,7 @@ Imports: naniar, prodlim, quarto, + rlang, scales, stats, stringr, 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_abundance_at_age.R b/R/plot_abundance_at_age.R index a6a89638..87d6238b 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 ) @@ -97,6 +97,9 @@ plot_abundance_at_age <- function( ggplot2::ggplot() ) } + + data <- data |> + dplyr::mutate(age = as.numeric(age)) # Plot data plot <- plot_aa( @@ -111,6 +114,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) |> round(digits = 3) + pop.naa.fish.max <- max(data$estimate) |> round(digits = 3) + + # 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/plot_biomass.R b/R/plot_biomass.R index b89fa29a..9619f10c 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -148,6 +148,47 @@ 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) + } + + B.ref.pt <- as.character(ref_line) + B.units <- as.character(unit_label) + 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, + B.start.year, + B.end.year) + + insert_kqs(B.ref.pt, + B.units, + B.start.year, + B.end.year) + create_rda( object = final, topic_label = ifelse(relative, "relative_biomass", "biomass"), diff --git a/R/plot_biomass_at_age.R b/R/plot_biomass_at_age.R index 041dfac7..8f3a6fde 100644 --- a/R/plot_biomass_at_age.R +++ b/R/plot_biomass_at_age.R @@ -82,6 +82,9 @@ plot_biomass_at_age <- function( ) } + data <- data |> + dplyr::mutate(age = as.numeric(age)) + # Plot data plot <- plot_aa( dat = data, @@ -95,6 +98,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) |> round(digits = 3) + pop.baa.fish.max <- max(data$estimate) |> round(digits = 3) + + # 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/plot_catch_comp.R b/R/plot_catch_comp.R index 13b3f6fe..4a5b08d3 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) |> round(digits = 3) + tot.catch.max <- max(data$estimate) |> round(digits = 3) + + # 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/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index 3a48dd54..8791455d 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -95,6 +95,47 @@ 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() |> + round(digits = 2) + rel.F.max <- ggplot2::ggplot_build(final)@data[[2]] |> + as.data.frame() |> + dplyr::pull(y) |> + max() |> + round(digits = 2) + + # 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) |> round(digits = 3) + F.max <- max(prepared_data$estimate) |> round(digits = 3) + + 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) + + export_kqs(F.ref.pt, + F.start.year, + F.end.year) + + insert_kqs(F.ref.pt, + F.start.year, + F.end.year) + create_rda( object = final, topic_label = ifelse(relative, "relative_fishing_mortality", "fishing_mortality"), diff --git a/R/plot_indices.R b/R/plot_indices.R index e0ff78e7..645bde3c 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) |> 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) + + # 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/plot_landings.R b/R/plot_landings.R index 7fc9041e..aed3e76b 100644 --- a/R/plot_landings.R +++ b/R/plot_landings.R @@ -99,6 +99,28 @@ plot_landings <- function( ### Make RDA ---- 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.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 + export_kqs(landings.start.year, + landings.end.year, + landings.min, + landings.max, + landings.units) + + # Add key quantities to captions/alt text + insert_kqs(landings.start.year, + landings.end.year, + landings.min, + landings.max, + landings.units) + create_rda( object = plt, # get name of function and remove "plot_" from it diff --git a/R/plot_natural_mortality.R b/R/plot_natural_mortality.R index 9e496123..ebf2fe96 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" @@ -87,8 +82,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 +100,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) |> round(digits = 3) + M.rate.max <- max(processed_data$estimate) |> round(digits = 3) + + # 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/plot_recruitment.R b/R/plot_recruitment.R index 5728113c..80bf4fc5 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) |> round(digits = 3) + recruitment.max <- max(recruitment$predicted_recruitment) |> round(digits = 3) + + # 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_recruitment_deviations.R b/R/plot_recruitment_deviations.R index ca84f27b..ed6e90ae 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) |> 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, + 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/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 115c33f2..9448f129 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -235,6 +235,52 @@ 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() |> + round(digits = 2) + rel.ssb.max <- ggplot2::ggplot_build(final)@data[[2]] |> + as.data.frame() |> + dplyr::pull(y) |> + max() |> + round(digits = 2) + + # 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) |> round(digits = 3) + ssb.max <- max(plot_data$estimate) |> round(digits = 3) + + 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) + + # calculate & export key quantities + export_kqs(ssb.ref.pt, + ssb.units, + ssb.start.year, + ssb.end.year) + + # Add key quantities to captions/alt text + insert_kqs(ssb.ref.pt, + ssb.units, + ssb.start.year, + ssb.end.year) + create_rda( object = final, topic_label = ifelse(relative, "relative_spawning_biomass", "spawning_biomass"), diff --git a/R/plot_stock_recruitment.R b/R/plot_stock_recruitment.R index 65732a51..39b2bf40 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) |> 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 + export_kqs(sr.age.min, + sr.ssb.units, + sr.ssb.min, + sr.ssb.max, + 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, + recruitment.min, + recruitment.max, + recruitment.units) + create_rda( object = final, # get name of function and remove "plot_" from it diff --git a/R/stockplotr-package.R b/R/stockplotr-package.R index 8e0e5a2f..cee42eaa 100644 --- a/R/stockplotr-package.R +++ b/R/stockplotr-package.R @@ -17,6 +17,6 @@ globvar <- c( "plot_data", "quantile", "rda_dir", "reorder", "total_estimate", "zvar", "filter_data", "Component", "age_bins", "alt_label", "final_df", "init", "keyword", "len_bins", "like", "match_key", "morph", "nsim", "output", "output_order", "parm_stdev", "seas", - "sexes", "subseas", "unique_count", "value", "yr", "fleet_names", "." + "sexes", "subseas", "unique_count", "value", "yr", "fleet_names", "across", "everything", "fleet_name", "name", "value_new", "y", "." ) if (getRversion() >= "2.15.1") utils::globalVariables(globvar) 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/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 diff --git a/R/utils_rda.R b/R/utils_rda.R index 6d513427..aa4ec1f8 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -2,6 +2,186 @@ # RDA utility functions # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +# TODO: update key quantities functions to work with a specified 'dir' instead of default 'getwd()'? +# TODO: update key quantities functions for plots that need to extract values from KQs csv to calculate their own, dependent KQs (none present in current plotting functions) + +#' 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) + 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) +} + +#' 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 + 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 + ) + +} + +#' 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) + 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 ellipsis + arg_names <- sapply(rlang::enexprs(...), as.character) + + # Get the actual values + vals <- as.character(list(...)) + + # 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("(? + # 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 + ) |> + # remove the added regex characters + dplyr::mutate( + name = stringr::str_remove_all(name, "^\\(\\?\\ - 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 +244,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, @@ -105,604 +253,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 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 - # 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 - - ## 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)) { - cli::cli_alert_warning("Some key quantities associated with relative spawning biomass were not extracted and added to captions_alt_text.csv due to missing data file (i.e., 'dat' argument).", wrap = TRUE) - } - if (is.null(ref_line)) { - cli::cli_alert_warning("ref_line was not provided. ssbtarg, rel.ssb.min, and rel.ssb.max were not calculated.", wrap = TRUE) - } else { - # 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) - - 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) - - # 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)) { - 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 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) - - # 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) - )) |> - # 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)}") - } - } - - ## 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)) { - ### 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(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 - 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----- @@ -726,33 +280,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 |> @@ -780,215 +307,12 @@ 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) - - # 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 - - - ## 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 - 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 # 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 @@ -996,12 +320,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 @@ -1009,13 +333,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 @@ -1023,7 +347,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 @@ -1031,158 +355,28 @@ 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 - - ## 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 # 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) - - - ## 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 # 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 @@ -1214,176 +408,30 @@ 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 - - ## 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) |> - # round(digits = 2) - # - # # maximum relative recruitment - # 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) - # 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 - # relative ssb min - # rel.ssb.min : added with add_more_key_quants - - # relative ssb max - # rel.ssb.max : 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 @@ -1394,107 +442,40 @@ 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 - # --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----- ## catch # catch.fleet <- # fleet - ## landings - # landings.tbl.units <- # landings units; remove if units already in table - ## discards # discards.tbl.units <- # discards units @@ -1502,256 +483,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), - - ## 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 - "B.start.year" = as.character(B.start.year), - # '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), - "F.start.year" = as.character(F.start.year), - "F.min" = as.character(F.min), - "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), - # '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), - # '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), - - ## 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), - - ## 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), - - ## 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), - # '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), - - ## 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.) - # '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), - - ## 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), - - ## spr (spawning potential ratio) - "spr.min" = as.character(spr.min), - "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), - "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), - # - # ## landings - # 'landings.tbl.units' = as.character(landings.tbl.units), - # - # ## 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/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/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index d5ea858c..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 in F.units, 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." -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." -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 ." -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." +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." 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 new file mode 100644 index 00000000..f766e082 --- /dev/null +++ b/inst/resources/key_quantity_template.csv @@ -0,0 +1,133 @@ +key_quantity,value,meaning,dependent_on_other_kq,dependent_kq,dependent_on_other_fxn_args,fxn_with_other_args +B.BMSY.end.yr,,biomass relative to BMSY in the final year,,,, +B.end.year,,final year of the biomass time series,,,, +B.max,,maximum biomass,,,, +B.min,,minimum biomass,,,, +B.ref.pt,,biomass limit reference point,,,, +B.start.year,,start year of the biomass time series,,,, +B.units,,units of biomass measurement,,,, +Bend,,biomass at the end of the time series,,,, +Bmsy,,biomass at maximum sustainable yield,,,, +Btarg,,target biomass reference point,,,, +caa.age.max,,maximum age in catch-at-age data,,,, +caa.age.min,,minimum age in catch-at-age data,,,, +caa.end.year,,final year for catch-at-age data,,,, +caa.start.year,,start year for catch-at-age data,,,, +cal.length.max,,maximum length in catch-at-length histograms,,,, +cal.length.min,,minimum length in catch-at-length histograms,,,, +cpue.end.year,,final year of the CPUE time series,,,, +cpue.max,,maximum catch per unit effort,,,, +cpue.min,,minimum catch per unit effort,,,, +cpue.start.year,,start year of the CPUE time series,,,, +cpue.units,,units for catch per unit effort,,,, +F.FMSY.end.yr,,fishing mortality relative to FMSY in the final year,,,, +F.end.year,,final year of the fishing mortality time series,,,, +F.max,,maximum fishing mortality,,,, +F.min,,minimum fishing mortality,,,, +F.ref.pt,,fishing mortality limit reference point,,,, +F.start.year,,start year of the fishing mortality time series,,,, +fecundity.length.max,,maximum fish length for fecundity relationship,,,, +fecundity.length.min,,minimum fish length for fecundity relationship,,,, +fecundity.length.units,,length units for fecundity measurement,,,, +fecundity.max,,maximum observed fecundity,,,, +fecundity.min,,minimum observed fecundity,,,, +fecundity.units,,units for fecundity (number of eggs),,,, +fork.length.max,,maximum fork length,,,, +fork.length.min,,minimum fork length,,,, +fork.length.units,,units for fork length,,,, +Ftarg,,target fishing mortality reference point,,,, +kobe.end.year,,final year displayed on the Kobe plot,,,, +landings.end.year,,final year of historical landings data,,,, +landings.max,,maximum historical landings,,,, +landings.min,,minimum historical landings,,,, +landings.start.year,,start year of historical landings data,,,, +landings.units,,units for landings,,,, +M.age.max,,maximum age for natural mortality,,,, +M.age.min,,minimum age for natural mortality,,,, +M.rate.max,,maximum natural mortality rate,,,, +M.rate.min,,minimum natural mortality rate,,,, +mod.fit.abun.end.year,,final year for model fit to abundance index,,,, +mod.fit.abun.start.year,,start year for model fit to abundance index,,,, +mod.fit.catch.end.year,,final year for model fit to catch data,,,, +mod.fit.catch.max,,maximum model estimated catch,,,, +mod.fit.catch.min,,minimum model estimated catch,,,, +mod.fit.catch.start.year,,start year for model fit to catch data,,,, +mod.fit.catch.units,,units for model estimated catch,,,, +mod.fit.discards.end.year,,final year for model fit to discards,,,, +mod.fit.discards.max,,maximum estimated discards,,,, +mod.fit.discards.min,,minimum estimated discards,,,, +mod.fit.discards.start.year,,start year for model fit to discards,,,, +mod.fit.discards.units,,units for estimated discards,,,, +overfished.status.is.isnot,,indicator of overfished status,,,, +overfishing.status.is.isnot,,indicator of overfishing status,,,, +pop.baa.age.max,,maximum age for biomass-at-age data,,,, +pop.baa.age.min,,minimum age for biomass-at-age data,,,, +pop.baa.end.year,,final year for biomass-at-age data,,,, +pop.baa.fish.max,,maximum bubble size for biomass-at-age,,,, +pop.baa.fish.min,,minimum bubble size for biomass-at-age,,,, +pop.baa.start.year,,start year for biomass-at-age data,,,, +pop.naa.age.max,,maximum age for numbers-at-age data,,,, +pop.naa.age.min,,minimum age for numbers-at-age data,,,, +pop.naa.end.year,,final year for numbers-at-age data,,,, +pop.naa.fish.max,,maximum bubble size for numbers-at-age,,,, +pop.naa.fish.min,,minimum bubble size for numbers-at-age,,,, +pop.naa.start.year,,start year for numbers-at-age data,,,, +proj.catch.end.year,,final year of catch projections,,,, +proj.catch.min,,minimum forecasted catch,,,, +proj.catch.start.year,,start year of catch projections,,,, +proj.catch.units,,units for projected catch,,,, +prop.mat.length.max,,maximum length for maturity schedule,,,, +prop.mat.length.min,,minimum length for maturity schedule,,,, +prop.mat.length.units,,length units for maturity data,,,, +R0,,unfished equilibrium recruitment,,,, +recruit.dev.end.year,,final year of recruitment deviations,,,, +recruit.dev.max,,maximum recruitment deviation,,,, +recruit.dev.min,,minimum recruitment deviation,,,, +recruit.dev.start.year,,start year of recruitment deviations,,,, +recruitment.end.year,,final year of estimated recruitment,,,, +recruitment.max,,maximum estimated recruitment,,,, +recruitment.min,,minimum estimated recruitment,,,, +recruitment.start.year,,start year of estimated recruitment,,,, +recruitment.units,,units for recruitment,,,, +rel.B.max,,maximum relative biomass,,,, +rel.B.min,,minimum relative biomass,,,, +rel.F.max,,maximum relative fishing mortality,,,, +rel.F.min,,minimum relative fishing mortality,,,, +rel.recruitment.max,,maximum relative recruitment (R/R0),,,, +rel.recruitment.min,,minimum relative recruitment (R/R0),,,, +rel.ssb.max,,maximum relative spawning stock biomass,,,, +rel.ssb.min,,minimum relative spawning stock biomass,,,, +selectivity.end.year,,final year for selectivity estimates,,,, +selectivity.length.max,,maximum length for gear selectivity,,,, +selectivity.length.min,,minimum length for gear selectivity,,,, +selectivity.length.units,,units for selectivity length,,,, +selectivity.start.year,,start year for selectivity estimates,,,, +spr.max,,maximum spawning potential ratio,,,, +spr.min,,minimum spawning potential ratio,,,, +spr.ref.pt,,spawning potential ratio reference point,,,, +sr.age.min,,age of newly recruited fish in SR model,,,, +sr.ssb.max,,maximum SSB in stock-recruit relationship,,,, +sr.ssb.min,,minimum SSB in stock-recruit relationship,,,, +sr.ssb.units,,units for SSB in stock-recruit relationship,,,, +ssb.end.year,,final year of the SSB time series,,,, +ssb.max,,maximum spawning stock biomass,,,, +ssb.min,,minimum spawning stock biomass,,,, +ssb.ref.pt,,spawning stock biomass reference point,,,, +ssb.start.year,,start year of the SSB time series,,,, +ssb.units,,units for spawning stock biomass,,,, +tot.catch.max,,maximum total catch,,,, +tot.catch.min,,minimum total catch,,,, +total.length.max,,maximum total length,,,, +total.length.min,,minimum total length,,,, +total.length.units,,units for total length,,,, +vonb.age.max,,maximum age for von Bertalanffy growth,,,, +vonb.age.min,,minimum age for von Bertalanffy growth,,,, +vonb.length.max,,maximum length for von Bertalanffy growth,,,, +vonb.length.min,,minimum length for von Bertalanffy growth,,,, +vonb.length.units,,units for length in growth model,,,, +wl.length.max,,maximum length for weight-length relationship,,,, +wl.length.min,,minimum length for weight-length relationship,,,, +wl.length.units,,length units for weight-length conversion,,,, +wl.weight.max,,maximum weight for weight-length relationship,,,, +wl.weight.min,,minimum weight for weight-length relationship,,,, +wl.weight.units,,weight units for weight-length conversion,,,, \ No newline at end of file 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) +} +} 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(), ... ) } 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/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 -) -} -} 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)) -}) 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.