diff --git a/R/plot_abundance_at_age.R b/R/plot_abundance_at_age.R index 87d6238b..24121a52 100644 --- a/R/plot_abundance_at_age.R +++ b/R/plot_abundance_at_age.R @@ -97,7 +97,7 @@ plot_abundance_at_age <- function( ggplot2::ggplot() ) } - + data <- data |> dplyr::mutate(age = as.numeric(age)) @@ -114,7 +114,6 @@ 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) @@ -122,23 +121,27 @@ plot_abundance_at_age <- function( 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) - + 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) - + 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 9619f10c..8740f4dd 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -148,8 +148,7 @@ plot_biomass <- function( ### Make RDA ---- if (make_rda) { - - if (relative){ + 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() |> @@ -159,36 +158,39 @@ plot_biomass <- function( 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) - + + 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 8f3a6fde..9162397d 100644 --- a/R/plot_biomass_at_age.R +++ b/R/plot_biomass_at_age.R @@ -84,7 +84,7 @@ plot_biomass_at_age <- function( data <- data |> dplyr::mutate(age = as.numeric(age)) - + # Plot data plot <- plot_aa( dat = data, @@ -98,7 +98,6 @@ 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) @@ -106,23 +105,27 @@ plot_biomass_at_age <- function( 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) - + 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) - + 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 4a5b08d3..f4998375 100644 --- a/R/plot_catch_comp.R +++ b/R/plot_catch_comp.R @@ -100,10 +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, @@ -115,7 +115,6 @@ 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) @@ -123,23 +122,27 @@ plot_catch_comp <- function( 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) - + 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) - + 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 8791455d..61fc5b5d 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -95,9 +95,8 @@ plot_fishing_mortality <- function( ### Make RDA ---- if (make_rda) { - - if (relative){ - # Obtain relevant key quantities for captions/alt text + 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() |> @@ -109,33 +108,36 @@ plot_fishing_mortality <- function( 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) - + + 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 645bde3c..c408467e 100644 --- a/R/plot_indices.R +++ b/R/plot_indices.R @@ -114,30 +114,34 @@ 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) + "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) - + 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) - + 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 aed3e76b..52baba98 100644 --- a/R/plot_landings.R +++ b/R/plot_landings.R @@ -99,28 +99,31 @@ 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 - + landings.units <- unit_label + # calculate & export key quantities - export_kqs(landings.start.year, - landings.end.year, - landings.min, - landings.max, - landings.units) - + 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) - + 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 ebf2fe96..c76fa153 100644 --- a/R/plot_natural_mortality.R +++ b/R/plot_natural_mortality.R @@ -84,7 +84,7 @@ plot_natural_mortality <- function( processed_data <- processed_data |> dplyr::mutate(age = as.numeric(age)) - + plt <- plot_timeseries( dat = processed_data, x = "age", @@ -100,7 +100,6 @@ 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) @@ -108,17 +107,21 @@ plot_natural_mortality <- function( 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) - + 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) - + 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 80bf4fc5..6bb7ce47 100644 --- a/R/plot_recruitment.R +++ b/R/plot_recruitment.R @@ -145,7 +145,6 @@ 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) @@ -154,20 +153,24 @@ plot_recruitment <- function( 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) - + 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) - - + 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 ed6e90ae..cf0ab577 100644 --- a/R/plot_recruitment_deviations.R +++ b/R/plot_recruitment_deviations.R @@ -100,25 +100,29 @@ 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) - + 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) - + 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 9448f129..b391e46b 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -235,8 +235,7 @@ plot_spawning_biomass <- function( ### Make RDA ---- if (make_rda) { - - if (relative){ + 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() |> @@ -248,39 +247,42 @@ plot_spawning_biomass <- function( 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) - + 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) - + 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 39b2bf40..3f13546d 100644 --- a/R/plot_stock_recruitment.R +++ b/R/plot_stock_recruitment.R @@ -127,7 +127,6 @@ 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)) |> @@ -139,26 +138,30 @@ plot_stock_recruitment <- function( 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 - + 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) - + 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) - + 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 cee42eaa..889f3164 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", "across", "everything", "fleet_name", "name", "value_new", "y", "." + "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_landings.R b/R/table_landings.R index 40d90a59..d1956794 100644 --- a/R/table_landings.R +++ b/R/table_landings.R @@ -112,16 +112,15 @@ 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 - + 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 aa4ec1f8..513cabb1 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -13,7 +13,7 @@ #' @param ... Key quantity objects whose values will be added to the output #' dataframe #' -#' @returns Dataframe based on key quantities template that contains +#' @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()]. #' @@ -21,18 +21,18 @@ #' fill_in_kqs( #' df = data, #' F.min, -#' F.max) +#' 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)) |> @@ -40,7 +40,8 @@ fill_in_kqs <- function(df, ...) { dplyr::mutate(value = dplyr::if_else( (is.na(value) | value == "" & !is.na(value_new)), value_new, - value)) |> + value + )) |> dplyr::select(-value_new) } @@ -50,16 +51,16 @@ fill_in_kqs <- function(df, ...) { #' 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 +#' 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) +#' 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) @@ -69,17 +70,18 @@ export_kqs <- function(...) { 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, - ...) - + 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 @@ -87,14 +89,15 @@ export_kqs <- function(...) { #' @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 +#' @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) +#' F.max +#' ) #' } insert_kqs <- function(...) { if (file.exists(fs::path(getwd(), "captions_alt_text.csv"))) { @@ -105,58 +108,58 @@ insert_kqs <- function(...) { 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") - + # 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() |> @@ -170,7 +173,7 @@ insert_kqs <- function(...) { name = stringr::str_remove_all(name, "^\\(\\?\\ 1, then select b_target +## kobe plot- don't code quantities yet +# kobe.end.year <- +# value of B/B(MSY) at the end year +# B.BMSY.end.yr <- B/BMSY +# --B = time series of biomass, last year +# --BMSY = dplyr::filter(grepl('b_target', label) | grepl('b_msy', label) | c(grepl('fishing_mortality_msy', label) & is.na(year))) +# CHECK: if length > 1, then select b_target - # value of F/F(MSY) at the end year - # F.FMSY.end.yr <- +# value of F/F(MSY) at the end year +# F.FMSY.end.yr <- - # object that should be "is" or "is not" and answers the question, - # "the stock overfishing status ... overfished" - # overfished.status.is.isnot <- +# object that should be "is" or "is not" and answers the question, +# "the stock overfishing status ... overfished" +# overfished.status.is.isnot <- - # object that should be "is" or "is not" and answers the question, - # "the stock ... experiencing overfishing" - # overfishing.status.is.isnot <- +# object that should be "is" or "is not" and answers the question, +# "the stock ... experiencing overfishing" +# overfishing.status.is.isnot <- - # TODO: uncomment and recode once we get clarity about how to extract this value properly - # R0 - # R0 <- dat |> - # dplyr::filter( - # # pull from BAM - # grepl('^recruitment$', label) & module_name == "parms" | - # grepl('^R0', label) | - # # pull from SS3 - # grepl('recruitment_virgin', label) - # ) |> - # dplyr::pull(estimate) |> - # unique() |> - # as.numeric() |> - # round(digits = 2) +# TODO: uncomment and recode once we get clarity about how to extract this value properly +# R0 +# R0 <- dat |> +# dplyr::filter( +# # pull from BAM +# grepl('^recruitment$', label) & module_name == "parms" | +# grepl('^R0', label) | +# # pull from SS3 +# grepl('recruitment_virgin', label) +# ) |> +# dplyr::pull(estimate) |> +# unique() |> +# as.numeric() |> +# round(digits = 2) - # Bend <- +# Bend <- - # TODO: uncomment and recode once we get clarity about how to extract this value properly - # Target biomass - # Btarg <- dat |> - # dplyr::filter(c(grepl('biomass', label) & grepl('target', label) & estimate >1) | label == 'biomass_msy') |> - # dplyr::pull(estimate) |> - # as.numeric() |> - # round(digits = 2) +# TODO: uncomment and recode once we get clarity about how to extract this value properly +# Target biomass +# Btarg <- dat |> +# dplyr::filter(c(grepl('biomass', label) & grepl('target', label) & estimate >1) | label == 'biomass_msy') |> +# dplyr::pull(estimate) |> +# as.numeric() |> +# round(digits = 2) - # Bmsy <- +# Bmsy <- - ## 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 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 +# vonB length units (plural) +# vonb.length.units - # vonb.length.min <- # minimum vonB length - # vonb.length.max <- # minimum vonB length +# vonb.length.min <- # minimum vonB length +# vonb.length.max <- # minimum vonB length - ## length-type conversion plot- don't code quantities yet - # total length units (plural) - # total.length.units +## length-type conversion plot- don't code quantities yet +# total length units (plural) +# total.length.units - # total.length.min <- # minimum total length - # total.length.max <- # maximum total length - # fork length units (plural) - # fork.length.units +# total.length.min <- # minimum total length +# total.length.max <- # maximum total length +# fork length units (plural) +# fork.length.units - # fork.length.min <- # minimum fork length - # fork.length.max <- # maximum fork length - - - ## weight-length conversion plot- don't code quantities yet - # length units (plural) - # wl.length.units - - # wl.length.min <- # minimum length - # wl.length.max <- # maximum length - - # weight units (plural) - # wl.weight.units - - # wl.weight.min <- # minimum weight - # wl.weight.max <- # maximum weight - - - ## maturity schedule (proportion mature)- don't code quantities yet - # length units (plural) - # prop.mat.length.units - - # prop.mat.length.min <- # minimum length - # prop.mat.length.max <- # maximum length - - - ## fecundity at length- don't code quantities yet - # length units (plural) - # fecundity.length.units - - # fecundity.length.min <- # minimum length - # fecundity.length.max <- # maximum length - - # fecundity units (plural) - # fecundity.units - - # fecundity.min <- # minimum fecundity - # fecundity.max <- # maximum fecundity - - ## 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 - - ## 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 - - # mod.fit.catch.min <- # minimum catch - # mod.fit.catch.max <- # maximum catch - - - ## mod_fit_abun (model fit to abundance indices plot)- don't code quantities yet - # start year of model fit to abundance indices plot - # mod.fit.abun.start.year <- - - # end year of model fit to abundance indices plot - # mod.fit.abun.end.year <- - - ## mod_fit_discards- will be by fleet - ## for ss3, obs discards not in output file - ## -filter labels as discard_observed | discard_predicted | discard - ## -then group_by(year, fleet, label), then summarize(estimate_y = sum(estimate)) - ## ---then, get the following: - ## -change alt text so that we add the line's min/max, but analyst has to describe further - # mod.fit.discards.start.year <- # start year of model fit to discards plot - # mod.fit.discards.end.year <- # end year of F - # mod.fit.discards.units <- # discards units (plural) - # mod.fit.discards.min <- # minimum discards - # mod.fit.discards.max <- # maximum discards - - ## selectivity- don't code quantities yet - # selectivity.start.year <- # start year of selectivity plot - # selectivity.end.year <- # end year of selectivity plot - # selectivity.length.units <- # length units (plural) - # selectivity.length.min <- # minimum length - # selectivity.length.max <- # maximum length - - ## tot_b (total biomass): same as B plot above - - # 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) - - # TODO: uncomment and recode once we get clarity about how to extract this value properly - # spr reference point - # spr.ref.pt <- dat |> - # dplyr::filter(label == "spr_msy") |> - # dplyr::select(estimate) |> - # as.numeric()# |> - # round(digits = 2) - - - ## proj_catch (projected catch) - # projected catch units (plural) - # proj.catch.units <- # probably mt, but wait until figure coded - - # start year of projected catch plot - # 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() - - # 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() - - # 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() - - # TABLES----- - - ## catch - # catch.fleet <- # fleet - - ## discards - # discards.tbl.units <- # discards units - - ## catchability - # catchability.fleet <- # fleet +# fork.length.min <- # minimum fork length +# fork.length.max <- # maximum fork length + + +## weight-length conversion plot- don't code quantities yet +# length units (plural) +# wl.length.units + +# wl.length.min <- # minimum length +# wl.length.max <- # maximum length + +# weight units (plural) +# wl.weight.units + +# wl.weight.min <- # minimum weight +# wl.weight.max <- # maximum weight + + +## maturity schedule (proportion mature)- don't code quantities yet +# length units (plural) +# prop.mat.length.units + +# prop.mat.length.min <- # minimum length +# prop.mat.length.max <- # maximum length + + +## fecundity at length- don't code quantities yet +# length units (plural) +# fecundity.length.units + +# fecundity.length.min <- # minimum length +# fecundity.length.max <- # maximum length + +# fecundity units (plural) +# fecundity.units + +# fecundity.min <- # minimum fecundity +# fecundity.max <- # maximum fecundity + +## 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 + +## 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 + +# mod.fit.catch.min <- # minimum catch +# mod.fit.catch.max <- # maximum catch + + +## mod_fit_abun (model fit to abundance indices plot)- don't code quantities yet +# start year of model fit to abundance indices plot +# mod.fit.abun.start.year <- + +# end year of model fit to abundance indices plot +# mod.fit.abun.end.year <- + +## mod_fit_discards- will be by fleet +## for ss3, obs discards not in output file +## -filter labels as discard_observed | discard_predicted | discard +## -then group_by(year, fleet, label), then summarize(estimate_y = sum(estimate)) +## ---then, get the following: +## -change alt text so that we add the line's min/max, but analyst has to describe further +# mod.fit.discards.start.year <- # start year of model fit to discards plot +# mod.fit.discards.end.year <- # end year of F +# mod.fit.discards.units <- # discards units (plural) +# mod.fit.discards.min <- # minimum discards +# mod.fit.discards.max <- # maximum discards + +## selectivity- don't code quantities yet +# selectivity.start.year <- # start year of selectivity plot +# selectivity.end.year <- # end year of selectivity plot +# selectivity.length.units <- # length units (plural) +# selectivity.length.min <- # minimum length +# selectivity.length.max <- # maximum length + +## tot_b (total biomass): same as B plot above + +# 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) + +# TODO: uncomment and recode once we get clarity about how to extract this value properly +# spr reference point +# spr.ref.pt <- dat |> +# dplyr::filter(label == "spr_msy") |> +# dplyr::select(estimate) |> +# as.numeric()# |> +# round(digits = 2) + + +## proj_catch (projected catch) +# projected catch units (plural) +# proj.catch.units <- # probably mt, but wait until figure coded + +# start year of projected catch plot +# 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() + +# 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() + +# 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() + +# TABLES----- + +## catch +# catch.fleet <- # fleet + +## discards +# discards.tbl.units <- # discards units + +## catchability +# catchability.fleet <- # fleet #------------------------------------------------------------------------------ diff --git a/man/export_kqs.Rd b/man/export_kqs.Rd index 0df5d304..7c1826ab 100644 --- a/man/export_kqs.Rd +++ b/man/export_kqs.Rd @@ -12,7 +12,7 @@ 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 +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{ @@ -22,6 +22,7 @@ Export updated key quantities template \dontrun{ export_kqs( F.min, - F.max) + F.max +) } } diff --git a/man/fill_in_kqs.Rd b/man/fill_in_kqs.Rd index dbccc2f4..7990ac20 100644 --- a/man/fill_in_kqs.Rd +++ b/man/fill_in_kqs.Rd @@ -15,7 +15,7 @@ quantity names and other associated information} dataframe} } \value{ -Dataframe based on key quantities template that contains +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()]. } @@ -27,6 +27,7 @@ Substitute key quantities' values into template fill_in_kqs( df = data, F.min, - F.max) + F.max +) } } diff --git a/man/insert_kqs.Rd b/man/insert_kqs.Rd index 22c53495..dc42a30d 100644 --- a/man/insert_kqs.Rd +++ b/man/insert_kqs.Rd @@ -11,7 +11,7 @@ insert_kqs(...) exported "captions_alt_text.csv"} } \value{ -Exports a file ("captions_alt_text.csv") containing captions +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. } @@ -22,6 +22,7 @@ Insert key quantities into the captions and alternative text file \dontrun{ insert_kqs( F.min, - F.max) + F.max +) } } diff --git a/tests/testthat/test-export_rda.R b/tests/testthat/test-export_rda.R index cd543048..03b49866 100644 --- a/tests/testthat/test-export_rda.R +++ b/tests/testthat/test-export_rda.R @@ -10,7 +10,7 @@ test_that("export_rda works for figures", { B.min <- 100 B.max <- 200 - + # add KQs to caps/alt text csv insert_kqs(B.min, B.max) @@ -52,7 +52,7 @@ test_that("export_rda works for tables", { B.min <- 100 B.max <- 200 - + # add KQs to caps/alt text csv insert_kqs(B.min, B.max) diff --git a/tests/testthat/test-extract_caps_alttext.R b/tests/testthat/test-extract_caps_alttext.R index df29d97a..116a78fc 100644 --- a/tests/testthat/test-extract_caps_alttext.R +++ b/tests/testthat/test-extract_caps_alttext.R @@ -10,7 +10,7 @@ test_that("extract_caps_alttext works for figures", { B.min <- 100 B.max <- 200 - + # add KQs to caps/alt text csv insert_kqs(B.min, B.max) @@ -41,7 +41,7 @@ test_that("extract_caps_alttext works for tables", { B.min <- 100 B.max <- 200 - + # add KQs to caps/alt text csv insert_kqs(B.min, B.max)