From ddb7c8de1291001a4623fc707ec76fa1a64c1afe Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 10 Jan 2025 14:07:41 -0500 Subject: [PATCH 01/54] first commit --- R/utils.R | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/R/utils.R b/R/utils.R index b450780..3973d09 100644 --- a/R/utils.R +++ b/R/utils.R @@ -65,3 +65,49 @@ export_rda <- function(final = final, "rda_files", paste0(topic_label, "_", fig_or_table, ".rda"))) } + +# substitute in more key quantities (units and end_years) to captions/alt text +add_yr_units <- function( + topic = topic_label, + fig_or_table = fig_or_table, + end_year = NULL, + units = NULL){ + + # import csv + caps_alt_df <- read.csv(fs::path(getwd(), "captions_alt_text.csv")) + + # make year character if not null + if (!is.null(end_year)){ + end_year <- as.character(end_year) + } + + # select specific fig/table's caption/alt text + topic_cap_alt <- caps_alt_df |> + dplyr::filter(label == topic, + type == fig_or_table) + + # select caption + topic_cap <- topic_cap_alt |> + dplyr::select(caption) + + # select alt text + topic_alt <- topic_cap_alt |> + dplyr::select(alt_text) + + # replace placeholders in alt text + ## end_year + # if "end.year" is found in topic_alt, replace it with end_year + topic_alt <- sub("\\S*end\\.year\\S*", + as.character(end_year), + topic_alt) + + ## units + topic_alt <- sub("\\S*units\\S*", + as.character(units), + topic_alt) + + + + + } + From fa94b09d158631db6e4bf6cf25ce60dc352dd601 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 10:00:52 -0500 Subject: [PATCH 02/54] added period after end.year in template csv so that period is preserved after replace function is run --- inst/resources/captions_alt_text_template.csv | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index 7f6b44e..b6d87a7 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -1,9 +1,9 @@ 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 from kobe.start.year to kobe.end.year. The x axis shows overfished status (i.e., B/B~MSY~), which spans from B.BMSY.min to B.BMSY.max. The y axis shows overfishing (i.e., F/F~MSY~), which spans from F.FMSY.min to F.FMSY.max. The B/B~MSY~ and F/F~MSY~ for end.year.kobe 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.ref.pt.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), where B(reference point) is B.ref.point B.ref.pt.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." -fishing.mortality,figure,Fishing mortality (F) over time. The horizontal dashed line represents the target reference point (F.ref.pt F.ref.pt.unit).,"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." -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." +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 from kobe.start.year to kobe.end.year . The x axis shows overfished status (i.e., B/B~MSY~), which spans from B.BMSY.min to B.BMSY.max. The y axis shows overfishing (i.e., F/F~MSY~), which spans from F.FMSY.min to F.FMSY.max. The B/B~MSY~ and F/F~MSY~ for end.year .kobe 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.ref.pt.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), where B(reference point) is B.ref.point B.ref.pt.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." +fishing.mortality,figure,Fishing mortality (F) over time. The horizontal dashed line represents the target reference point (F.ref.pt F.ref.pt.unit).,"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." +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 M.units, 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 vonb.age.units, 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." @@ -12,23 +12,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." CAA,figure,"Histograms showing the proportion of the catch in each age group for each year in 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 age group. The x axis of each histogram shows age groups that span from caa.age.min to caa.age.max. The y axis of each histogram shows the proportion of the catch and spans from 0--1." 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." -CPUE.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 for fleet or survey from cpue.start.year to cpue.end.year, 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." -NAA,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 bubble.start.year to bubble.end.year. The y axis shows age in bubble.age.units, which spans from bubble.age.min to bubble.age.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 model-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." +CPUE.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 for fleet or survey from cpue.start.year to cpue.end.year, 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." +NAA,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 bubble.start.year to bubble.end.year . The y axis shows age in bubble.age.units, which spans from bubble.age.min to bubble.age.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 model-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." sr,figure,Stock recruitment relationship estimated by the assessment model. ,"Point and line graph showing the relationship between spawning 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 spawning stock biomass, after adjusted by annual recruitment deviations. The line represents the best fit through the points for the spawning 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, while 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 sr.units for each year. The x axis shows years, 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.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,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at biomass.ref.pt biomass.ref.pt.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from biomass.start.year to biomass.end.year. The y axis shows model-estimated biomass in biomass.units, which spans from biomass.min to biomass.max." -spawning.biomass,figure,Model-estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point at ssb.ref.pt ssb.ref.pt.units.,"Line graph showing model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year. The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." -relative.spawning.biomass,figure,"Model-estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point), where SSB(reference point) is ssb.ref.pt ssb.ref.pt.units.","Line graph showing model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." -spr,figure,Model-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 spr.ref.pt.units.,"Line graph showing model-estimated spawning potential ratio over time. The x axis shows the year, which spans from spr.start.year to spr.end.year. The y axis shows model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." -pop.naa.baa,figure,Model-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 model-estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.naa.baa.start.year to pop.naa.baa.end.year. The y axis shows numbers of fish, which spans from pop.naa.baa.fish.min to pop.naa.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,figure,Forecasted biomass in proj.biomass.units over future years for different fishing mortality scenarios as indicated in the legend. The horizontal dashed line represents the biomass limit reference point. ,"Time series line graph showing forecasted biomass over future years for different fishing mortality scenarios. The horizontal dashed line represents the biomass limit reference point. The x axis shows the year, which spans from proj.biomass.start.year to proj.biomass.end.year. The y axis shows biomass in proj.biomass.units, which spans from proj.biomass.min to proj.biomass.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.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,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at biomass.ref.pt biomass.ref.pt.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from biomass.start.year to biomass.end.year . The y axis shows model-estimated biomass in biomass.units, which spans from biomass.min to biomass.max." +spawning.biomass,figure,Model-estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point at ssb.ref.pt ssb.ref.pt.units.,"Line graph showing model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." +relative.spawning.biomass,figure,"Model-estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point), where SSB(reference point) is ssb.ref.pt ssb.ref.pt.units.","Line graph showing model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." +spr,figure,Model-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 spr.ref.pt.units.,"Line graph showing model-estimated spawning potential ratio over time. The x axis shows the year, which spans from spr.start.year to spr.end.year . The y axis shows model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." +pop.naa.baa,figure,Model-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 model-estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.naa.baa.start.year to pop.naa.baa.end.year . The y axis shows numbers of fish, which spans from pop.naa.baa.fish.min to pop.naa.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,figure,Forecasted biomass in proj.biomass.units over future years for different fishing mortality scenarios as indicated in the legend. The horizontal dashed line represents the biomass limit reference point. ,"Time series line graph showing forecasted biomass over future years for different fishing mortality scenarios. The horizontal dashed line represents the biomass limit reference point. The x axis shows the year, which spans from proj.biomass.start.year to proj.biomass.end.year . The y axis shows biomass in proj.biomass.units, which spans from proj.biomass.min to proj.biomass.max." 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. , catch,table,Observed (points) and model estimated (line) catch over time for fleet catch.fleet., From eb179687bde7f5b5989d51e3856c75763a4910c7 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 12:09:35 -0500 Subject: [PATCH 03/54] Added notes and edits from Friday's coworking meeting. Notes explain how to extract more key quantities from model results. Some key quantities were deleted. Alt text/caps csv updated in accordance with changes. --- R/write_captions.R | 115 +++++++++++------- inst/resources/captions_alt_text_template.csv | 18 +-- 2 files changed, 77 insertions(+), 56 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 3215258..309ed5d 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -33,21 +33,28 @@ write_captions <- function(dat, # converted model output object # FIGURES----- - ## kobe plot - # B.BMSY.min <- # minimum value of B/B(MSY) - # B.BMSY.max <- # maximum value of B/B(MSY) - # F.FMSY.min <- # minimum value of F/F(MSY) - # F.FMSY.max <- # maximum value of F/F(MSY) - # B.BMSY.end.yr <- # value of B/B(MSY) at the end year - # F.FMSY.end.yr <- # value of F/F(MSY) at the end year - # overfished.status.is.isnot <- # object that should be "is" or "is not" and answers the question, "the stock overfishing status ... overfished" - # overfishing.status.is.isnot <- # object that should be "is" or "is not" and answers the question, "the stock ... experiencing overfishing" - # kobe.start.year <- # start year of kobe plot - # kobe.end.year <- # end year of kobe plot + ## 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 <- + + # 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 <- + ## Biomass plot - # B.ref.pt <- # biomass reference point- SHARED with kobe plot, above - # B.ref.pt.units <- # biomass reference point unit + # B.ref.pt <- # biomass reference point # start year of biomass plot B.start.year <- dat |> @@ -123,14 +130,24 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot # F.ref.pt <- # F reference point - # F.ref.pt.units <- # F reference point unit + # --(use to_lower): F_targ if it's present. if not, use F_proxy; if not, Fmsy (these are labels in dat) # F.start.year. <- # start year of F plot - # F.end.year <- # start year of F plot - # F.units <- # units of F (plural) + # --like Fend, but minimum year + # F.end.year <- # end year of F plot + # --Fend # F.min <- # minimum F + # --if age = na, then take min(time series) + # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) |> + # pull(min(val)) # F.max <- # maximum F + # --if age = na, then take max(time series) + # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) + # pull(max(val)) # Ftarg <- + # --Ftarg # F.Ftarg <- + # --F_Ftarg + ## landings plot @@ -200,20 +217,22 @@ write_captions <- function(dat, # converted model output object ## natural mortality (M) # M.age.min <- # minimum age of M + # --if age != na, min(age) # M.age.max <- # maximum age of M - # M.units <- # units of M (plural) + # --if age != na, max(age) # M.rate.min <- # minimum M rate + # -label = natural_mortality (min); est in est col # M.rate.max <- # maximum M rate + # -label = natural_mortality (min); est in est col - ## vonB LAA (von Bertalanffy growth function + length at age) - # vonb.age.units <- # vonB age units (plural) + ## 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 <- # vonB length units (plural) # vonb.length.min <- # minimum vonB length # vonb.length.max <- # minimum vonB length - ## length-type conversion plot + ## length-type conversion plot- don't code quantities yet # total.length.units <- # total length units (plural) # total.length.min <- # minimum total length # total.length.max <- # maximum total length @@ -221,7 +240,7 @@ write_captions <- function(dat, # converted model output object # fork.length.min <- # minimum fork length # fork.length.max <- # maximum fork length - ## weight-length conversion plot + ## weight-length conversion plot- don't code quantities yet # wl.length.units <- # length units (plural) # wl.length.min <- # minimum length # wl.length.max <- # maximum length @@ -229,12 +248,12 @@ write_captions <- function(dat, # converted model output object # wl.weight.min <- # minimum weight # wl.weight.max <- # maximum weight - ## maturity schedule (proportion mature) + ## maturity schedule (proportion mature)- don't code quantities yet # prop.mat.length.units <- # length units (plural) # prop.mat.length.min <- # minimum length # prop.mat.length.max <- # maximum length - ## fecundity at length + ## fecundity at length- don't code quantities yet # fecundity.length.units <- # length units (plural) # fecundity.length.min <- # minimum length # fecundity.length.max <- # maximum length @@ -242,16 +261,16 @@ write_captions <- function(dat, # converted model output object # fecundity.min <- # minimum fecundity # fecundity.max <- # maximum fecundity - ## CAA (catch at age) + ## CAA (catch at age)- don't code quantities yet # fleet.or.survey.name <- # fleet or survey name (SHARED with CAL, below) # caa.age.min <- # minimum age group # caa.age.max <- # maximum age group - ## CAL (catch at length) + ## CAL (catch at length)- don't code quantities yet # cal.length.min <- # minimum length group # cal.length.max <- # maximum length group - ## CPUE indices plot + ## 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 <- # CPUE units (plural) (SHARED with mod.fit.abun, below) @@ -260,12 +279,15 @@ write_captions <- function(dat, # converted model output object ## NAA (numbers at age) # bubble.start.year.min <- # start year of NAA plot + # -label = abundance ; year != na; min(year) # bubble.end.year.max <- # end year of NAA plot - # bubble.age.units <- # age units (plural) + # -label = abundance ; year != na; max(year) # bubble.age.min <- # minimum age + # year != na; min(age) # bubble.age.max <- # maximum age + # year != na; max(age) - ## mod_fit_catch (model fit to catch ts) + ## 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 # mod.fit.catch.units <- # catch units (plural) @@ -276,14 +298,19 @@ write_captions <- function(dat, # converted model output object # mod.fit.abun.start.year <- # start year of model fit to abundance indices plot # mod.fit.abun.end.year <- # end year of model fit to abundance indices plot - ## mod_fit_discards + ## 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 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 + ## 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) @@ -291,9 +318,9 @@ write_captions <- function(dat, # converted model output object # selectivity.length.max <- # maximum length - ## estimated stock recruitment (aka spawning stock biomass) + ## estimated stock recruitment # youngest-age recruited fish (instead of age-0) - # sr.age.min <- # unsure how to extract this + # sr.age.min <- min(age) (usually 1) # ssb units (plural) # sr.ssb.units <- # this will take some thought, since @@ -476,6 +503,7 @@ write_captions <- function(dat, # converted model output object round(digits = 2) ## tot_b (total biomass) + ## -same as B plot above # biomass.start.year <- # start year of biomass plot # biomass.end.year <- # end year of biomass plot # biomass.units <- # biomass units (plural) @@ -560,12 +588,17 @@ write_captions <- function(dat, # converted model output object ## spr (spawning potential ratio) # spr.start.year <- # start year of spr plot + # -same as ssb plot # spr.end.year <- # end year of spr plot + # -same as ssb plot # spr.min <- # minimum spr + # -label = spr (min) & end.year != na ; OR grepl("spr")- could be iffy # spr.max <- # maximum spr + # - " # spr.ref.pt <- # spr reference point - # spr.ref.pt.units <- # spr reference point units + # -spr_msy + #TODO: LEFT OFF HERE (identifying how to extract quantities with Sam, Steve) ## pop_naa_baa (population numbers at age and population biomass at age) # pop.naa.baa.start.year <- # start year of spr plot # pop.naa.baa.end.year <- # end year of spr plot @@ -622,16 +655,11 @@ write_captions <- function(dat, # converted model output object # FIGURES----- ## kobe plot - # 'B.BMSY.min' = as.character(B.BMSY.min), - # 'B.BMSY.max' = as.character(B.BMSY.max), - # 'F.FMSY.min' = as.character(F.FMSY.min), - # 'F.FMSY.max' = as.character(F.FMSY.max), + # '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), - # 'kobe.start.year' = as.character(kobe.start.year), - # 'kobe.end.year' = as.character(kobe.end.year), ## Relative biomass plot # NOTE: moving this above biomass so rel.B.min isn't changed to "rel." + B.min (etc.) @@ -640,7 +668,6 @@ write_captions <- function(dat, # converted model output object ## Biomass plot # 'B.ref.pt' = as.character(B.ref.pt), - # 'B.ref.pt.units' = as.character(B.ref.pt.units), 'B.start.year' = as.character(B.start.year), # 'B.end.year' = as.character(B.end.year), # 'B.units' = as.character(B.units), @@ -653,10 +680,8 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot # 'F.ref.pt' = as.character(F.ref.pt), - # 'F.ref.pt.units' = as.character(F.ref.pt.units), # 'F.start.year' = as.character(F.start.year), # 'F.end.year' = as.character(F.end.year), - # 'F.units' = as.character(F.units), # 'F.min' = as.character(F.min), # 'F.max' = as.character(F.max), # 'Ftarg' = as.character(Ftarg), @@ -672,12 +697,10 @@ write_captions <- function(dat, # converted model output object ## natural mortality (M) # 'M.age.min' = as.character(M.age.min), # 'M.age.max' = as.character(M.age.max), - # 'M.units' = as.character(M.units), # '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.units' = as.character(vonb.age.units), # 'vonb.age.min' = as.character(vonb.age.min), # 'vonb.age.max' = as.character(vonb.age.max), # 'vonb.length.units' = as.character(vonb.length.units), @@ -732,7 +755,6 @@ write_captions <- function(dat, # converted model output object ## NAA (numbers at age) # 'bubble.start.year.min' = as.character(bubble.start.year.min), # 'bubble.end.year.max' = as.character(bubble.end.year.max), - # 'bubble.age.units' = as.character(bubble.age.units), # 'bubble.age.min' = as.character(bubble.age.min), # 'bubble.age.max' = as.character(bubble.age.max), @@ -821,7 +843,6 @@ write_captions <- function(dat, # converted model output object # 'spr.min' = as.character(spr.min), # 'spr.max' = as.character(spr.max), # 'spr.ref.pt' = as.character(spr.ref.pt), - # 'spr.ref.pt.units' = as.character(spr.ref.pt.units), # # ## pop_naa_baa (population numbers at age and population biomass at age) # 'pop.naa.baa.start.year' = as.character(pop.naa.baa.start.year), diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index b6d87a7..1a7d9c7 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -1,11 +1,11 @@ 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 from kobe.start.year to kobe.end.year . The x axis shows overfished status (i.e., B/B~MSY~), which spans from B.BMSY.min to B.BMSY.max. The y axis shows overfishing (i.e., F/F~MSY~), which spans from F.FMSY.min to F.FMSY.max. The B/B~MSY~ and F/F~MSY~ for end.year .kobe 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.ref.pt.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), where B(reference point) is B.ref.point B.ref.pt.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." -fishing.mortality,figure,Fishing mortality (F) over time. The horizontal dashed line represents the target reference point (F.ref.pt F.ref.pt.unit).,"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." +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), where B(reference point) is 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." +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." 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 M.units, 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 vonb.age.units, 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." +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." weight.length.converstion,figure,Weight-length conversion relationship where the points represent individual fish observations and the line is the fitted relationship. ,"Point and line graph showing observations of measured length and measured weight from the same fish for conversion purposes as points, and an exponential fit through those points as a line. The x axis shows length in wl.length.units, which spans from wl.length.min to wl.length.max. The y axis shows weight in wl.weight.units, which spans from wl.weight.min to wl.weight.max." maturity.schedule,figure,The relationship between the proportion mature and fish length. ,"Graph showing the relationship between fish maturity and length. Points, if present, show observations of maturity at different fish lengths and the line represents either the fit to the points, or the assumed or known maturity schedule. The x axis shows length in prop.mat.length.units, which spans from prop.mat.length.min to prop.mat.length.max. The y axis shows the proportion mature, which spans from 0--1." @@ -13,19 +13,19 @@ fecundity.at.length,figure,Fecundity (in number of eggs) as a function of fish l CAA,figure,"Histograms showing the proportion of the catch in each age group for each year in 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 age group. The x axis of each histogram shows age groups that span from caa.age.min to caa.age.max. The y axis of each histogram shows the proportion of the catch and spans from 0--1." 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." CPUE.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 for fleet or survey from cpue.start.year to cpue.end.year, 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." -NAA,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 bubble.start.year to bubble.end.year . The y axis shows age in bubble.age.units, which spans from bubble.age.min to bubble.age.max." +NAA,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 bubble.start.year to bubble.end.year . The y axis shows age in years, which spans from bubble.age.min to bubble.age.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 model-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." -sr,figure,Stock recruitment relationship estimated by the assessment model. ,"Point and line graph showing the relationship between spawning 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 spawning stock biomass, after adjusted by annual recruitment deviations. The line represents the best fit through the points for the spawning 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." +sr,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, while 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 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.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,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at biomass.ref.pt biomass.ref.pt.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from biomass.start.year to biomass.end.year . The y axis shows model-estimated biomass in biomass.units, which spans from biomass.min to biomass.max." spawning.biomass,figure,Model-estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point at ssb.ref.pt ssb.ref.pt.units.,"Line graph showing model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." relative.spawning.biomass,figure,"Model-estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point), where SSB(reference point) is ssb.ref.pt ssb.ref.pt.units.","Line graph showing model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." -spr,figure,Model-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 spr.ref.pt.units.,"Line graph showing model-estimated spawning potential ratio over time. The x axis shows the year, which spans from spr.start.year to spr.end.year . The y axis shows model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." +spr,figure,Model-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 model-estimated spawning potential ratio over time. The x axis shows the year, which spans from spr.start.year to spr.end.year . The y axis shows model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." pop.naa.baa,figure,Model-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 model-estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.naa.baa.start.year to pop.naa.baa.end.year . The y axis shows numbers of fish, which spans from pop.naa.baa.fish.min to pop.naa.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,figure,Forecasted biomass in proj.biomass.units over future years for different fishing mortality scenarios as indicated in the legend. The horizontal dashed line represents the biomass limit reference point. ,"Time series line graph showing forecasted biomass over future years for different fishing mortality scenarios. The horizontal dashed line represents the biomass limit reference point. The x axis shows the year, which spans from proj.biomass.start.year to proj.biomass.end.year . The y axis shows biomass in proj.biomass.units, which spans from proj.biomass.min to proj.biomass.max." From f0911ef46fbc25b9c6158147744a62bfe34b8d1d Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 14:17:19 -0500 Subject: [PATCH 04/54] Small updates to broaden new fxn that inserts more key quantities- including reference points --- R/utils.R | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/R/utils.R b/R/utils.R index 3973d09..1adeb9c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -66,8 +66,8 @@ export_rda <- function(final = final, paste0(topic_label, "_", fig_or_table, ".rda"))) } -# substitute in more key quantities (units and end_years) to captions/alt text -add_yr_units <- function( +# substitute in more key quantities (units, end_years, reference points) to captions/alt text +add_more_key_quants <- function( topic = topic_label, fig_or_table = fig_or_table, end_year = NULL, @@ -87,8 +87,8 @@ add_yr_units <- function( type == fig_or_table) # select caption - topic_cap <- topic_cap_alt |> - dplyr::select(caption) + # topic_cap <- topic_cap_alt |> + # dplyr::select(caption) # select alt text topic_alt <- topic_cap_alt |> @@ -106,7 +106,13 @@ add_yr_units <- function( as.character(units), topic_alt) + ## reference points + ## TODO: ADD HERE + # replace old alt text with new alt text + test <- caps_alt_df[caps_alt_df$type == fig_or_table, + # caps_alt_df$label == topic + ] } From 92e98e00c04e1700a3c2af1a1ee53c5aecc06928 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 14:17:39 -0500 Subject: [PATCH 05/54] Continue updating key quantities in write_captions and alt text/caps csv --- R/write_captions.R | 112 +++++++++++++----- inst/resources/captions_alt_text_template.csv | 2 +- 2 files changed, 86 insertions(+), 28 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 309ed5d..cd5ce14 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -133,8 +133,19 @@ write_captions <- function(dat, # converted model output object # --(use to_lower): F_targ if it's present. if not, use F_proxy; if not, Fmsy (these are labels in dat) # F.start.year. <- # start year of F plot # --like Fend, but minimum year - # F.end.year <- # end year of F plot - # --Fend + + # terminal fishing mortality + # TODO: Put this in utils function, since F.end.year needs end_year + # 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) + # F.min <- # minimum F # --if age = na, then take min(time series) # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) |> @@ -143,10 +154,19 @@ write_captions <- function(dat, # converted model output object # --if age = na, then take max(time series) # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) # pull(max(val)) - # Ftarg <- - # --Ftarg - # F.Ftarg <- - # --F_Ftarg + + # 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 + # TODO: Put this in utils function, since F.end.year needs end_year + # F.Ftarg <- F.end.year / Ftarg ## landings plot @@ -216,11 +236,27 @@ write_captions <- function(dat, # converted model output object round(digits = 2) ## natural mortality (M) - # M.age.min <- # minimum age of M - # --if age != na, min(age) - # M.age.max <- # maximum age of M - # --if age != na, max(age) - # M.rate.min <- # minimum M rate + ## Should this be filtered by label = natural_mortality too? + # minimum age of M + # M.age.min <- dat |> + # dplyr::select(age) |> + # dplyr::filter(!is.na(age)) |> + # dplyr::slice(which.min(age)) |> + # as.numeric() + # + # # maximum age of M + # M.age.max <- dat |> + # dplyr::select(age) |> + # dplyr::filter(!is.na(age)) |> + # dplyr::slice(which.max(age)) |> + # as.numeric() + + # minimum M rate + # The "petrale_sole_std_output.csv" doesn't have a natural_mortality label... + # doesn't work + # M.rate.min <- dat |> + # dplyr::filter( + # grepl("natural_mortality", label)) |> # -label = natural_mortality (min); est in est col # M.rate.max <- # maximum M rate # -label = natural_mortality (min); est in est col @@ -278,14 +314,33 @@ write_captions <- function(dat, # converted model output object # cpue.max <- # maximum CPUE (SHARED with mod_fit_abun, below) ## NAA (numbers at age) - # bubble.start.year.min <- # start year of NAA plot - # -label = abundance ; year != na; min(year) - # bubble.end.year.max <- # end year of NAA plot - # -label = abundance ; year != na; max(year) - # bubble.age.min <- # minimum age - # year != na; min(age) - # bubble.age.max <- # maximum age - # year != na; max(age) + # start year of NAA plot + bubble.start.year.min <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.min(year)) |> + dplyr::select(year) |> + as.numeric() + + # end year of NAA plot + bubble.end.year.max <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.max(year)) |> + dplyr::select(year) |> + as.numeric() + + # minimum age + bubble.age.min <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.min(age)) |> + dplyr::select(age) |> + as.numeric() + + # maximum age + bubble.age.max <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.max(age)) |> + dplyr::select(age) |> + as.numeric() ## 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 @@ -294,9 +349,12 @@ write_captions <- function(dat, # converted model output object # mod.fit.catch.min <- # minimum catch # mod.fit.catch.max <- # maximum catch - ## mod_fit_abun (model fit to abundance indices plot) - # mod.fit.abun.start.year <- # start year of model fit to abundance indices plot - # mod.fit.abun.end.year <- # end year of model fit to abundance indices plot + ## 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 @@ -320,7 +378,11 @@ write_captions <- function(dat, # converted model output object ## estimated stock recruitment # youngest-age recruited fish (instead of age-0) - # sr.age.min <- min(age) (usually 1) + # 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 <- # this will take some thought, since @@ -509,8 +571,6 @@ write_captions <- function(dat, # converted model output object # biomass.units <- # biomass units (plural) # biomass.min <- # minimum biomass # biomass.max <- # maximum biomass - # biomass.ref.pt <- # biomass reference point - # biomass.ref.pt.units <- # biomass reference point units ## spawning_biomass (ssb) # start year of ssb plot @@ -817,8 +877,6 @@ write_captions <- function(dat, # converted model output object # 'biomass.units' = as.character(biomass.units), # 'biomass.min' = as.character(biomass.min), # 'biomass.max' = as.character(biomass.max), - # 'biomass.ref.pt' = as.character(biomass.ref.pt), - # 'biomass.ref.pt.units' = as.character(biomass.ref.pt.units), # relative ssb # NOTE: moving this above recruitment so rel.ssb.min isn't changed to diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index 1a7d9c7..e61e96b 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -22,7 +22,7 @@ sr,figure,Stock recruitment relationship estimated by the assessment model. ,"Po 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, while 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 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.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,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at biomass.ref.pt biomass.ref.pt.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from biomass.start.year to biomass.end.year . The y axis shows model-estimated biomass in biomass.units, which spans from biomass.min to biomass.max." +tot.b,figure,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from biomass.start.year to biomass.end.year . The y axis shows model-estimated biomass in biomass.units, which spans from biomass.min to biomass.max." spawning.biomass,figure,Model-estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point at ssb.ref.pt ssb.ref.pt.units.,"Line graph showing model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." relative.spawning.biomass,figure,"Model-estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point), where SSB(reference point) is ssb.ref.pt ssb.ref.pt.units.","Line graph showing model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." spr,figure,Model-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 model-estimated spawning potential ratio over time. The x axis shows the year, which spans from spr.start.year to spr.end.year . The y axis shows model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." From 34d21f8e444b740bdd6e83f13cdf92b5225afdc6 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 10 Jan 2025 14:07:41 -0500 Subject: [PATCH 06/54] first commit --- R/utils.R | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/R/utils.R b/R/utils.R index b450780..3973d09 100644 --- a/R/utils.R +++ b/R/utils.R @@ -65,3 +65,49 @@ export_rda <- function(final = final, "rda_files", paste0(topic_label, "_", fig_or_table, ".rda"))) } + +# substitute in more key quantities (units and end_years) to captions/alt text +add_yr_units <- function( + topic = topic_label, + fig_or_table = fig_or_table, + end_year = NULL, + units = NULL){ + + # import csv + caps_alt_df <- read.csv(fs::path(getwd(), "captions_alt_text.csv")) + + # make year character if not null + if (!is.null(end_year)){ + end_year <- as.character(end_year) + } + + # select specific fig/table's caption/alt text + topic_cap_alt <- caps_alt_df |> + dplyr::filter(label == topic, + type == fig_or_table) + + # select caption + topic_cap <- topic_cap_alt |> + dplyr::select(caption) + + # select alt text + topic_alt <- topic_cap_alt |> + dplyr::select(alt_text) + + # replace placeholders in alt text + ## end_year + # if "end.year" is found in topic_alt, replace it with end_year + topic_alt <- sub("\\S*end\\.year\\S*", + as.character(end_year), + topic_alt) + + ## units + topic_alt <- sub("\\S*units\\S*", + as.character(units), + topic_alt) + + + + + } + From 231c14271ef0cbc59b52543f54b406f399857735 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 12:09:35 -0500 Subject: [PATCH 07/54] Added notes and edits from Friday's coworking meeting. Notes explain how to extract more key quantities from model results. Some key quantities were deleted. Alt text/caps csv updated in accordance with changes. --- R/write_captions.R | 115 +++++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 47 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 3215258..309ed5d 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -33,21 +33,28 @@ write_captions <- function(dat, # converted model output object # FIGURES----- - ## kobe plot - # B.BMSY.min <- # minimum value of B/B(MSY) - # B.BMSY.max <- # maximum value of B/B(MSY) - # F.FMSY.min <- # minimum value of F/F(MSY) - # F.FMSY.max <- # maximum value of F/F(MSY) - # B.BMSY.end.yr <- # value of B/B(MSY) at the end year - # F.FMSY.end.yr <- # value of F/F(MSY) at the end year - # overfished.status.is.isnot <- # object that should be "is" or "is not" and answers the question, "the stock overfishing status ... overfished" - # overfishing.status.is.isnot <- # object that should be "is" or "is not" and answers the question, "the stock ... experiencing overfishing" - # kobe.start.year <- # start year of kobe plot - # kobe.end.year <- # end year of kobe plot + ## 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 <- + + # 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 <- + ## Biomass plot - # B.ref.pt <- # biomass reference point- SHARED with kobe plot, above - # B.ref.pt.units <- # biomass reference point unit + # B.ref.pt <- # biomass reference point # start year of biomass plot B.start.year <- dat |> @@ -123,14 +130,24 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot # F.ref.pt <- # F reference point - # F.ref.pt.units <- # F reference point unit + # --(use to_lower): F_targ if it's present. if not, use F_proxy; if not, Fmsy (these are labels in dat) # F.start.year. <- # start year of F plot - # F.end.year <- # start year of F plot - # F.units <- # units of F (plural) + # --like Fend, but minimum year + # F.end.year <- # end year of F plot + # --Fend # F.min <- # minimum F + # --if age = na, then take min(time series) + # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) |> + # pull(min(val)) # F.max <- # maximum F + # --if age = na, then take max(time series) + # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) + # pull(max(val)) # Ftarg <- + # --Ftarg # F.Ftarg <- + # --F_Ftarg + ## landings plot @@ -200,20 +217,22 @@ write_captions <- function(dat, # converted model output object ## natural mortality (M) # M.age.min <- # minimum age of M + # --if age != na, min(age) # M.age.max <- # maximum age of M - # M.units <- # units of M (plural) + # --if age != na, max(age) # M.rate.min <- # minimum M rate + # -label = natural_mortality (min); est in est col # M.rate.max <- # maximum M rate + # -label = natural_mortality (min); est in est col - ## vonB LAA (von Bertalanffy growth function + length at age) - # vonb.age.units <- # vonB age units (plural) + ## 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 <- # vonB length units (plural) # vonb.length.min <- # minimum vonB length # vonb.length.max <- # minimum vonB length - ## length-type conversion plot + ## length-type conversion plot- don't code quantities yet # total.length.units <- # total length units (plural) # total.length.min <- # minimum total length # total.length.max <- # maximum total length @@ -221,7 +240,7 @@ write_captions <- function(dat, # converted model output object # fork.length.min <- # minimum fork length # fork.length.max <- # maximum fork length - ## weight-length conversion plot + ## weight-length conversion plot- don't code quantities yet # wl.length.units <- # length units (plural) # wl.length.min <- # minimum length # wl.length.max <- # maximum length @@ -229,12 +248,12 @@ write_captions <- function(dat, # converted model output object # wl.weight.min <- # minimum weight # wl.weight.max <- # maximum weight - ## maturity schedule (proportion mature) + ## maturity schedule (proportion mature)- don't code quantities yet # prop.mat.length.units <- # length units (plural) # prop.mat.length.min <- # minimum length # prop.mat.length.max <- # maximum length - ## fecundity at length + ## fecundity at length- don't code quantities yet # fecundity.length.units <- # length units (plural) # fecundity.length.min <- # minimum length # fecundity.length.max <- # maximum length @@ -242,16 +261,16 @@ write_captions <- function(dat, # converted model output object # fecundity.min <- # minimum fecundity # fecundity.max <- # maximum fecundity - ## CAA (catch at age) + ## CAA (catch at age)- don't code quantities yet # fleet.or.survey.name <- # fleet or survey name (SHARED with CAL, below) # caa.age.min <- # minimum age group # caa.age.max <- # maximum age group - ## CAL (catch at length) + ## CAL (catch at length)- don't code quantities yet # cal.length.min <- # minimum length group # cal.length.max <- # maximum length group - ## CPUE indices plot + ## 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 <- # CPUE units (plural) (SHARED with mod.fit.abun, below) @@ -260,12 +279,15 @@ write_captions <- function(dat, # converted model output object ## NAA (numbers at age) # bubble.start.year.min <- # start year of NAA plot + # -label = abundance ; year != na; min(year) # bubble.end.year.max <- # end year of NAA plot - # bubble.age.units <- # age units (plural) + # -label = abundance ; year != na; max(year) # bubble.age.min <- # minimum age + # year != na; min(age) # bubble.age.max <- # maximum age + # year != na; max(age) - ## mod_fit_catch (model fit to catch ts) + ## 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 # mod.fit.catch.units <- # catch units (plural) @@ -276,14 +298,19 @@ write_captions <- function(dat, # converted model output object # mod.fit.abun.start.year <- # start year of model fit to abundance indices plot # mod.fit.abun.end.year <- # end year of model fit to abundance indices plot - ## mod_fit_discards + ## 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 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 + ## 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) @@ -291,9 +318,9 @@ write_captions <- function(dat, # converted model output object # selectivity.length.max <- # maximum length - ## estimated stock recruitment (aka spawning stock biomass) + ## estimated stock recruitment # youngest-age recruited fish (instead of age-0) - # sr.age.min <- # unsure how to extract this + # sr.age.min <- min(age) (usually 1) # ssb units (plural) # sr.ssb.units <- # this will take some thought, since @@ -476,6 +503,7 @@ write_captions <- function(dat, # converted model output object round(digits = 2) ## tot_b (total biomass) + ## -same as B plot above # biomass.start.year <- # start year of biomass plot # biomass.end.year <- # end year of biomass plot # biomass.units <- # biomass units (plural) @@ -560,12 +588,17 @@ write_captions <- function(dat, # converted model output object ## spr (spawning potential ratio) # spr.start.year <- # start year of spr plot + # -same as ssb plot # spr.end.year <- # end year of spr plot + # -same as ssb plot # spr.min <- # minimum spr + # -label = spr (min) & end.year != na ; OR grepl("spr")- could be iffy # spr.max <- # maximum spr + # - " # spr.ref.pt <- # spr reference point - # spr.ref.pt.units <- # spr reference point units + # -spr_msy + #TODO: LEFT OFF HERE (identifying how to extract quantities with Sam, Steve) ## pop_naa_baa (population numbers at age and population biomass at age) # pop.naa.baa.start.year <- # start year of spr plot # pop.naa.baa.end.year <- # end year of spr plot @@ -622,16 +655,11 @@ write_captions <- function(dat, # converted model output object # FIGURES----- ## kobe plot - # 'B.BMSY.min' = as.character(B.BMSY.min), - # 'B.BMSY.max' = as.character(B.BMSY.max), - # 'F.FMSY.min' = as.character(F.FMSY.min), - # 'F.FMSY.max' = as.character(F.FMSY.max), + # '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), - # 'kobe.start.year' = as.character(kobe.start.year), - # 'kobe.end.year' = as.character(kobe.end.year), ## Relative biomass plot # NOTE: moving this above biomass so rel.B.min isn't changed to "rel." + B.min (etc.) @@ -640,7 +668,6 @@ write_captions <- function(dat, # converted model output object ## Biomass plot # 'B.ref.pt' = as.character(B.ref.pt), - # 'B.ref.pt.units' = as.character(B.ref.pt.units), 'B.start.year' = as.character(B.start.year), # 'B.end.year' = as.character(B.end.year), # 'B.units' = as.character(B.units), @@ -653,10 +680,8 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot # 'F.ref.pt' = as.character(F.ref.pt), - # 'F.ref.pt.units' = as.character(F.ref.pt.units), # 'F.start.year' = as.character(F.start.year), # 'F.end.year' = as.character(F.end.year), - # 'F.units' = as.character(F.units), # 'F.min' = as.character(F.min), # 'F.max' = as.character(F.max), # 'Ftarg' = as.character(Ftarg), @@ -672,12 +697,10 @@ write_captions <- function(dat, # converted model output object ## natural mortality (M) # 'M.age.min' = as.character(M.age.min), # 'M.age.max' = as.character(M.age.max), - # 'M.units' = as.character(M.units), # '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.units' = as.character(vonb.age.units), # 'vonb.age.min' = as.character(vonb.age.min), # 'vonb.age.max' = as.character(vonb.age.max), # 'vonb.length.units' = as.character(vonb.length.units), @@ -732,7 +755,6 @@ write_captions <- function(dat, # converted model output object ## NAA (numbers at age) # 'bubble.start.year.min' = as.character(bubble.start.year.min), # 'bubble.end.year.max' = as.character(bubble.end.year.max), - # 'bubble.age.units' = as.character(bubble.age.units), # 'bubble.age.min' = as.character(bubble.age.min), # 'bubble.age.max' = as.character(bubble.age.max), @@ -821,7 +843,6 @@ write_captions <- function(dat, # converted model output object # 'spr.min' = as.character(spr.min), # 'spr.max' = as.character(spr.max), # 'spr.ref.pt' = as.character(spr.ref.pt), - # 'spr.ref.pt.units' = as.character(spr.ref.pt.units), # # ## pop_naa_baa (population numbers at age and population biomass at age) # 'pop.naa.baa.start.year' = as.character(pop.naa.baa.start.year), From 29a83b0840f07acf98afa3f293b25910a7ad40cf Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 14:17:19 -0500 Subject: [PATCH 08/54] Small updates to broaden new fxn that inserts more key quantities- including reference points --- R/utils.R | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/R/utils.R b/R/utils.R index 3973d09..1adeb9c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -66,8 +66,8 @@ export_rda <- function(final = final, paste0(topic_label, "_", fig_or_table, ".rda"))) } -# substitute in more key quantities (units and end_years) to captions/alt text -add_yr_units <- function( +# substitute in more key quantities (units, end_years, reference points) to captions/alt text +add_more_key_quants <- function( topic = topic_label, fig_or_table = fig_or_table, end_year = NULL, @@ -87,8 +87,8 @@ add_yr_units <- function( type == fig_or_table) # select caption - topic_cap <- topic_cap_alt |> - dplyr::select(caption) + # topic_cap <- topic_cap_alt |> + # dplyr::select(caption) # select alt text topic_alt <- topic_cap_alt |> @@ -106,7 +106,13 @@ add_yr_units <- function( as.character(units), topic_alt) + ## reference points + ## TODO: ADD HERE + # replace old alt text with new alt text + test <- caps_alt_df[caps_alt_df$type == fig_or_table, + # caps_alt_df$label == topic + ] } From e6d276222096cc42464874cb4cacba5825239e43 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 14:17:39 -0500 Subject: [PATCH 09/54] Continue updating key quantities in write_captions and alt text/caps csv --- R/write_captions.R | 112 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 85 insertions(+), 27 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 309ed5d..cd5ce14 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -133,8 +133,19 @@ write_captions <- function(dat, # converted model output object # --(use to_lower): F_targ if it's present. if not, use F_proxy; if not, Fmsy (these are labels in dat) # F.start.year. <- # start year of F plot # --like Fend, but minimum year - # F.end.year <- # end year of F plot - # --Fend + + # terminal fishing mortality + # TODO: Put this in utils function, since F.end.year needs end_year + # 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) + # F.min <- # minimum F # --if age = na, then take min(time series) # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) |> @@ -143,10 +154,19 @@ write_captions <- function(dat, # converted model output object # --if age = na, then take max(time series) # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) # pull(max(val)) - # Ftarg <- - # --Ftarg - # F.Ftarg <- - # --F_Ftarg + + # 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 + # TODO: Put this in utils function, since F.end.year needs end_year + # F.Ftarg <- F.end.year / Ftarg ## landings plot @@ -216,11 +236,27 @@ write_captions <- function(dat, # converted model output object round(digits = 2) ## natural mortality (M) - # M.age.min <- # minimum age of M - # --if age != na, min(age) - # M.age.max <- # maximum age of M - # --if age != na, max(age) - # M.rate.min <- # minimum M rate + ## Should this be filtered by label = natural_mortality too? + # minimum age of M + # M.age.min <- dat |> + # dplyr::select(age) |> + # dplyr::filter(!is.na(age)) |> + # dplyr::slice(which.min(age)) |> + # as.numeric() + # + # # maximum age of M + # M.age.max <- dat |> + # dplyr::select(age) |> + # dplyr::filter(!is.na(age)) |> + # dplyr::slice(which.max(age)) |> + # as.numeric() + + # minimum M rate + # The "petrale_sole_std_output.csv" doesn't have a natural_mortality label... + # doesn't work + # M.rate.min <- dat |> + # dplyr::filter( + # grepl("natural_mortality", label)) |> # -label = natural_mortality (min); est in est col # M.rate.max <- # maximum M rate # -label = natural_mortality (min); est in est col @@ -278,14 +314,33 @@ write_captions <- function(dat, # converted model output object # cpue.max <- # maximum CPUE (SHARED with mod_fit_abun, below) ## NAA (numbers at age) - # bubble.start.year.min <- # start year of NAA plot - # -label = abundance ; year != na; min(year) - # bubble.end.year.max <- # end year of NAA plot - # -label = abundance ; year != na; max(year) - # bubble.age.min <- # minimum age - # year != na; min(age) - # bubble.age.max <- # maximum age - # year != na; max(age) + # start year of NAA plot + bubble.start.year.min <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.min(year)) |> + dplyr::select(year) |> + as.numeric() + + # end year of NAA plot + bubble.end.year.max <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.max(year)) |> + dplyr::select(year) |> + as.numeric() + + # minimum age + bubble.age.min <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.min(age)) |> + dplyr::select(age) |> + as.numeric() + + # maximum age + bubble.age.max <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.max(age)) |> + dplyr::select(age) |> + as.numeric() ## 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 @@ -294,9 +349,12 @@ write_captions <- function(dat, # converted model output object # mod.fit.catch.min <- # minimum catch # mod.fit.catch.max <- # maximum catch - ## mod_fit_abun (model fit to abundance indices plot) - # mod.fit.abun.start.year <- # start year of model fit to abundance indices plot - # mod.fit.abun.end.year <- # end year of model fit to abundance indices plot + ## 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 @@ -320,7 +378,11 @@ write_captions <- function(dat, # converted model output object ## estimated stock recruitment # youngest-age recruited fish (instead of age-0) - # sr.age.min <- min(age) (usually 1) + # 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 <- # this will take some thought, since @@ -509,8 +571,6 @@ write_captions <- function(dat, # converted model output object # biomass.units <- # biomass units (plural) # biomass.min <- # minimum biomass # biomass.max <- # maximum biomass - # biomass.ref.pt <- # biomass reference point - # biomass.ref.pt.units <- # biomass reference point units ## spawning_biomass (ssb) # start year of ssb plot @@ -817,8 +877,6 @@ write_captions <- function(dat, # converted model output object # 'biomass.units' = as.character(biomass.units), # 'biomass.min' = as.character(biomass.min), # 'biomass.max' = as.character(biomass.max), - # 'biomass.ref.pt' = as.character(biomass.ref.pt), - # 'biomass.ref.pt.units' = as.character(biomass.ref.pt.units), # relative ssb # NOTE: moving this above recruitment so rel.ssb.min isn't changed to From 400c29809977ed6052f43a9872a69fc10e017606 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 16:12:55 -0500 Subject: [PATCH 10/54] Continue updating key quantities in write_captions and alt text/caps csv --- R/write_captions.R | 128 ++++++++++-------- inst/resources/captions_alt_text_template.csv | 10 +- 2 files changed, 74 insertions(+), 64 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index cd5ce14..37f2902 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -129,10 +129,23 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot - # F.ref.pt <- # F reference point - # --(use to_lower): F_targ if it's present. if not, use F_proxy; if not, Fmsy (these are labels in dat) - # F.start.year. <- # start year of F plot - # --like Fend, but minimum year + # 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") + ) |> + 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 # TODO: Put this in utils function, since F.end.year needs end_year @@ -146,11 +159,21 @@ write_captions <- function(dat, # converted model output object # as.numeric() |> # round(digits = 2) - # F.min <- # minimum F + # minimum F + # F.min <- dat |> + # dplyr::filter(label == 'fishing_mortality') + # + # if (F.min$age) + # + # dplyr::slice(which.min(year)) |> + # as.numeric() |> + # round(digits = 2) # --if age = na, then take min(time series) # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) |> # pull(min(val)) - # F.max <- # maximum F + + # maximum F + # F.max <- # --if age = na, then take max(time series) # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) # pull(max(val)) @@ -491,20 +514,14 @@ write_captions <- function(dat, # converted model output object # recruitment.end.year <- # this will take some thought, since end_year is # either entered by the user or calculated in plot_recruitment.R - # minimum recruitment - recruitment.min <- sr.min - - # maximum recruitment - recruitment.max <- sr.max - ## relative recruitment # minimum relative recruitment - rel.recruitment.min <- (recruitment.min / R0) |> + rel.recruitment.min <- (sr.min / R0) |> round(digits = 2) # maximum relative recruitment - rel.recruitment.max <- (recruitment.max / R0) |> + rel.recruitment.max <- (sr.max / R0) |> round(digits = 2) @@ -564,13 +581,7 @@ write_captions <- function(dat, # converted model output object as.numeric() |> round(digits = 2) - ## tot_b (total biomass) - ## -same as B plot above - # biomass.start.year <- # start year of biomass plot - # biomass.end.year <- # end year of biomass plot - # biomass.units <- # biomass units (plural) - # biomass.min <- # minimum biomass - # biomass.max <- # maximum biomass + ## tot_b (total biomass): same as B plot above ## spawning_biomass (ssb) # start year of ssb plot @@ -627,9 +638,6 @@ write_captions <- function(dat, # converted model output object # ssb.ref.pt <- # this will take some thought, since ref_line_val is # calculated in plot_spawning_biomass.R - # ssb reference point units - # ssb.ref.pt.units <- ssb_units - ssbtarg <- dat |> dplyr::filter(c(grepl('spawning_biomass', label) & grepl('msy$', label) & estimate >1) | label == 'spawning_biomass_msy$') |> dplyr::pull(estimate) |> @@ -647,16 +655,30 @@ write_captions <- function(dat, # converted model output object ## spr (spawning potential ratio) - # spr.start.year <- # start year of spr plot - # -same as ssb plot - # spr.end.year <- # end year of spr plot - # -same as ssb plot - # spr.min <- # minimum spr - # -label = spr (min) & end.year != na ; OR grepl("spr")- could be iffy - # spr.max <- # maximum spr - # - " - # spr.ref.pt <- # spr reference point - # -spr_msy + # 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 reference point + spr.ref.pt <- dat |> + dplyr::filter(label == "spr_msy") |> + dplyr::select(estimate) |> + as.numeric() |> + round(digits = 2) #TODO: LEFT OFF HERE (identifying how to extract quantities with Sam, Steve) ## pop_naa_baa (population numbers at age and population biomass at age) @@ -739,12 +761,12 @@ write_captions <- function(dat, # converted model output object # 'Bmsy' = as.character(Bmsy), ## mortality (F) plot - # 'F.ref.pt' = as.character(F.ref.pt), - # 'F.start.year' = as.character(F.start.year), + 'F.ref.pt' = as.character(F.ref.pt), + 'F.start.year' = as.character(F.start.year), # 'F.end.year' = as.character(F.end.year), # 'F.min' = as.character(F.min), # 'F.max' = as.character(F.max), - # 'Ftarg' = as.character(Ftarg), + 'Ftarg' = as.character(Ftarg), # 'F.Ftarg' = as.character(F.Ftarg), ## landings plot @@ -813,10 +835,10 @@ write_captions <- function(dat, # converted model output object # 'cpue.max' = as.character(cpue.max), ## NAA (numbers at age) - # 'bubble.start.year.min' = as.character(bubble.start.year.min), - # 'bubble.end.year.max' = as.character(bubble.end.year.max), - # 'bubble.age.min' = as.character(bubble.age.min), - # 'bubble.age.max' = as.character(bubble.age.max), + 'bubble.start.year.min' = as.character(bubble.start.year.min), + 'bubble.end.year.max' = as.character(bubble.end.year.max), + 'bubble.age.min' = as.character(bubble.age.min), + 'bubble.age.max' = as.character(bubble.age.max), ## mod_fit_catch (model fit to catch ts) # 'mod.fit.catch.start.year' = as.character(mod.fit.catch.start.year), @@ -862,8 +884,6 @@ write_captions <- function(dat, # converted model output object # 'recruitment.units' = as.character(recruitment.units), 'recruitment.start.year' = as.character(recruitment.start.year), # 'recruitment.end.year' = as.character(recruitment.end.year), - 'recruitment.min' = as.character(recruitment.min), - 'recruitment.max' = as.character(recruitment.max), ## recruitment deviations 'recruit.dev.start.year' = as.character(recruit.dev.start.year), @@ -871,13 +891,6 @@ write_captions <- function(dat, # converted model output object 'recruit.dev.min' = as.character(recruit.dev.min), 'recruit.dev.max' = as.character(recruit.dev.max), - ## tot_b (total biomass) - # 'biomass.start.year' = as.character(biomass.start.year), - # 'biomass.end.year' = as.character(biomass.end.year), - # 'biomass.units' = as.character(biomass.units), - # 'biomass.min' = as.character(biomass.min), - # 'biomass.max' = as.character(biomass.max), - # relative ssb # NOTE: moving this above recruitment so rel.ssb.min isn't changed to # "rel." + ssb.min), etc. @@ -889,19 +902,16 @@ write_captions <- function(dat, # converted model output object # 'ssb.end.year' = as.character(ssb.end.year), # 'ssb.units' = as.character(ssb.units), 'ssb.min' = as.character(ssb.min), - 'ssb.max' = as.character(ssb.max)#, + 'ssb.max' = as.character(ssb.max), # 'ssb.ref.pt' = as.character(ssb.ref.pt), - # 'ssb.ref.pt.units' = as.character(ssb.ref.pt.units), # 'ssbtarg' = as.character(ssbtarg), - # ## spr (spawning potential ratio) - # 'spr.start.year' = as.character(spr.start.year), - # 'spr.end.year' = as.character(spr.end.year), - # 'spr.min' = as.character(spr.min), - # 'spr.max' = as.character(spr.max), - # 'spr.ref.pt' = as.character(spr.ref.pt), - # + ## 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_naa_baa (population numbers at age and population biomass at age) # 'pop.naa.baa.start.year' = as.character(pop.naa.baa.start.year), # 'pop.naa.baa.end.year' = as.character(pop.naa.baa.end.year), diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index e61e96b..b63ab50 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -19,13 +19,13 @@ mod.fit.abun,figure,Assessment model fits to input catch per unit of effort inde mod.fit.discards,figure,Observed discards (points) and model-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." sr,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, while 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 sr.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year, while 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.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,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from biomass.start.year to biomass.end.year . The y axis shows model-estimated biomass in biomass.units, which spans from biomass.min to biomass.max." -spawning.biomass,figure,Model-estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point at ssb.ref.pt ssb.ref.pt.units.,"Line graph showing model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." -relative.spawning.biomass,figure,"Model-estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point), where SSB(reference point) is ssb.ref.pt ssb.ref.pt.units.","Line graph showing model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." -spr,figure,Model-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 model-estimated spawning potential ratio over time. The x axis shows the year, which spans from spr.start.year to spr.end.year . The y axis shows model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." +tot.b,figure,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows model-estimated biomass in B.units, which spans from B.min to B.max." +spawning.biomass,figure,Model-estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point at ssb.ref.pt ssb.units.,"Line graph showing model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." +relative.spawning.biomass,figure,"Model-estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point), where SSB(reference point) is ssb.ref.pt ssb.units.","Line graph showing model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." +spr,figure,Model-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 model-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 model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." pop.naa.baa,figure,Model-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 model-estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.naa.baa.start.year to pop.naa.baa.end.year . The y axis shows numbers of fish, which spans from pop.naa.baa.fish.min to pop.naa.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,figure,Forecasted biomass in proj.biomass.units over future years for different fishing mortality scenarios as indicated in the legend. The horizontal dashed line represents the biomass limit reference point. ,"Time series line graph showing forecasted biomass over future years for different fishing mortality scenarios. The horizontal dashed line represents the biomass limit reference point. The x axis shows the year, which spans from proj.biomass.start.year to proj.biomass.end.year . The y axis shows biomass in proj.biomass.units, which spans from proj.biomass.min to proj.biomass.max." From 2763323ead593ea62e1fff102fe24e6ad6dc939b Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 11:04:04 -0500 Subject: [PATCH 11/54] Finished first working draft of add_more_key_quants fxn; updated alt text/caps template to facilitate regex pattern recognition --- R/utils.R | 85 +++++++++++++------ inst/resources/captions_alt_text_template.csv | 10 +-- 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/R/utils.R b/R/utils.R index 1adeb9c..22613cf 100644 --- a/R/utils.R +++ b/R/utils.R @@ -70,8 +70,10 @@ export_rda <- function(final = final, add_more_key_quants <- function( topic = topic_label, fig_or_table = fig_or_table, + dir = NULL, end_year = NULL, - units = NULL){ + units = NULL, + ref_pt = NULL){ # import csv caps_alt_df <- read.csv(fs::path(getwd(), "captions_alt_text.csv")) @@ -86,34 +88,69 @@ add_more_key_quants <- function( dplyr::filter(label == topic, type == fig_or_table) - # select caption - # topic_cap <- topic_cap_alt |> - # dplyr::select(caption) - - # select alt text - topic_alt <- topic_cap_alt |> - dplyr::select(alt_text) - - # replace placeholders in alt text + # replace placeholders ## end_year - # if "end.year" is found in topic_alt, replace it with end_year - topic_alt <- sub("\\S*end\\.year\\S*", - as.character(end_year), - topic_alt) + ### alt text + # if "end.year " is found in topic_alt, replace it with 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)) ## units - topic_alt <- sub("\\S*units\\S*", - as.character(units), - topic_alt) + ### 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))) + + ### 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?,)"), + 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*"), + as.character(units))) ## reference points - ## TODO: ADD HERE - - # replace old alt text with new alt text - test <- caps_alt_df[caps_alt_df$type == fig_or_table, - # caps_alt_df$label == topic - ] - + ### 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)))) + + # 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) } diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index b63ab50..eaf9b4b 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -1,7 +1,7 @@ label,type,caption,alt_text kobe,figure,"Kobe plot showing stock status. Triangles delineate start and end years. Horizontal and vertical dashed lines delineate the proportion of F/F~MSY~ where F~MSY~ = B.ref.pt, and B/B~MSY~ where B~MSY~ = B.ref.pt. Overfishing is occurring when F/F~MSY~ > 1 and an overfished status is indicated where B/B~MSY~ > 1.","A Kobe plot showing stock status. The x axis shows overfished status (i.e., B/B~MSY~) and the y axis shows overfishing (i.e., F/F~MSY~). The B/B~MSY~ and F/F~MSY~ for kobe.end.year were B.BMSY.end.yr and F.FMSY.end.yr, respectively, indicating that the stock overfished.status.is.isnot overfished and overfishing.status.is.is.not experiencing overfishing." biomass,figure,Biomass (B) time series. The horizontal dashed line represents the limit reference point (B.ref.pt B.units).,"Line graph showing biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows biomass in B.units, which spans from B.min to B.max." -relative.biomass,figure,"Relative biomass (B) time series. The horizontal dashed line represents the limit reference point calculated as B/B(reference point), where B(reference point) is 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." +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." 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." 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." @@ -9,7 +9,7 @@ vonb.laa,figure,Aged and measured fish (points) and von Bertalanffy growth funct 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." weight.length.converstion,figure,Weight-length conversion relationship where the points represent individual fish observations and the line is the fitted relationship. ,"Point and line graph showing observations of measured length and measured weight from the same fish for conversion purposes as points, and an exponential fit through those points as a line. The x axis shows length in wl.length.units, which spans from wl.length.min to wl.length.max. The y axis shows weight in wl.weight.units, which spans from wl.weight.min to wl.weight.max." maturity.schedule,figure,The relationship between the proportion mature and fish length. ,"Graph showing the relationship between fish maturity and length. Points, if present, show observations of maturity at different fish lengths and the line represents either the fit to the points, or the assumed or known maturity schedule. The x axis shows length in prop.mat.length.units, which spans from prop.mat.length.min to prop.mat.length.max. The y axis shows the proportion mature, which spans from 0--1." -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." +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." CAA,figure,"Histograms showing the proportion of the catch in each age group for each year in 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 age group. The x axis of each histogram shows age groups that span from caa.age.min to caa.age.max. The y axis of each histogram shows the proportion of the catch and spans from 0--1." 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." CPUE.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 for fleet or survey from cpue.start.year to cpue.end.year, 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." @@ -19,12 +19,12 @@ mod.fit.abun,figure,Assessment model fits to input catch per unit of effort inde mod.fit.discards,figure,Observed discards (points) and model-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." sr,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, while the y axis shows recruitment in recruitment.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." 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.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,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows model-estimated biomass in B.units, which spans from B.min to B.max." -spawning.biomass,figure,Model-estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point at ssb.ref.pt ssb.units.,"Line graph showing model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." -relative.spawning.biomass,figure,"Model-estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point), where SSB(reference point) is ssb.ref.pt ssb.units.","Line graph showing model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." +spawning.biomass,figure,Model-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 model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." +relative.spawning.biomass,figure,Model-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 model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." spr,figure,Model-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 model-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 model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." pop.naa.baa,figure,Model-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 model-estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.naa.baa.start.year to pop.naa.baa.end.year . The y axis shows numbers of fish, which spans from pop.naa.baa.fish.min to pop.naa.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." From f484780e0e4eea11a8162ab5cfed34cda1476808 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 11:22:17 -0500 Subject: [PATCH 12/54] Small updates to fix regex matching --- R/utils.R | 12 ++++++++++-- inst/resources/captions_alt_text_template.csv | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/R/utils.R b/R/utils.R index 22613cf..9cb9071 100644 --- a/R/utils.R +++ b/R/utils.R @@ -88,15 +88,23 @@ add_more_key_quants <- function( dplyr::filter(label == topic, type == fig_or_table) - # replace placeholders + # replace placeholders (e.g., if "end.year" is found in topic_alt, replace it with end_year) ## end_year ### alt text - # if "end.year " is found in topic_alt, replace it with end_year + ### 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)) + + ## units ### caption ### this regex preserves the closing ) after the units diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index eaf9b4b..6394a7a 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -12,7 +12,7 @@ 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." CAA,figure,"Histograms showing the proportion of the catch in each age group for each year in 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 age group. The x axis of each histogram shows age groups that span from caa.age.min to caa.age.max. The y axis of each histogram shows the proportion of the catch and spans from 0--1." 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." -CPUE.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 for fleet or survey from cpue.start.year to cpue.end.year, 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." +CPUE.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." NAA,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 bubble.start.year to bubble.end.year . The y axis shows age in years, which spans from bubble.age.min to bubble.age.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." From ba4a93dee480fe93f5293f8a075837eb9c5c12fd Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 15:14:43 -0500 Subject: [PATCH 13/54] added new fxn (add_more_key_quants) to plotting fxns --- R/plot_biomass.R | 13 ++++++++++++- R/plot_indices.R | 8 ++++++++ R/plot_landings.R | 8 ++++++++ R/plot_recruitment.R | 9 +++++++++ R/plot_recruitment_deviations.R | 8 ++++++++ R/plot_spawning_biomass.R | 10 ++++++++++ 6 files changed, 55 insertions(+), 1 deletion(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 06057c9..3ebcd11 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -179,6 +179,16 @@ plot_biomass <- function( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + end_year = end_year, + units = unit_label, + ref_pt = ref_point + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, @@ -189,6 +199,7 @@ plot_biomass <- function( rda_dir = rda_dir, topic_label = topic_label, fig_or_table = fig_or_table) - } + + } return(final) } diff --git a/R/plot_indices.R b/R/plot_indices.R index bb06476..8d60aed 100644 --- a/R/plot_indices.R +++ b/R/plot_indices.R @@ -143,6 +143,14 @@ plot_indices <- function( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + units = unit_label + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, diff --git a/R/plot_landings.R b/R/plot_landings.R index 46cc6e6..2b8bf19 100644 --- a/R/plot_landings.R +++ b/R/plot_landings.R @@ -84,6 +84,14 @@ plot_landings <- function(dat, year = NULL) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + units = unit_label + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, diff --git a/R/plot_recruitment.R b/R/plot_recruitment.R index 83a9453..49da1f9 100644 --- a/R/plot_recruitment.R +++ b/R/plot_recruitment.R @@ -125,6 +125,15 @@ plot_recruitment <- function( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + end_year = end_year, + units = unit_label + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, diff --git a/R/plot_recruitment_deviations.R b/R/plot_recruitment_deviations.R index 0aaec31..815412c 100644 --- a/R/plot_recruitment_deviations.R +++ b/R/plot_recruitment_deviations.R @@ -102,6 +102,14 @@ plot_recruitment_deviations <- function ( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + end_year = end_year + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 88fca14..fd117ae 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -186,6 +186,16 @@ plot_spawning_biomass <- function( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + end_year = end_year, + units = unit_label, + ref_pt = ref_point + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, From 8077ef2e2ce88944c172739ac5928178d91364c3 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 16:07:10 -0500 Subject: [PATCH 14/54] updated add_more_key_quants fxn to handle two units (arguments) specified in plot_spawn_recruitment, and added add_more_key_quants to plot_spawn_recruitment --- R/plot_spawn_recruitment.R | 11 +++++++++++ R/utils.R | 34 +++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/R/plot_spawn_recruitment.R b/R/plot_spawn_recruitment.R index 25593ff..1fc3296 100644 --- a/R/plot_spawn_recruitment.R +++ b/R/plot_spawn_recruitment.R @@ -83,6 +83,17 @@ plot_spawn_recruitment <- function( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + end_year = end_year, + sr_ssb_units = spawning_biomass_label, + sr_recruitment_units = recruitment_label, + units = NULL + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, diff --git a/R/utils.R b/R/utils.R index 9cb9071..bfd96ae 100644 --- a/R/utils.R +++ b/R/utils.R @@ -73,6 +73,8 @@ add_more_key_quants <- function( dir = NULL, end_year = NULL, units = NULL, + sr_ssb_units = NULL, + sr_recruitment_units = NULL, ref_pt = NULL){ # import csv @@ -89,7 +91,7 @@ add_more_key_quants <- function( type == fig_or_table) # replace placeholders (e.g., if "end.year" is found in topic_alt, replace it with end_year) - ## end_year + ## end_year----- ### alt text ### this regex preserves the comma after the end year topic_cap_alt <- topic_cap_alt |> @@ -102,10 +104,8 @@ add_more_key_quants <- function( dplyr::mutate(alt_text = stringr::str_replace_all(alt_text, stringr::regex("\\S*end\\.year\\S*\\s*"), end_year)) - - - - ## units + ## units----- + if(!is.null(units)){ ### caption ### this regex preserves the closing ) after the units topic_cap_alt <- topic_cap_alt |> @@ -125,7 +125,27 @@ add_more_key_quants <- function( dplyr::mutate(caption = stringr::str_replace_all(caption, stringr::regex("\\S*units\\S*"), as.character(units))) + } + + if(!is.null(sr_ssb_units)){ + ### this is for plot_spawn_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))) + } + if(!is.null(sr_recruitment_units)){ + ### this is for plot_spawn_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))) + } + + if(!is.null(units)){ ### alt text ### this regex preserves the comma after the units topic_cap_alt <- topic_cap_alt |> @@ -140,7 +160,8 @@ add_more_key_quants <- function( stringr::regex("\\S*units\\S*"), as.character(units))) - ## reference points + } + ## reference points----- ### caption ### this regex preserves the opening ( before the ref pt topic_cap_alt <- topic_cap_alt |> @@ -161,4 +182,3 @@ add_more_key_quants <- function( row.names=FALSE) } - From aa6222f415385216590ce4ad3a7e3f149baf5a0b Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 16:42:08 -0500 Subject: [PATCH 15/54] minor update to test-plot_landings --- tests/testthat/test-plot_landings.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-plot_landings.R b/tests/testthat/test-plot_landings.R index 29f200f..4695131 100644 --- a/tests/testthat/test-plot_landings.R +++ b/tests/testthat/test-plot_landings.R @@ -30,6 +30,7 @@ test_that("rda file made when indicated",{ # export rda plot_landings(dat, make_rda = TRUE, + unit_label = "metric tons", rda_dir = getwd()) # expect that both rda_files dir and the landings_figure.rda file exist From 4f2a362d1335be7a56601bc59d90acdb02a73c08 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 16:43:03 -0500 Subject: [PATCH 16/54] -fixed bug in add_more_key_quants to allow str_replace_all to work even if some args are NULL -updated write_captions to explain which key quantities will be substituted with add_more_key_quants --- R/utils.R | 4 ++ R/write_captions.R | 104 +++++++++++++++++++++++++-------------------- 2 files changed, 62 insertions(+), 46 deletions(-) diff --git a/R/utils.R b/R/utils.R index bfd96ae..10689fd 100644 --- a/R/utils.R +++ b/R/utils.R @@ -92,6 +92,7 @@ add_more_key_quants <- function( # 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 |> @@ -104,6 +105,7 @@ add_more_key_quants <- function( dplyr::mutate(alt_text = stringr::str_replace_all(alt_text, stringr::regex("\\S*end\\.year\\S*\\s*"), end_year)) + } ## units----- if(!is.null(units)){ ### caption @@ -162,12 +164,14 @@ add_more_key_quants <- function( } ## 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)))) + } # remove row with old caption/alt text, then add new row replaced_df <- dplyr::anti_join(caps_alt_df, diff --git a/R/write_captions.R b/R/write_captions.R index 37f2902..0216dc0 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -54,7 +54,8 @@ write_captions <- function(dat, # converted model output object ## Biomass plot - # B.ref.pt <- # biomass reference point + # biomass reference point + # B.ref.pt : added with add_more_key_quants # start year of biomass plot B.start.year <- dat |> @@ -68,12 +69,10 @@ write_captions <- function(dat, # converted model output object as.numeric() # end year of biomass plot - # B.end.year <- # this will take some thought, since end_year is - # either entered by the user or calculated in plot_biomass.R + # B.end.year : added with add_more_key_quants # units of B (plural) - # B.units <- # this will take some thought, since units is - # entered by the user in plot_biomass.R + # B.units : added with add_more_key_quants # minimum B B.min <- dat |> @@ -223,8 +222,7 @@ write_captions <- function(dat, # converted model output object as.numeric() # units of landings (plural) - # landings.units <- # this will take some thought, since units is - # entered by the user in plot_landings.R + # landings.units : added with add_more_key_quants # minimum landings landings.min <- dat |> @@ -284,42 +282,67 @@ write_captions <- function(dat, # converted model output object # M.rate.max <- # maximum M rate # -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 <- # vonB length units (plural) + + # vonB length units (plural) + # vonb.length.units : added with add_more_key_quants + # vonb.length.min <- # minimum vonB length # vonb.length.max <- # minimum vonB length + ## length-type conversion plot- don't code quantities yet - # total.length.units <- # total length units (plural) + # total length units (plural) + # total.length.units : added with add_more_key_quants + # total.length.min <- # minimum total length # total.length.max <- # maximum total length - # fork.length.units <- # fork length units (plural) + # fork length units (plural) + # fork.length.units : added with add_more_key_quants + # fork.length.min <- # minimum fork length # fork.length.max <- # maximum fork length + ## weight-length conversion plot- don't code quantities yet - # wl.length.units <- # length units (plural) + # length units (plural) + # wl.length.units : added with add_more_key_quants + # wl.length.min <- # minimum length # wl.length.max <- # maximum length - # wl.weight.units <- # weight units (plural) + + # weight units (plural) + # wl.weight.units : added with add_more_key_quants + # wl.weight.min <- # minimum weight # wl.weight.max <- # maximum weight + ## maturity schedule (proportion mature)- don't code quantities yet - # prop.mat.length.units <- # length units (plural) + # length units (plural) + # prop.mat.length.units : added with add_more_key_quants + # prop.mat.length.min <- # minimum length # prop.mat.length.max <- # maximum length + ## fecundity at length- don't code quantities yet - # fecundity.length.units <- # length units (plural) + # length units (plural) + # fecundity.length.units : added with add_more_key_quants + # fecundity.length.min <- # minimum length # fecundity.length.max <- # maximum length - # fecundity.units <- # fecundity units (plural) + + # fecundity units (plural) + # fecundity.units : added with add_more_key_quants + # fecundity.min <- # minimum fecundity # fecundity.max <- # maximum fecundity + ## CAA (catch at age)- don't code quantities yet # fleet.or.survey.name <- # fleet or survey name (SHARED with CAL, below) # caa.age.min <- # minimum age group @@ -329,13 +352,18 @@ write_captions <- function(dat, # converted model output object # cal.length.min <- # minimum length group # cal.length.max <- # maximum length group + ## 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 <- # CPUE units (plural) (SHARED with mod.fit.abun, below) + + # 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 bubble.start.year.min <- dat |> @@ -365,13 +393,18 @@ write_captions <- function(dat, # converted model output object dplyr::select(age) |> as.numeric() + ## 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 - # mod.fit.catch.units <- # catch units (plural) + + # catch units (plural) + # mod.fit.catch.units : added with add_more_key_quants + # 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 <- @@ -408,8 +441,7 @@ write_captions <- function(dat, # converted model output object # as.numeric() # ssb units (plural) - # sr.ssb.units <- # this will take some thought, since - # spawning_biomass_label is entered by the user in plot_spawn_recruitment.R + # sr.ssb.units : added with add_more_key_quants # minimum ssb sr.ssb.min <- dat |> @@ -448,8 +480,7 @@ write_captions <- function(dat, # converted model output object round(digits = 2) # recruitment units (plural) - # sr.units <- # this will take some thought, since recruitment_label - # is entered by the user in plot_spawn_recruitment.R + # sr.units : added with add_more_key_quants # minimum recruitment sr.min <- dat |> @@ -490,8 +521,7 @@ write_captions <- function(dat, # converted model output object ## recruitment ts # recruitment units (plural) - numbers of fish, in thousands - # recruitment.units <- # this will take some thought, since unit_label is - # entered by the user in plot_recruitment.R + # recruitment.units : added with add_more_key_quants # start year of recruitment ts plot recruitment.start.year <- dat |> @@ -511,8 +541,7 @@ write_captions <- function(dat, # converted model output object as.numeric() # end year of recruitment ts plot - # recruitment.end.year <- # this will take some thought, since end_year is - # either entered by the user or calculated in plot_recruitment.R + # recruitment.end.year : added with add_more_key_quants ## relative recruitment @@ -544,8 +573,7 @@ write_captions <- function(dat, # converted model output object as.numeric() # end year of recruitment deviations plot - # recruit.dev.end.year <- # this will take some thought, since end_year is - # either entered by the user or calculated in plot_recruitment_deviations.R + # recruit.dev.end.year : added with add_more_key_quants # minimum recruitment deviation recruit.dev.min <- dat |> @@ -599,12 +627,10 @@ write_captions <- function(dat, # converted model output object round(digits = 2) # end year of ssb plot - # ssb.end.year <- # this will take some thought, since end_year is - # either entered by the user or calculated in plot_spawning_biomass.R + # ssb.end.year : added with add_more_key_quants # ssb units (plural) - # ssb.units <- # this will take some thought, since unit_label is - # entered by the user in plot_spawning_biomass.R + # ssb.units : added with add_more_key_quants # minimum ssb ssb.min <- dat |> @@ -635,8 +661,7 @@ write_captions <- function(dat, # converted model output object round(digits = 2) # ssb reference point - # ssb.ref.pt <- # this will take some thought, since ref_line_val is - # calculated in plot_spawning_biomass.R + # ssb.ref.pt : added with add_more_key_quants ssbtarg <- dat |> dplyr::filter(c(grepl('spawning_biomass', label) & grepl('msy$', label) & estimate >1) | label == 'spawning_biomass_msy$') |> @@ -749,10 +774,7 @@ write_captions <- function(dat, # converted model output object 'rel.B.max' = as.character(rel.B.max), ## Biomass plot - # 'B.ref.pt' = as.character(B.ref.pt), 'B.start.year' = as.character(B.start.year), - # 'B.end.year' = as.character(B.end.year), - # 'B.units' = as.character(B.units), 'B.min' = as.character(B.min), 'B.max' = as.character(B.max), 'R0' = as.character(R0), @@ -772,7 +794,6 @@ write_captions <- function(dat, # converted model output object ## landings plot 'landings.start.year' = as.character(landings.start.year), 'landings.end.year' = as.character(landings.end.year), - # 'landings.units' = as.character(landings.units), 'landings.min' = as.character(landings.min), 'landings.max' = as.character(landings.max), @@ -830,7 +851,6 @@ write_captions <- function(dat, # converted model output object ## CPUE indices plot # 'cpue.start.year' = as.character(cpue.start.year), # 'cpue.end.year' = as.character(cpue.end.year), - # 'cpue.units' = as.character(cpue.units), # 'cpue.min' = as.character(cpue.min), # 'cpue.max' = as.character(cpue.max), @@ -867,10 +887,8 @@ write_captions <- function(dat, # converted model output object ## estimated stock recruitment (aka spawning stock biomass) # 'sr.age.min' = as.character(sr.age.min), - # 'sr.ssb.units' = as.character(sr.ssb.units), 'sr.ssb.min' = as.character(sr.ssb.min), 'sr.ssb.max' = as.character(sr.ssb.max), - # 'sr.units' = as.character(sr.units), 'sr.min' = as.character(sr.min), 'sr.max' = as.character(sr.max), @@ -881,13 +899,10 @@ write_captions <- function(dat, # converted model output object 'rel.recruitment.max' = as.character(rel.recruitment.max), ## recruitment ts - # 'recruitment.units' = as.character(recruitment.units), 'recruitment.start.year' = as.character(recruitment.start.year), - # 'recruitment.end.year' = as.character(recruitment.end.year), ## recruitment deviations 'recruit.dev.start.year' = as.character(recruit.dev.start.year), - # 'recruit.dev.end.year' = as.character(recruit.dev.end.year), 'recruit.dev.min' = as.character(recruit.dev.min), 'recruit.dev.max' = as.character(recruit.dev.max), @@ -899,11 +914,8 @@ write_captions <- function(dat, # converted model output object ## spawning.biomass (ssb) 'ssb.start.year' = as.character(ssb.start.year), - # 'ssb.end.year' = as.character(ssb.end.year), - # 'ssb.units' = as.character(ssb.units), 'ssb.min' = as.character(ssb.min), 'ssb.max' = as.character(ssb.max), - # 'ssb.ref.pt' = as.character(ssb.ref.pt), # 'ssbtarg' = as.character(ssbtarg), From 42be843b62e991b6b5d7b44700142ac0e566aa7b Mon Sep 17 00:00:00 2001 From: Schiano-NOAA Date: Wed, 15 Jan 2025 17:08:25 -0500 Subject: [PATCH 17/54] Bring dependency and use of nmfspalette back into package --- DESCRIPTION | 3 + R/add_theme.R | 4 +- tests/testthat/test-add_theme.R | 168 ++++++++++++++++---------------- 3 files changed, 89 insertions(+), 86 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6abde2e..f395671 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,12 +32,15 @@ Imports: kableExtra, knitr, naniar, + nmfspalette, officer, stringr, tibble, tidyr, tidyselect, utils +Remotes: + nmfs-fish-tools/nmfspalette Depends: R (>= 2.10) Suggests: diff --git a/R/add_theme.R b/R/add_theme.R index 4a7001d..b2d8041 100644 --- a/R/add_theme.R +++ b/R/add_theme.R @@ -48,8 +48,8 @@ add_theme <- function(x) { # add nmfs color palette (palette will be default) # I believe including both functions is fine and will # works regardless of the type of plot (one won't be used) - # nmfspalette::scale_color_nmfs() + - # nmfspalette::scale_fill_nmfs() + nmfspalette::scale_color_nmfs() + + nmfspalette::scale_fill_nmfs() # Determining how to treat a legend if there is one # check if one is present # check_for_legend <- function(x) { diff --git a/tests/testthat/test-add_theme.R b/tests/testthat/test-add_theme.R index c8ccd1f..e64572b 100644 --- a/tests/testthat/test-add_theme.R +++ b/tests/testthat/test-add_theme.R @@ -35,87 +35,87 @@ test_that("add_theme applies NOAA formatting correctly", { expect_error(add_theme(unsupported_obj2)) }) -# test_that("nmfspalette returns correct scales", { -# # Test 1: Ensure nmfspalette integrates with ggplot color scales -# ggplot_obj <- ggplot2::ggplot(data = cars, ggplot2::aes(x = speed, y = dist, color = speed)) + -# ggplot2::geom_point() + -# nmfspalette::scale_color_nmfs() -# -# # Check if the correct scale is added -# expect_true("ScaleDiscrete" %in% class(ggplot_obj$scales$scales[[1]])) -# expect_true(ggplot_obj$scales$scales[[1]]$aesthetics == "colour") -# -# # Test 2: Ensure fill scale works with ggplot -# ggplot_fill_obj <- ggplot2::ggplot(data = cars, ggplot2::aes(x = factor(speed), fill = factor(speed))) + -# ggplot2::geom_bar() + -# nmfspalette::scale_fill_nmfs() -# -# # Check if the correct fill scale is applied -# expect_true("ScaleDiscrete" %in% class(ggplot_fill_obj$scales$scales[[1]])) -# expect_true(ggplot_fill_obj$scales$scales[[1]]$aesthetics == "fill") -# }) -# -# test_that("nmfspalette scales are applied to ggplot object", { -# p <- ggplot2::ggplot(mtcars, ggplot2::aes(x = wt, y = mpg, color = as.factor(cyl))) + -# ggplot2::geom_point() -# formatted_plot <- add_theme(p) -# -# # Extract the color and fill scales from the plot -# scales <- formatted_plot$scales$scales -# -# # Check if the color scale is from nmfspalette -# color_scale <- scales[[1]] -# expect_true("ScaleDiscrete" %in% class(color_scale)) # It's a discrete scale -# expect_true(grepl("nmfs", deparse(color_scale$call))) # Check if nmfspalette scale is used -# -# # Ensure the color scale is the default "oceans" nmfspalette scale -# test_plot <- ggplot2::ggplot( -# Orange, -# ggplot2::aes( -# x = age, -# y = circumference, -# color = Tree -# ) -# ) + -# ggplot2::geom_smooth( -# se = F, -# method = "loess", -# formula = "y ~ x" -# ) -# -# ## use default color palette -# plot_default <- test_plot + -# nmfspalette::scale_color_nmfs() -# -# ## use oceans color palette -# plot_oceans <- test_plot + -# nmfspalette::scale_color_nmfs("oceans", 5) -# -# ## extract colors used for both plots -# plot_default_build <- ggplot2::ggplot_build(plot_default) -# colors_default <- unique(plot_default_build$data[[1]]["colour"]) -# -# plot_oceans_build <- ggplot2::ggplot_build(plot_oceans) -# colors_oceans <- unique(plot_oceans_build$data[[1]]["colour"]) -# -# ## test if plots' colors are identical -# expect_equal(colors_default, colors_oceans) # Check if the colors match -# }) -# -# # Test for both color and fill scales -# test_that("nmfspalette scales apply both color and fill scales", { -# p <- ggplot2::ggplot(mtcars, ggplot2::aes(x = wt, y = mpg, color = as.factor(cyl), fill = as.factor(cyl))) + -# ggplot2::geom_point() -# formatted_plot <- add_theme(p) -# -# # Extract the scales from the plot -# scales <- formatted_plot$scales$scales -# -# # Check color scale -# color_scale <- scales[[1]] -# expect_true(grepl("nmfs", deparse(color_scale$call))) # Color scale from nmfspalette -# -# # Check fill scale -# fill_scale <- scales[[2]] -# expect_true(grepl("nmfs", deparse(fill_scale$call))) # Fill scale from nmfspalette -# }) +test_that("nmfspalette returns correct scales", { + # Test 1: Ensure nmfspalette integrates with ggplot color scales + ggplot_obj <- ggplot2::ggplot(data = cars, ggplot2::aes(x = speed, y = dist, color = speed)) + + ggplot2::geom_point() + + nmfspalette::scale_color_nmfs() + + # Check if the correct scale is added + expect_true("ScaleDiscrete" %in% class(ggplot_obj$scales$scales[[1]])) + expect_true(ggplot_obj$scales$scales[[1]]$aesthetics == "colour") + + # Test 2: Ensure fill scale works with ggplot + ggplot_fill_obj <- ggplot2::ggplot(data = cars, ggplot2::aes(x = factor(speed), fill = factor(speed))) + + ggplot2::geom_bar() + + nmfspalette::scale_fill_nmfs() + + # Check if the correct fill scale is applied + expect_true("ScaleDiscrete" %in% class(ggplot_fill_obj$scales$scales[[1]])) + expect_true(ggplot_fill_obj$scales$scales[[1]]$aesthetics == "fill") +}) + +test_that("nmfspalette scales are applied to ggplot object", { + p <- ggplot2::ggplot(mtcars, ggplot2::aes(x = wt, y = mpg, color = as.factor(cyl))) + + ggplot2::geom_point() + formatted_plot <- add_theme(p) + + # Extract the color and fill scales from the plot + scales <- formatted_plot$scales$scales + + # Check if the color scale is from nmfspalette + color_scale <- scales[[1]] + expect_true("ScaleDiscrete" %in% class(color_scale)) # It's a discrete scale + expect_true(grepl("nmfs", deparse(color_scale$call))) # Check if nmfspalette scale is used + + # Ensure the color scale is the default "oceans" nmfspalette scale + test_plot <- ggplot2::ggplot( + Orange, + ggplot2::aes( + x = age, + y = circumference, + color = Tree + ) + ) + + ggplot2::geom_smooth( + se = F, + method = "loess", + formula = "y ~ x" + ) + + ## use default color palette + plot_default <- test_plot + + nmfspalette::scale_color_nmfs() + + ## use oceans color palette + plot_oceans <- test_plot + + nmfspalette::scale_color_nmfs("oceans", 5) + + ## extract colors used for both plots + plot_default_build <- ggplot2::ggplot_build(plot_default) + colors_default <- unique(plot_default_build$data[[1]]["colour"]) + + plot_oceans_build <- ggplot2::ggplot_build(plot_oceans) + colors_oceans <- unique(plot_oceans_build$data[[1]]["colour"]) + + ## test if plots' colors are identical + expect_equal(colors_default, colors_oceans) # Check if the colors match +}) + +# Test for both color and fill scales +test_that("nmfspalette scales apply both color and fill scales", { + p <- ggplot2::ggplot(mtcars, ggplot2::aes(x = wt, y = mpg, color = as.factor(cyl), fill = as.factor(cyl))) + + ggplot2::geom_point() + formatted_plot <- add_theme(p) + + # Extract the scales from the plot + scales <- formatted_plot$scales$scales + + # Check color scale + color_scale <- scales[[1]] + expect_true(grepl("nmfs", deparse(color_scale$call))) # Color scale from nmfspalette + + # Check fill scale + fill_scale <- scales[[2]] + expect_true(grepl("nmfs", deparse(fill_scale$call))) # Fill scale from nmfspalette +}) From 8cab783920dccf5f372ba514b1282e8f91f30cb0 Mon Sep 17 00:00:00 2001 From: Schiano-NOAA Date: Thu, 16 Jan 2025 09:04:24 -0500 Subject: [PATCH 18/54] Missed uncommenting + in code - resulted in failed tests --- R/add_theme.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/add_theme.R b/R/add_theme.R index b2d8041..9d0f2ce 100644 --- a/R/add_theme.R +++ b/R/add_theme.R @@ -44,7 +44,7 @@ add_theme <- function(x) { panel.grid = ggplot2::element_blank(), panel.border = ggplot2::element_rect(colour = "black", fill = NA, linewidth = 0.5) # text = ggplot2::element_text(size = 12, family = "Cambria") - )# + + ) + # add nmfs color palette (palette will be default) # I believe including both functions is fine and will # works regardless of the type of plot (one won't be used) From e5e0c2be2366098754348e6756d85a7e65b64385 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 16 Jan 2025 11:56:47 -0500 Subject: [PATCH 19/54] coded more key quantities --- R/write_captions.R | 71 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 0216dc0..10b550a 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -159,23 +159,50 @@ write_captions <- function(dat, # converted model output object # round(digits = 2) # minimum F - # F.min <- dat |> - # dplyr::filter(label == 'fishing_mortality') - # - # if (F.min$age) - # - # dplyr::slice(which.min(year)) |> - # as.numeric() |> - # round(digits = 2) - # --if age = na, then take min(time series) - # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) |> - # pull(min(val)) + 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 = min(estimate)) |> + dplyr::slice(which.min(val)) |> + dplyr::select(val) |> + as.numeric() |> + round(digits = 2) + } + # maximum F - # F.max <- - # --if age = na, then take max(time series) - # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) - # pull(max(val)) + 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() |> + signif(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() |> + signif(2) + } + # fishing mortality at msy Ftarg <- dat |> @@ -434,11 +461,11 @@ write_captions <- function(dat, # converted model output object ## 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() + 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 @@ -786,8 +813,8 @@ write_captions <- function(dat, # converted model output object 'F.ref.pt' = as.character(F.ref.pt), 'F.start.year' = as.character(F.start.year), # 'F.end.year' = as.character(F.end.year), - # 'F.min' = as.character(F.min), - # 'F.max' = as.character(F.max), + 'F.min' = as.character(F.min), + 'F.max' = as.character(F.max), 'Ftarg' = as.character(Ftarg), # 'F.Ftarg' = as.character(F.Ftarg), From a1f398937f10f6ff280956f86c57dfaf5429f0f8 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 16 Jan 2025 11:56:58 -0500 Subject: [PATCH 20/54] started "manual" vignette --- .gitignore | 1 + DESCRIPTION | 2 ++ vignettes/.gitignore | 2 ++ vignettes/manual.Rmd | 25 +++++++++++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 vignettes/.gitignore create mode 100644 vignettes/manual.Rmd diff --git a/.gitignore b/.gitignore index 7f7c052..d970e30 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ captions_alt_text.csv converted_file_1126.csv rda_files petrale_sole_std_output.csv +inst/doc diff --git a/DESCRIPTION b/DESCRIPTION index 6abde2e..2a46536 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -41,7 +41,9 @@ Imports: Depends: R (>= 2.10) Suggests: + rmarkdown, testthat (>= 3.0.0) Config/testthat/edition: 3 Config/testthat/parallel: false Language: en-US +VignetteBuilder: knitr diff --git a/vignettes/.gitignore b/vignettes/.gitignore new file mode 100644 index 0000000..097b241 --- /dev/null +++ b/vignettes/.gitignore @@ -0,0 +1,2 @@ +*.html +*.R diff --git a/vignettes/manual.Rmd b/vignettes/manual.Rmd new file mode 100644 index 0000000..12a96da --- /dev/null +++ b/vignettes/manual.Rmd @@ -0,0 +1,25 @@ +--- +title: "manual" +output: + html_document: + toc: true +vignette: > + %\VignetteIndexEntry{manual} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +``` + +# General manual + +This manual is a work in progress. In the meantime, please read the function documentation and refer to the documentation of [the companion package, `asar`](https://github.com/nmfs-ost/asar). + +## Updating alternative text and captions + + From 442e89c5c569e3b2a001e8786393d55ac3702655 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 16 Jan 2025 17:21:44 -0500 Subject: [PATCH 21/54] Changed projected biomass csv entry from figure to table; updated key quantities coding in write_captions --- R/write_captions.R | 187 ++++++++---------- inst/resources/captions_alt_text_template.csv | 2 +- 2 files changed, 85 insertions(+), 104 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 10b550a..6aef8df 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -24,6 +24,11 @@ write_captions <- function(dat, # converted model output object system.file("resources", "captions_alt_text_template.csv", package = "satf") ) + 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. @@ -63,7 +68,6 @@ write_captions <- function(dat, # converted model output object module_name == "TIME_SERIES" | module_name == "t.series", # SS3 and BAM target module names is.na(fleet), is.na(age)) |> - dplyr::mutate(estimate = as.numeric(estimate)) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() @@ -80,7 +84,6 @@ write_captions <- function(dat, # converted model output object module_name == "TIME_SERIES" | module_name == "t.series", # SS3 and BAM target module names is.na(fleet), is.na(age)) |> - dplyr::mutate(estimate = as.numeric(estimate)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -92,7 +95,6 @@ write_captions <- function(dat, # converted model output object module_name == "TIME_SERIES" | module_name == "t.series", # SS3 and BAM target module names is.na(fleet), is.na(age)) |> - dplyr::mutate(estimate = as.numeric(estimate)) |> dplyr::slice(which.max(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -173,7 +175,7 @@ write_captions <- function(dat, # converted model output object } else { F.min <- F.min |> dplyr::group_by(age)|> - dplyr::summarize(val = min(estimate)) |> + dplyr::summarize(val = max(estimate)) |> dplyr::slice(which.min(val)) |> dplyr::select(val) |> as.numeric() |> @@ -191,7 +193,7 @@ write_captions <- function(dat, # converted model output object dplyr::filter(module_name %in% c("TIME_SERIES","t.series")) |> dplyr::slice(which.max(estimate)) |> as.numeric() |> - signif(2) + round(digits = 2) } } else { F.max <- F.max |> @@ -200,7 +202,7 @@ write_captions <- function(dat, # converted model output object dplyr::slice(which.max(val)) |> dplyr::select(val) |> as.numeric() |> - signif(2) + round(digits = 2) } @@ -227,9 +229,6 @@ write_captions <- function(dat, # converted model output object # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output !is.na(fleet) ) |> - dplyr::mutate( - year = as.numeric(year) - ) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() @@ -241,9 +240,6 @@ write_captions <- function(dat, # converted model output object # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output !is.na(fleet) ) |> - dplyr::mutate( - year = as.numeric(year) - ) |> dplyr::slice(which.max(year)) |> dplyr::select(year) |> as.numeric() @@ -258,10 +254,6 @@ write_captions <- function(dat, # converted model output object # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output !is.na(fleet) ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year) - ) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -274,17 +266,13 @@ write_captions <- function(dat, # converted model output object # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output !is.na(fleet) ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year) - ) |> dplyr::slice(which.max(estimate)) |> dplyr::select(estimate) |> as.numeric() |> round(digits = 2) - ## natural mortality (M) - ## Should this be filtered by label = natural_mortality too? + ## natural mortality (M)- bam examples have label as natural_mortality + ## but other formats don't (in input) (use if statement) # minimum age of M # M.age.min <- dat |> # dplyr::select(age) |> @@ -300,13 +288,13 @@ write_captions <- function(dat, # converted model output object # as.numeric() # minimum M rate - # The "petrale_sole_std_output.csv" doesn't have a natural_mortality label... - # doesn't work # M.rate.min <- dat |> # dplyr::filter( # grepl("natural_mortality", label)) |> # -label = natural_mortality (min); est in est col - # M.rate.max <- # maximum M rate + + # maximum M rate + # M.rate.max <- # -label = natural_mortality (min); est in est col @@ -481,8 +469,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -499,8 +485,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.max(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -520,8 +504,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -538,8 +520,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.max(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -561,8 +541,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() @@ -593,8 +571,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() @@ -613,8 +589,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -645,9 +619,6 @@ write_captions <- function(dat, # converted model output object label == "spawning_biomass", module_name %in% c("DERIVED_QUANTITIES", "t.series") ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() |> @@ -665,9 +636,6 @@ write_captions <- function(dat, # converted model output object label == "spawning_biomass", module_name %in% c("DERIVED_QUANTITIES", "t.series") ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -679,9 +647,6 @@ write_captions <- function(dat, # converted model output object label == "spawning_biomass", module_name %in% c("DERIVED_QUANTITIES", "t.series") ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.max(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -732,35 +697,67 @@ write_captions <- function(dat, # converted model output object as.numeric() |> round(digits = 2) - #TODO: LEFT OFF HERE (identifying how to extract quantities with Sam, Steve) + ## pop_naa_baa (population numbers at age and population biomass at age) - # pop.naa.baa.start.year <- # start year of spr plot - # pop.naa.baa.end.year <- # end year of spr plot - # pop.naa.baa.fish.min <- # minimum number of fish - # pop.naa.baa.fish.max <- # maximum number of fish + # start year of pop_naa_baa plot + pop.naa.baa.start.year <- dat |> + dplyr::filter(label == "abundance") |> + dplyr::slice(which.min(year)) |> + dplyr::select(year) |> + as.numeric() + + # end year of pop_naa_baa plot + pop.naa.baa.end.year <- landings.end.year + + # minimum number of fish + pop.naa.baa.fish.min <- dat |> + dplyr::filter(label == "abundance") |> + dplyr::group_by(year) |> + dplyr::summarize(val = max(estimate)) |> + dplyr::slice(which.min(val)) |> + dplyr::select(val) |> + as.numeric() |> + round(digits = 2) + + # maximum number of fish + pop.naa.baa.fish.max <- dat |> + dplyr::filter(label == "abundance") |> + dplyr::group_by(year) |> + dplyr::summarize(val = max(estimate)) |> + dplyr::slice(which.max(val)) |> + dplyr::select(val) |> + as.numeric() |> + round(digits = 2) + ## proj_catch (projected catch) - # proj.catch.units <- # projected catch units (plural) - # proj.catch.start.year <- # start year of projected catch plot - # proj.catch.end.year <- # end year of projected catch plot - # proj.catch.min <- # minimum projected catch - # proj.catch.max <- # maximum projected catch - - ## proj_biomass (projected biomass) - # proj.biomass.units <- # projected biomass units (plural) - # proj.biomass.start.year <- # start year of projected biomass plot - # proj.biomass.end.year <- # end year of projected biomass plot - # proj.biomass.min <- # minimum projected biomass - # proj.biomass.max <- # maximum projected biomass - # proj.biomass.ref.pt <- # projected biomass reference point - # proj.biomass.ref.pt.units <- # projected biomass reference point units - - ## Other - # tot.catch <- - # M <- - # steep <- - # SBmsy <- - # fSB <- + # 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 + + # 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 |> + 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----- @@ -949,38 +946,21 @@ write_captions <- function(dat, # converted model output object ## spr (spawning potential ratio) 'spr.min' = as.character(spr.min), 'spr.max' = as.character(spr.max), - 'spr.ref.pt' = as.character(spr.ref.pt)#, + 'spr.ref.pt' = as.character(spr.ref.pt), # ## pop_naa_baa (population numbers at age and population biomass at age) - # 'pop.naa.baa.start.year' = as.character(pop.naa.baa.start.year), - # 'pop.naa.baa.end.year' = as.character(pop.naa.baa.end.year), - # 'pop.naa.baa.fish.min' = as.character(pop.naa.baa.fish.min), - # 'pop.naa.baa.fish.max' = as.character(pop.naa.baa.fish.max), - # - # ## proj_catch (projected catch) + 'pop.naa.baa.start.year' = as.character(pop.naa.baa.start.year), + 'pop.naa.baa.end.year' = as.character(pop.naa.baa.end.year), + 'pop.naa.baa.fish.min' = as.character(pop.naa.baa.fish.min), + 'pop.naa.baa.fish.max' = as.character(pop.naa.baa.fish.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), - # - # ## proj_biomass (projected biomass) - # 'proj.biomass.units' = as.character(proj.biomass.units), - # 'proj.biomass.start.year' = as.character(proj.biomass.start.year), - # 'proj.biomass.end.year' = as.character(proj.biomass.end.year), - # 'proj.biomass.min' = as.character(proj.biomass.min), - # 'proj.biomass.max' = as.character(proj.biomass.max), - # 'proj.biomass.ref.pt' = as.character(proj.biomass.ref.pt), - # 'proj.biomass.ref.pt.units' = as.character(proj.biomass.ref.pt.units), - # - # ## Other - # 'tot.catch' = as.character(tot.catch), - # 'M' = as.character(M), - # 'steep' = as.character(steep), - # 'SBmsy' = as.character(SBmsy), - # 'fSB' = as.character(fSB), - # 'sbtarg' = as.character(sbtarg), - # + '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 @@ -994,6 +974,7 @@ write_captions <- function(dat, # converted model output object # # ## catchability # 'catchability.fleet' = as.character(catchability.fleet) + ) # take the values associated with the quantities and replace the df's diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index 6394a7a..62be9b3 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -28,7 +28,7 @@ relative.spawning.biomass,figure,Model-estimated relative spawning stock biomass spr,figure,Model-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 model-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 model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." pop.naa.baa,figure,Model-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 model-estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.naa.baa.start.year to pop.naa.baa.end.year . The y axis shows numbers of fish, which spans from pop.naa.baa.fish.min to pop.naa.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,figure,Forecasted biomass in proj.biomass.units over future years for different fishing mortality scenarios as indicated in the legend. The horizontal dashed line represents the biomass limit reference point. ,"Time series line graph showing forecasted biomass over future years for different fishing mortality scenarios. The horizontal dashed line represents the biomass limit reference point. The x axis shows the year, which spans from proj.biomass.start.year to proj.biomass.end.year . The y axis shows biomass in proj.biomass.units, which spans from proj.biomass.min to proj.biomass.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. , catch,table,Observed (points) and model estimated (line) catch over time for fleet catch.fleet., From 24679edc1b033ab3d694c29015c56166cffd7db3 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 17 Jan 2025 11:33:42 -0500 Subject: [PATCH 22/54] Started updating manual with documentation for new fxn --- vignettes/manual.Rmd | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vignettes/manual.Rmd b/vignettes/manual.Rmd index 12a96da..5844e0e 100644 --- a/vignettes/manual.Rmd +++ b/vignettes/manual.Rmd @@ -22,4 +22,16 @@ This manual is a work in progress. In the meantime, please read the function doc ## Updating alternative text and captions +When a plotting function (e.g., `plot_biomass()`, `table_indices()`, `exp_all_figs_tables()`) is run, placeholders within the plot's associated alternative text and captions are replaced with key quantities (see the [`asar` package's Accessibility vignette](https://nmfs-ost.github.io/asar/articles/accessibility_guide.html) and/or the [`satf::write_captions()` function](https://github.com/nmfs-ost/satf/blob/master/R/write_captions.R) for more details). As stated in the Accessibility vignette, *you should always check that these alternative texts and captions have been assembled correctly.* +There are multiple reasons why you may want to edit these. For instance, maybe you entered an incorrect value for the units when running `plot_biomass()`. Maybe you need to add some extra information to the alternative text accurately describe the plot. In any case, **here's what to do if you want to alter the alternative text and/or captions in `satf`**, depending on which plotting function you're using: + +### `exp_all_figs_tables()` + +1. Delete the captions_alt_text.csv file + +2. Rerun `exp_all_figs_tables()` with the correct arguments + +### individual fxns + +-edit the rda directly (easier if editing, for instance, units instead of an extracted key quantity) -replace the fxn's existing row in the caps_alt_text.csv with the analogous row from the alt text/csv template, then rerun the plotting fxn so that the KQs can be replaced correctly From b5dc676f48740bbafdfd3b7b2789370d25a465a5 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 17 Jan 2025 11:50:13 -0500 Subject: [PATCH 23/54] fix(table_indices): add missing extract_caps_alttext argument --- R/table_indices.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/table_indices.R b/R/table_indices.R index 6d40529..fbc6869 100644 --- a/R/table_indices.R +++ b/R/table_indices.R @@ -121,7 +121,8 @@ table_indices <- function( # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, - fig_or_table = fig_or_table) + fig_or_table = fig_or_table, + dir = rda_dir) export_rda(final = final, From 5ab2a8f9c3beb1216e9e8f6e8aaa29f6e032b8e0 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 17 Jan 2025 15:18:26 -0500 Subject: [PATCH 24/54] Finished updating new section in manual --- vignettes/manual.Rmd | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/vignettes/manual.Rmd b/vignettes/manual.Rmd index 5844e0e..963df88 100644 --- a/vignettes/manual.Rmd +++ b/vignettes/manual.Rmd @@ -24,14 +24,50 @@ This manual is a work in progress. In the meantime, please read the function doc When a plotting function (e.g., `plot_biomass()`, `table_indices()`, `exp_all_figs_tables()`) is run, placeholders within the plot's associated alternative text and captions are replaced with key quantities (see the [`asar` package's Accessibility vignette](https://nmfs-ost.github.io/asar/articles/accessibility_guide.html) and/or the [`satf::write_captions()` function](https://github.com/nmfs-ost/satf/blob/master/R/write_captions.R) for more details). As stated in the Accessibility vignette, *you should always check that these alternative texts and captions have been assembled correctly.* -There are multiple reasons why you may want to edit these. For instance, maybe you entered an incorrect value for the units when running `plot_biomass()`. Maybe you need to add some extra information to the alternative text accurately describe the plot. In any case, **here's what to do if you want to alter the alternative text and/or captions in `satf`**, depending on which plotting function you're using: +There are multiple reasons why you may want to edit these. For instance, maybe you entered an incorrect value for the units when running `plot_biomass()`. Maybe you need to add some extra information to the alternative text accurately describe the plot. In any case, here's what to do if you want to alter the alternative text and/or captions in `satf`, depending on what you wish to change: -### `exp_all_figs_tables()` +### I want to add or remove text + +Directly edit the rda's caption or alt_text objects. To do so, assign a new value to the text you wish to change. For example: + +1. Locate your rda file. It may be called something like "biomass_figure.rda". + +2. Load it into your R environment. You can do this by clicking the file or by using a command like this: `load("biomass_figure.rda")`. It will probably be called `rda`. + +3. If you want to change the caption to "my new caption", you'd enter the following command: `rda[["cap"]] <- "my new caption"`. To change the alt text, you'd change "cap" to "alt_text" (e.g., `rda[["alt_text"]] <- "my new alt text"`.). + +4. Save the changes to the rda's file (most likely you'll want to overwrite your original rda) by entering the following command: `save(rda, file = 'biomass_figure.rda')`. + + +**NOTE: Your edits will be lost if:** + +- You regenerate (overwrite) the captions_alt_text.csv file +- You rerun `exp_all_figs_tables()` or that plot's function with the `make_rda` argument = T after regenerating the captions_alt_text.csv file. + +We recommend saving a version of your altered text elsewhere to ensure you can reuse it if it were to be overwritten. + +### I want to change/update the arguments or key quantities + +#### `exp_all_figs_tables()` 1. Delete the captions_alt_text.csv file 2. Rerun `exp_all_figs_tables()` with the correct arguments -### individual fxns +**NOTE**: It is possible to directly edit the captions_alt_text.csv file to change the caption or alternative text. However, if you wish to do anything besides adding or removing text, we recommend using the two-step workflow, above, to avoid potential issues where an incorrect argument was used to calculate other (now incorrect) key quantities. For instance, `end_year` may be used to calculate values associated with F, which are then used to calculate other values. + +#### Individual functions (e.g., `plot_biomass()`) + +1. Open your function's R file and find the `topic_label` object. If you set the `relative` argument to TRUE, then choose the `topic_label` with "relative" in it (e.g., "relative.biomass"). + +2. Open your caps_alt_text.csv file. + +3. Find the row in the caps_alt_text.csv file where the "label" column is the `topic_label`. Delete it. + +4. Open the inst/resources/captions_alt_text_template.csv file. Find the row where the "label" column is the `topic_label`. Copy that row. + +5. Paste the row into the caps_alt_text.csv file. + +6. Rerun the function so that the key quantities are replaced correctly. --edit the rda directly (easier if editing, for instance, units instead of an extracted key quantity) -replace the fxn's existing row in the caps_alt_text.csv with the analogous row from the alt text/csv template, then rerun the plotting fxn so that the KQs can be replaced correctly +This process ensures that only the alt text and/or captions for your specific function are being changed, and not those associated with other plotting functions. From 59be9e10bcbc48a07d91c26bf835c90c9f1ea5df Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 17 Jan 2025 17:00:58 -0500 Subject: [PATCH 25/54] Updating F-related key quantities' calculations --- R/utils.R | 39 +++++++++++++++++++++++++++++++++++++++ R/write_captions.R | 25 ++++++++++--------------- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/R/utils.R b/R/utils.R index 10689fd..99e474f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -90,6 +90,45 @@ add_more_key_quants <- function( dplyr::filter(label == topic, type == fig_or_table) + + # calculate key quantities that rely on end_year for calculation + ## terminal fishing mortality + if (topic_cap_alt$label == "fishing.mortality") { + + 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) + } + } + + # replace placeholders (e.g., if "end.year" is found in topic_alt, replace it with end_year) ## end_year----- if(!is.null(end_year)){ diff --git a/R/write_captions.R b/R/write_captions.R index 6aef8df..00b5410 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -135,8 +135,15 @@ write_captions <- function(dat, # converted model output object dplyr::filter( label == stringr::str_to_lower("F_targ") | label == stringr::str_to_lower("F_proxy") | - label == stringr::str_to_lower("F_msy") + 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) @@ -149,16 +156,7 @@ write_captions <- function(dat, # converted model output object as.numeric() # terminal fishing mortality - # TODO: Put this in utils function, since F.end.year needs end_year - # 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) + # F.end.year : added with add_more_key_quants # minimum F F.min <- dat |> @@ -216,8 +214,7 @@ write_captions <- function(dat, # converted model output object round(digits = 2) # Terminal year F respective to F target - # TODO: Put this in utils function, since F.end.year needs end_year - # F.Ftarg <- F.end.year / Ftarg + # F.Ftarg : added with add_more_key_quants ## landings plot @@ -809,11 +806,9 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot 'F.ref.pt' = as.character(F.ref.pt), 'F.start.year' = as.character(F.start.year), - # 'F.end.year' = as.character(F.end.year), 'F.min' = as.character(F.min), 'F.max' = as.character(F.max), 'Ftarg' = as.character(Ftarg), - # 'F.Ftarg' = as.character(F.Ftarg), ## landings plot 'landings.start.year' = as.character(landings.start.year), From a5191ee66284eceaeff4d9f0bd7ca7954820c2fe Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Tue, 21 Jan 2025 11:32:54 -0500 Subject: [PATCH 26/54] Moving utils functions into their own .R files as an attempt towards resolving https://github.com/nmfs-ost/asar/issues/144 --- NAMESPACE | 2 ++ R/export_rda.R | 67 +++++++++++++++++++++++++++++++++++++ R/extract_caps_alttext.R | 57 +++++++++++++++++++++++++++++++ R/utils.R | 61 --------------------------------- man/export_rda.Rd | 56 +++++++++++++++++++++++++++++++ man/extract_caps_alttext.Rd | 35 +++++++++++++++++++ 6 files changed, 217 insertions(+), 61 deletions(-) create mode 100644 R/export_rda.R create mode 100644 R/extract_caps_alttext.R create mode 100644 man/export_rda.Rd create mode 100644 man/extract_caps_alttext.Rd diff --git a/NAMESPACE b/NAMESPACE index 246e3ec..4029ced 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,8 @@ export(add_theme) export(exp_all_figs_tables) +export(export_rda) +export(extract_caps_alttext) export(plot_biomass) export(plot_indices) export(plot_landings) diff --git a/R/export_rda.R b/R/export_rda.R new file mode 100644 index 0000000..9257d8b --- /dev/null +++ b/R/export_rda.R @@ -0,0 +1,67 @@ +#' Export a figure or table to rda +#' +#' Export a figure/table, and its caption and alternative text, to an rda object. +#' Typically used after satf::extract_caps_alttext(). +#' +#' @param final The final figure (ggplot) or table (flextable) object. +#' @param caps_alttext The object containing a figure's caption and alternative +#' text, in a list, or a table's caption, likely generated with +#' satf::extract_caps_alttext(). +#' @param rda_dir If the user has already created a folder containing .rda +#' files with figures, tables, alt text, and captions, rda_dir represents +#' the location of the folder containing these .rda files ("rda_files"). +#' Otherwise, an "rda_files" folder will be created automatically, then used +#' to store the exported rda files. +##' @param topic_label A string that describes a figure or table's label. These +#' labels are found in the "label" column of the "captions_alt_text.csv" file +#' and are used to link the figure or table with its caption/alt text. +#' @param fig_or_table A string describing whether the plot is a figure or table. +#' +#' @return An rda file with a figure's ggplot, caption, and alternative text, or +#' a table's flextable and caption. +#' +#' @export +#' +#' @examples +#'\dontrun{ +#' export_rda(final = final_table_object, +#' caps_alttext = caps_alttext_object, +#' rda_dir = here::here(), +#' topic_label = "bnc", +#' fig_or_table = "table") +#' +#' export_rda(final = final_figure_object, +#' caps_alttext = another_caps_alttext_object, +#' rda_dir = "my_rda_dir", +#' topic_label = "landings", +#' fig_or_table = "figure") +#'} + +export_rda <- function(final = NULL, + caps_alttext = NULL, + rda_dir = NULL, + topic_label = NULL, + fig_or_table = NULL){ + + # make rda for figures + if (fig_or_table == "figure") { + rda <- list("figure" = final, + "cap" = caps_alttext[[1]], + "alt_text" = caps_alttext[[2]]) + # make rda for tables + } else if (fig_or_table == "table"){ + rda <- list("table" = final, + "cap" = caps_alttext[[1]]) + } + + # check if an rda_files folder already exists; if not, make one + if (!dir.exists(fs::path(rda_dir, "rda_files"))) { + dir.create(fs::path(rda_dir, "rda_files")) + } + + # export rda + save(rda, + file = fs::path(rda_dir, + "rda_files", + paste0(topic_label, "_", fig_or_table, ".rda"))) +} diff --git a/R/extract_caps_alttext.R b/R/extract_caps_alttext.R new file mode 100644 index 0000000..8eb2d7c --- /dev/null +++ b/R/extract_caps_alttext.R @@ -0,0 +1,57 @@ +#' Extract captions and alternative texts +#' +#' Extract a figure or table's caption and alternative text for usage when +#' generating a figure or table. Typically used before satf::export_rda(). +#' +#' @param topic_label A string that describes a figure or table's label. These +#' labels are found in the "label" column of the "captions_alt_text.csv" file +#' and are used to link the figure or table with its caption/alt text. +#' @param fig_or_table A string describing whether the plot is a figure or table. +#' @param dir The directory containing the "captions_alt_text.csv" file. +#' +#' @return A figure's caption and alternative text, in a list, or a table's caption. +#' +#' @export +#' +#' @examples +#'\dontrun{ +#'extract_caps_alttext(topic_label = "biomass", +#' fig_or_table = "figure", +#' dir = here::here()) +#' +#'extract_caps_alttext(topic_label = "bnc", +#' fig_or_table = "table", +#' dir = getwd()) +#'} + +extract_caps_alttext <- function(topic_label = NULL, + fig_or_table = NULL, + dir = getwd()){ + + # import csv with captions and alt text + captions_alttext_df <- utils::read.csv( + fs::path(dir, "captions_alt_text.csv") + ) + + # extract plot or table's caption and alt text + cap <- captions_alttext_df |> + dplyr::filter(label == topic_label, + type == fig_or_table) |> + dplyr::select(caption) |> + as.character() + + if (fig_or_table == "figure"){ + alt_text <- captions_alttext_df |> + dplyr::filter(label == topic_label, + type == "figure") |> + dplyr::select(alt_text) |> + as.character() + + caps_alttext_list <- list(cap, + alt_text) + } else { + caps_alttext_list <- list(cap) + } + + return(caps_alttext_list) +} diff --git a/R/utils.R b/R/utils.R index b450780..51d861f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -2,66 +2,5 @@ # General utility functions #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -# extract captions and alt text from csv in wd -extract_caps_alttext <- function(topic_label, - fig_or_table, - dir = getwd()){ - # import csv with captions and alt text - # Note: make sure to check this later in the workflow and its use - - # I could see it having issues - captions_alttext_df <- utils::read.csv( - fs::path(dir, "captions_alt_text.csv") - ) - # extract plot or table's caption and alt text - cap <- captions_alttext_df |> - dplyr::filter(label == topic_label, - type == fig_or_table) |> - dplyr::select(caption) |> - as.character() - - if (fig_or_table == "figure"){ - alt_text <- captions_alttext_df |> - dplyr::filter(label == topic_label, - type == "figure") |> - dplyr::select(alt_text) |> - as.character() - - caps_alttext_list <- list(cap, - alt_text) - } else { - caps_alttext_list <- list(cap) - } - - return(caps_alttext_list) -} - -# export ggplot, alt text, and caption to rda if indicated in arguments -export_rda <- function(final = final, - caps_alttext = caps_alttext, - rda_dir = rda_dir, - topic_label = topic_label, - fig_or_table = fig_or_table){ - - # make rda for figures - if (fig_or_table == "figure") { - rda <- list("figure" = final, - "cap" = caps_alttext[[1]], - "alt_text" = caps_alttext[[2]]) - # make rda for tables - } else if (fig_or_table == "table"){ - rda <- list("table" = final, - "cap" = caps_alttext[[1]]) - } - - # check if an rda_files folder already exists; if not, make one - if (!dir.exists(fs::path(rda_dir, "rda_files"))) { - dir.create(fs::path(rda_dir, "rda_files")) - } - - save(rda, - file = fs::path(rda_dir, - "rda_files", - paste0(topic_label, "_", fig_or_table, ".rda"))) -} diff --git a/man/export_rda.Rd b/man/export_rda.Rd new file mode 100644 index 0000000..46e5c17 --- /dev/null +++ b/man/export_rda.Rd @@ -0,0 +1,56 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/export_rda.R +\name{export_rda} +\alias{export_rda} +\title{Export a figure or table to rda} +\usage{ +export_rda( + final = NULL, + caps_alttext = NULL, + rda_dir = NULL, + topic_label = NULL, + fig_or_table = NULL +) +} +\arguments{ +\item{final}{The final figure (ggplot) or table (flextable) object.} + +\item{caps_alttext}{The object containing a figure's caption and alternative +text, in a list, or a table's caption, likely generated with +satf::extract_caps_alttext().} + +\item{rda_dir}{If the user has already created a folder containing .rda +files with figures, tables, alt text, and captions, rda_dir represents +the location of the folder containing these .rda files ("rda_files"). +Otherwise, an "rda_files" folder will be created automatically, then used +to store the exported rda files.} + +\item{topic_label}{A string that describes a figure or table's label. These +labels are found in the "label" column of the "captions_alt_text.csv" file +and are used to link the figure or table with its caption/alt text.} + +\item{fig_or_table}{A string describing whether the plot is a figure or table.} +} +\value{ +An rda file with a figure's ggplot, caption, and alternative text, or +a table's flextable and caption. +} +\description{ +Export a figure/table, and its caption and alternative text, to an rda object. +Typically used after satf::extract_caps_alttext(). +} +\examples{ +\dontrun{ +export_rda(final = final_table_object, +caps_alttext = caps_alttext_object, +rda_dir = here::here(), +topic_label = "bnc", +fig_or_table = "table") + +export_rda(final = final_figure_object, +caps_alttext = another_caps_alttext_object, +rda_dir = "my_rda_dir", +topic_label = "landings", +fig_or_table = "figure") +} +} diff --git a/man/extract_caps_alttext.Rd b/man/extract_caps_alttext.Rd new file mode 100644 index 0000000..51d46f7 --- /dev/null +++ b/man/extract_caps_alttext.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/extract_caps_alttext.R +\name{extract_caps_alttext} +\alias{extract_caps_alttext} +\title{Extract captions and alternative texts} +\usage{ +extract_caps_alttext(topic_label = NULL, fig_or_table = NULL, dir = getwd()) +} +\arguments{ +\item{topic_label}{A string that describes a figure or table's label. These +labels are found in the "label" column of the "captions_alt_text.csv" file +and are used to link the figure or table with its caption/alt text.} + +\item{fig_or_table}{A string describing whether the plot is a figure or table.} + +\item{dir}{The directory containing the "captions_alt_text.csv" file.} +} +\value{ +A figure's caption and alternative text, in a list, or a table's caption. +} +\description{ +Extract a figure or table's caption and alternative text for usage when +generating a figure or table. Typically used before satf::export_rda(). +} +\examples{ +\dontrun{ +extract_caps_alttext(topic_label = "biomass", + fig_or_table = "figure", + dir = here::here()) + +extract_caps_alttext(topic_label = "bnc", + fig_or_table = "table", + dir = getwd()) +} +} From 9682059513a1dc78dce6c8e815f7f996b83cac1b Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 22 Jan 2025 10:46:27 -0500 Subject: [PATCH 27/54] Update manual to emphasize importance of checking/editing alt text --- vignettes/manual.Rmd | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/vignettes/manual.Rmd b/vignettes/manual.Rmd index 963df88..4ae9bf7 100644 --- a/vignettes/manual.Rmd +++ b/vignettes/manual.Rmd @@ -22,9 +22,15 @@ This manual is a work in progress. In the meantime, please read the function doc ## Updating alternative text and captions -When a plotting function (e.g., `plot_biomass()`, `table_indices()`, `exp_all_figs_tables()`) is run, placeholders within the plot's associated alternative text and captions are replaced with key quantities (see the [`asar` package's Accessibility vignette](https://nmfs-ost.github.io/asar/articles/accessibility_guide.html) and/or the [`satf::write_captions()` function](https://github.com/nmfs-ost/satf/blob/master/R/write_captions.R) for more details). As stated in the Accessibility vignette, *you should always check that these alternative texts and captions have been assembled correctly.* +When a plotting function (e.g., `plot_biomass()`, `table_indices()`, `exp_all_figs_tables()`) is run, placeholders within the plot's associated alternative text and captions are replaced with key quantities (see the [`asar` package's Accessibility vignette](https://nmfs-ost.github.io/asar/articles/accessibility_guide.html) and/or the [`satf::write_captions()` function](https://github.com/nmfs-ost/satf/blob/master/R/write_captions.R) for more details). -There are multiple reasons why you may want to edit these. For instance, maybe you entered an incorrect value for the units when running `plot_biomass()`. Maybe you need to add some extra information to the alternative text accurately describe the plot. In any case, here's what to do if you want to alter the alternative text and/or captions in `satf`, depending on what you wish to change: +As stated in the [Accessibility vignette's "Your to-do list" section](https://nmfs-ost.github.io/asar/articles/accessibility_guide.html#your-to-do-list), **you should always:** + +**1. Check that these alternative texts and captions have been assembled correctly.** + +**2. Write the final component of each figure’s alt text.** + +Here's how to alter the alternative text and/or captions in `satf`, depending on what you wish to change: ### I want to add or remove text From b45412ce2ed34b60a1ba4dce9cb173f9bcc0480d Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 22 Jan 2025 16:48:35 -0500 Subject: [PATCH 28/54] Created tests for new functions (export_rda and extract_caps_alttext) --- tests/testthat/test-export_rda.R | 96 ++++++++++++++++++++++ tests/testthat/test-extract_caps_alttext.R | 66 +++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 tests/testthat/test-export_rda.R create mode 100644 tests/testthat/test-extract_caps_alttext.R diff --git a/tests/testthat/test-export_rda.R b/tests/testthat/test-export_rda.R new file mode 100644 index 0000000..8eb9f5e --- /dev/null +++ b/tests/testthat/test-export_rda.R @@ -0,0 +1,96 @@ +test_that("export_rda works for figures", { + # read in sample dataset + dat <- utils::read.csv( + system.file( + "resources", + "sample_data", + "petrale_sole-after_2020.csv", + package = "satf" + ) + ) + + topic_label <- "biomass" + fig_or_table <- "figure" + + # run write_captions.R + satf::write_captions(dat = dat, + dir = getwd(), + year = 2022) + + # extract this plot's caption and alt text + caps_alttext <- extract_caps_alttext(topic_label = topic_label, + fig_or_table = fig_or_table, + dir = getwd()) + + # make a simple plot + library(ggplot2) + final <- ggplot2::ggplot(Orange, aes(circumference, age)) + + ggplot2::geom_point() + + # export rda + export_rda( + final = final, + caps_alttext = caps_alttext, + rda_dir = getwd(), + topic_label = topic_label, + fig_or_table = fig_or_table + ) + + # expect that both rda_files dir and the biomass_figure.rda file exist + expect_true(dir.exists(fs::path(getwd(), "rda_files"))) + expect_true(file.exists(fs::path( + getwd(), "rda_files", "biomass_figure.rda" + ))) + + # erase temporary testing files + file.remove(fs::path(getwd(), "captions_alt_text.csv")) + unlink(fs::path(getwd(), "rda_files"), recursive = T) + +}) + +test_that("export_rda works for tables", { + # read in sample dataset + dat <- utils::read.csv( + system.file( + "resources", + "sample_data", + "petrale_sole-after_2020.csv", + package = "satf" + ) + ) + + topic_label <- "bnc" + fig_or_table <- "table" + + # run write_captions.R + satf::write_captions(dat = dat, + dir = getwd(), + year = 2022) + + # extract this plot's caption and alt text + caps_alttext <- extract_caps_alttext(topic_label = topic_label, + fig_or_table = fig_or_table, + dir = getwd()) + + # make a simple table + final <- flextable::flextable(data = data.frame(x = c(1, 2, 3), + y = c(4, 5, 6))) + + # export rda + export_rda( + final = final, + caps_alttext = caps_alttext, + rda_dir = getwd(), + topic_label = topic_label, + fig_or_table = fig_or_table + ) + + # expect that both rda_files dir and the bnc_table.rda file exist + expect_true(dir.exists(fs::path(getwd(), "rda_files"))) + expect_true(file.exists(fs::path(getwd(), "rda_files", "bnc_table.rda"))) + + # erase temporary testing files + file.remove(fs::path(getwd(), "captions_alt_text.csv")) + unlink(fs::path(getwd(), "rda_files"), recursive = T) + +}) diff --git a/tests/testthat/test-extract_caps_alttext.R b/tests/testthat/test-extract_caps_alttext.R new file mode 100644 index 0000000..44bf783 --- /dev/null +++ b/tests/testthat/test-extract_caps_alttext.R @@ -0,0 +1,66 @@ +test_that("extract_caps_alttext works for figures", { + + # read in sample dataset + dat <- utils::read.csv( + system.file("resources", "sample_data", "petrale_sole-after_2020.csv", package = "satf") + ) + + topic_label <- "biomass" + fig_or_table <- "figure" + + # run write_captions.R + satf::write_captions(dat = dat, + dir = getwd(), + year = 2022) + + # extract this plot's caption and alt text + caps_alttext <- extract_caps_alttext(topic_label = topic_label, + fig_or_table = fig_or_table, + dir = getwd()) + + + # expect that the caps_alttext is a list with two objects (caption, alt text) + expect_true(length(caps_alttext) == 2) + + # expect the first 4 words of the caption + expect_true("Biomass (B) time series." == stringr::word(caps_alttext[[1]], 1, 4)) + + # expect the first 3 words of the alt text + expect_true("Line graph showing" == stringr::word(caps_alttext[[2]], 1, 3)) + + # erase temporary testing files + file.remove(fs::path(getwd(), "captions_alt_text.csv")) + +}) + +test_that("extract_caps_alttext works for tables", { + + # read in sample dataset + dat <- utils::read.csv( + system.file("resources", "sample_data", "petrale_sole-after_2020.csv", package = "satf") + ) + + topic_label <- "bnc" + fig_or_table <- "table" + + # run write_captions.R + satf::write_captions(dat = dat, + dir = getwd(), + year = 2022) + + # extract this plot's caption and alt text + caps_alttext <- extract_caps_alttext(topic_label = topic_label, + fig_or_table = fig_or_table, + dir = getwd()) + + + # expect that the caps_alttext is a list with one object (caption) + expect_true(length(caps_alttext) == 1) + + # expect the first 4 words of the caption + expect_true("Historical biomass, abundance, and" == stringr::word(caps_alttext, 1, 4)) + + # erase temporary testing files + file.remove(fs::path(getwd(), "captions_alt_text.csv")) + +}) From ac9bdd925ec130c63717f8bd862ca02b132e4e25 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 23 Jan 2025 14:13:08 -0500 Subject: [PATCH 29/54] Updated calculation for landings plot's y axis max, since it's a stacked area plot --- R/write_captions.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 00b5410..3b7ad96 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -263,8 +263,13 @@ write_captions <- function(dat, # converted model output object # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output !is.na(fleet) ) |> - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> + 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) From e7319305178abc6b2fd2e3f0344cea624dcfbb60 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 10 Jan 2025 14:07:41 -0500 Subject: [PATCH 30/54] first commit --- R/utils.R | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/R/utils.R b/R/utils.R index 51d861f..be9dfaa 100644 --- a/R/utils.R +++ b/R/utils.R @@ -4,3 +4,101 @@ + + # extract plot or table's caption and alt text + cap <- captions_alttext_df |> + dplyr::filter(label == topic_label, + type == fig_or_table) |> + dplyr::select(caption) |> + as.character() + + if (fig_or_table == "figure"){ + alt_text <- captions_alttext_df |> + dplyr::filter(label == topic_label, + type == "figure") |> + dplyr::select(alt_text) |> + as.character() + + caps_alttext_list <- list(cap, + alt_text) + } else { + caps_alttext_list <- list(cap) + } + + return(caps_alttext_list) +} + +# export ggplot, alt text, and caption to rda if indicated in arguments +export_rda <- function(final = final, + caps_alttext = caps_alttext, + rda_dir = rda_dir, + topic_label = topic_label, + fig_or_table = fig_or_table){ + + # make rda for figures + if (fig_or_table == "figure") { + rda <- list("figure" = final, + "cap" = caps_alttext[[1]], + "alt_text" = caps_alttext[[2]]) + # make rda for tables + } else if (fig_or_table == "table"){ + rda <- list("table" = final, + "cap" = caps_alttext[[1]]) + } + + # check if an rda_files folder already exists; if not, make one + if (!dir.exists(fs::path(rda_dir, "rda_files"))) { + dir.create(fs::path(rda_dir, "rda_files")) + } + + save(rda, + file = fs::path(rda_dir, + "rda_files", + paste0(topic_label, "_", fig_or_table, ".rda"))) +} + +# substitute in more key quantities (units and end_years) to captions/alt text +add_yr_units <- function( + topic = topic_label, + fig_or_table = fig_or_table, + end_year = NULL, + units = NULL){ + + # import csv + caps_alt_df <- read.csv(fs::path(getwd(), "captions_alt_text.csv")) + + # make year character if not null + if (!is.null(end_year)){ + end_year <- as.character(end_year) + } + + # select specific fig/table's caption/alt text + topic_cap_alt <- caps_alt_df |> + dplyr::filter(label == topic, + type == fig_or_table) + + # select caption + topic_cap <- topic_cap_alt |> + dplyr::select(caption) + + # select alt text + topic_alt <- topic_cap_alt |> + dplyr::select(alt_text) + + # replace placeholders in alt text + ## end_year + # if "end.year" is found in topic_alt, replace it with end_year + topic_alt <- sub("\\S*end\\.year\\S*", + as.character(end_year), + topic_alt) + + ## units + topic_alt <- sub("\\S*units\\S*", + as.character(units), + topic_alt) + + + + + } + From 5042e73ff58a000258c65cfb7d46aa80f32536f6 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 12:09:35 -0500 Subject: [PATCH 31/54] Added notes and edits from Friday's coworking meeting. Notes explain how to extract more key quantities from model results. Some key quantities were deleted. Alt text/caps csv updated in accordance with changes. --- R/write_captions.R | 115 +++++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 47 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 3215258..309ed5d 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -33,21 +33,28 @@ write_captions <- function(dat, # converted model output object # FIGURES----- - ## kobe plot - # B.BMSY.min <- # minimum value of B/B(MSY) - # B.BMSY.max <- # maximum value of B/B(MSY) - # F.FMSY.min <- # minimum value of F/F(MSY) - # F.FMSY.max <- # maximum value of F/F(MSY) - # B.BMSY.end.yr <- # value of B/B(MSY) at the end year - # F.FMSY.end.yr <- # value of F/F(MSY) at the end year - # overfished.status.is.isnot <- # object that should be "is" or "is not" and answers the question, "the stock overfishing status ... overfished" - # overfishing.status.is.isnot <- # object that should be "is" or "is not" and answers the question, "the stock ... experiencing overfishing" - # kobe.start.year <- # start year of kobe plot - # kobe.end.year <- # end year of kobe plot + ## 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 <- + + # 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 <- + ## Biomass plot - # B.ref.pt <- # biomass reference point- SHARED with kobe plot, above - # B.ref.pt.units <- # biomass reference point unit + # B.ref.pt <- # biomass reference point # start year of biomass plot B.start.year <- dat |> @@ -123,14 +130,24 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot # F.ref.pt <- # F reference point - # F.ref.pt.units <- # F reference point unit + # --(use to_lower): F_targ if it's present. if not, use F_proxy; if not, Fmsy (these are labels in dat) # F.start.year. <- # start year of F plot - # F.end.year <- # start year of F plot - # F.units <- # units of F (plural) + # --like Fend, but minimum year + # F.end.year <- # end year of F plot + # --Fend # F.min <- # minimum F + # --if age = na, then take min(time series) + # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) |> + # pull(min(val)) # F.max <- # maximum F + # --if age = na, then take max(time series) + # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) + # pull(max(val)) # Ftarg <- + # --Ftarg # F.Ftarg <- + # --F_Ftarg + ## landings plot @@ -200,20 +217,22 @@ write_captions <- function(dat, # converted model output object ## natural mortality (M) # M.age.min <- # minimum age of M + # --if age != na, min(age) # M.age.max <- # maximum age of M - # M.units <- # units of M (plural) + # --if age != na, max(age) # M.rate.min <- # minimum M rate + # -label = natural_mortality (min); est in est col # M.rate.max <- # maximum M rate + # -label = natural_mortality (min); est in est col - ## vonB LAA (von Bertalanffy growth function + length at age) - # vonb.age.units <- # vonB age units (plural) + ## 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 <- # vonB length units (plural) # vonb.length.min <- # minimum vonB length # vonb.length.max <- # minimum vonB length - ## length-type conversion plot + ## length-type conversion plot- don't code quantities yet # total.length.units <- # total length units (plural) # total.length.min <- # minimum total length # total.length.max <- # maximum total length @@ -221,7 +240,7 @@ write_captions <- function(dat, # converted model output object # fork.length.min <- # minimum fork length # fork.length.max <- # maximum fork length - ## weight-length conversion plot + ## weight-length conversion plot- don't code quantities yet # wl.length.units <- # length units (plural) # wl.length.min <- # minimum length # wl.length.max <- # maximum length @@ -229,12 +248,12 @@ write_captions <- function(dat, # converted model output object # wl.weight.min <- # minimum weight # wl.weight.max <- # maximum weight - ## maturity schedule (proportion mature) + ## maturity schedule (proportion mature)- don't code quantities yet # prop.mat.length.units <- # length units (plural) # prop.mat.length.min <- # minimum length # prop.mat.length.max <- # maximum length - ## fecundity at length + ## fecundity at length- don't code quantities yet # fecundity.length.units <- # length units (plural) # fecundity.length.min <- # minimum length # fecundity.length.max <- # maximum length @@ -242,16 +261,16 @@ write_captions <- function(dat, # converted model output object # fecundity.min <- # minimum fecundity # fecundity.max <- # maximum fecundity - ## CAA (catch at age) + ## CAA (catch at age)- don't code quantities yet # fleet.or.survey.name <- # fleet or survey name (SHARED with CAL, below) # caa.age.min <- # minimum age group # caa.age.max <- # maximum age group - ## CAL (catch at length) + ## CAL (catch at length)- don't code quantities yet # cal.length.min <- # minimum length group # cal.length.max <- # maximum length group - ## CPUE indices plot + ## 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 <- # CPUE units (plural) (SHARED with mod.fit.abun, below) @@ -260,12 +279,15 @@ write_captions <- function(dat, # converted model output object ## NAA (numbers at age) # bubble.start.year.min <- # start year of NAA plot + # -label = abundance ; year != na; min(year) # bubble.end.year.max <- # end year of NAA plot - # bubble.age.units <- # age units (plural) + # -label = abundance ; year != na; max(year) # bubble.age.min <- # minimum age + # year != na; min(age) # bubble.age.max <- # maximum age + # year != na; max(age) - ## mod_fit_catch (model fit to catch ts) + ## 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 # mod.fit.catch.units <- # catch units (plural) @@ -276,14 +298,19 @@ write_captions <- function(dat, # converted model output object # mod.fit.abun.start.year <- # start year of model fit to abundance indices plot # mod.fit.abun.end.year <- # end year of model fit to abundance indices plot - ## mod_fit_discards + ## 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 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 + ## 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) @@ -291,9 +318,9 @@ write_captions <- function(dat, # converted model output object # selectivity.length.max <- # maximum length - ## estimated stock recruitment (aka spawning stock biomass) + ## estimated stock recruitment # youngest-age recruited fish (instead of age-0) - # sr.age.min <- # unsure how to extract this + # sr.age.min <- min(age) (usually 1) # ssb units (plural) # sr.ssb.units <- # this will take some thought, since @@ -476,6 +503,7 @@ write_captions <- function(dat, # converted model output object round(digits = 2) ## tot_b (total biomass) + ## -same as B plot above # biomass.start.year <- # start year of biomass plot # biomass.end.year <- # end year of biomass plot # biomass.units <- # biomass units (plural) @@ -560,12 +588,17 @@ write_captions <- function(dat, # converted model output object ## spr (spawning potential ratio) # spr.start.year <- # start year of spr plot + # -same as ssb plot # spr.end.year <- # end year of spr plot + # -same as ssb plot # spr.min <- # minimum spr + # -label = spr (min) & end.year != na ; OR grepl("spr")- could be iffy # spr.max <- # maximum spr + # - " # spr.ref.pt <- # spr reference point - # spr.ref.pt.units <- # spr reference point units + # -spr_msy + #TODO: LEFT OFF HERE (identifying how to extract quantities with Sam, Steve) ## pop_naa_baa (population numbers at age and population biomass at age) # pop.naa.baa.start.year <- # start year of spr plot # pop.naa.baa.end.year <- # end year of spr plot @@ -622,16 +655,11 @@ write_captions <- function(dat, # converted model output object # FIGURES----- ## kobe plot - # 'B.BMSY.min' = as.character(B.BMSY.min), - # 'B.BMSY.max' = as.character(B.BMSY.max), - # 'F.FMSY.min' = as.character(F.FMSY.min), - # 'F.FMSY.max' = as.character(F.FMSY.max), + # '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), - # 'kobe.start.year' = as.character(kobe.start.year), - # 'kobe.end.year' = as.character(kobe.end.year), ## Relative biomass plot # NOTE: moving this above biomass so rel.B.min isn't changed to "rel." + B.min (etc.) @@ -640,7 +668,6 @@ write_captions <- function(dat, # converted model output object ## Biomass plot # 'B.ref.pt' = as.character(B.ref.pt), - # 'B.ref.pt.units' = as.character(B.ref.pt.units), 'B.start.year' = as.character(B.start.year), # 'B.end.year' = as.character(B.end.year), # 'B.units' = as.character(B.units), @@ -653,10 +680,8 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot # 'F.ref.pt' = as.character(F.ref.pt), - # 'F.ref.pt.units' = as.character(F.ref.pt.units), # 'F.start.year' = as.character(F.start.year), # 'F.end.year' = as.character(F.end.year), - # 'F.units' = as.character(F.units), # 'F.min' = as.character(F.min), # 'F.max' = as.character(F.max), # 'Ftarg' = as.character(Ftarg), @@ -672,12 +697,10 @@ write_captions <- function(dat, # converted model output object ## natural mortality (M) # 'M.age.min' = as.character(M.age.min), # 'M.age.max' = as.character(M.age.max), - # 'M.units' = as.character(M.units), # '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.units' = as.character(vonb.age.units), # 'vonb.age.min' = as.character(vonb.age.min), # 'vonb.age.max' = as.character(vonb.age.max), # 'vonb.length.units' = as.character(vonb.length.units), @@ -732,7 +755,6 @@ write_captions <- function(dat, # converted model output object ## NAA (numbers at age) # 'bubble.start.year.min' = as.character(bubble.start.year.min), # 'bubble.end.year.max' = as.character(bubble.end.year.max), - # 'bubble.age.units' = as.character(bubble.age.units), # 'bubble.age.min' = as.character(bubble.age.min), # 'bubble.age.max' = as.character(bubble.age.max), @@ -821,7 +843,6 @@ write_captions <- function(dat, # converted model output object # 'spr.min' = as.character(spr.min), # 'spr.max' = as.character(spr.max), # 'spr.ref.pt' = as.character(spr.ref.pt), - # 'spr.ref.pt.units' = as.character(spr.ref.pt.units), # # ## pop_naa_baa (population numbers at age and population biomass at age) # 'pop.naa.baa.start.year' = as.character(pop.naa.baa.start.year), From 007e0b75a99c3794ef94b1711dd1968eabf1bdba Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 14:17:19 -0500 Subject: [PATCH 32/54] Small updates to broaden new fxn that inserts more key quantities- including reference points --- R/utils.R | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/R/utils.R b/R/utils.R index be9dfaa..446c217 100644 --- a/R/utils.R +++ b/R/utils.R @@ -57,8 +57,8 @@ export_rda <- function(final = final, paste0(topic_label, "_", fig_or_table, ".rda"))) } -# substitute in more key quantities (units and end_years) to captions/alt text -add_yr_units <- function( +# substitute in more key quantities (units, end_years, reference points) to captions/alt text +add_more_key_quants <- function( topic = topic_label, fig_or_table = fig_or_table, end_year = NULL, @@ -78,8 +78,8 @@ add_yr_units <- function( type == fig_or_table) # select caption - topic_cap <- topic_cap_alt |> - dplyr::select(caption) + # topic_cap <- topic_cap_alt |> + # dplyr::select(caption) # select alt text topic_alt <- topic_cap_alt |> @@ -97,7 +97,13 @@ add_yr_units <- function( as.character(units), topic_alt) + ## reference points + ## TODO: ADD HERE + # replace old alt text with new alt text + test <- caps_alt_df[caps_alt_df$type == fig_or_table, + # caps_alt_df$label == topic + ] } From 5ea8a4dbd732cb2196be51d1fc9ed0cdbf691bca Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 14:17:39 -0500 Subject: [PATCH 33/54] Continue updating key quantities in write_captions and alt text/caps csv --- R/write_captions.R | 112 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 85 insertions(+), 27 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 309ed5d..cd5ce14 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -133,8 +133,19 @@ write_captions <- function(dat, # converted model output object # --(use to_lower): F_targ if it's present. if not, use F_proxy; if not, Fmsy (these are labels in dat) # F.start.year. <- # start year of F plot # --like Fend, but minimum year - # F.end.year <- # end year of F plot - # --Fend + + # terminal fishing mortality + # TODO: Put this in utils function, since F.end.year needs end_year + # 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) + # F.min <- # minimum F # --if age = na, then take min(time series) # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) |> @@ -143,10 +154,19 @@ write_captions <- function(dat, # converted model output object # --if age = na, then take max(time series) # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) # pull(max(val)) - # Ftarg <- - # --Ftarg - # F.Ftarg <- - # --F_Ftarg + + # 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 + # TODO: Put this in utils function, since F.end.year needs end_year + # F.Ftarg <- F.end.year / Ftarg ## landings plot @@ -216,11 +236,27 @@ write_captions <- function(dat, # converted model output object round(digits = 2) ## natural mortality (M) - # M.age.min <- # minimum age of M - # --if age != na, min(age) - # M.age.max <- # maximum age of M - # --if age != na, max(age) - # M.rate.min <- # minimum M rate + ## Should this be filtered by label = natural_mortality too? + # minimum age of M + # M.age.min <- dat |> + # dplyr::select(age) |> + # dplyr::filter(!is.na(age)) |> + # dplyr::slice(which.min(age)) |> + # as.numeric() + # + # # maximum age of M + # M.age.max <- dat |> + # dplyr::select(age) |> + # dplyr::filter(!is.na(age)) |> + # dplyr::slice(which.max(age)) |> + # as.numeric() + + # minimum M rate + # The "petrale_sole_std_output.csv" doesn't have a natural_mortality label... + # doesn't work + # M.rate.min <- dat |> + # dplyr::filter( + # grepl("natural_mortality", label)) |> # -label = natural_mortality (min); est in est col # M.rate.max <- # maximum M rate # -label = natural_mortality (min); est in est col @@ -278,14 +314,33 @@ write_captions <- function(dat, # converted model output object # cpue.max <- # maximum CPUE (SHARED with mod_fit_abun, below) ## NAA (numbers at age) - # bubble.start.year.min <- # start year of NAA plot - # -label = abundance ; year != na; min(year) - # bubble.end.year.max <- # end year of NAA plot - # -label = abundance ; year != na; max(year) - # bubble.age.min <- # minimum age - # year != na; min(age) - # bubble.age.max <- # maximum age - # year != na; max(age) + # start year of NAA plot + bubble.start.year.min <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.min(year)) |> + dplyr::select(year) |> + as.numeric() + + # end year of NAA plot + bubble.end.year.max <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.max(year)) |> + dplyr::select(year) |> + as.numeric() + + # minimum age + bubble.age.min <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.min(age)) |> + dplyr::select(age) |> + as.numeric() + + # maximum age + bubble.age.max <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.max(age)) |> + dplyr::select(age) |> + as.numeric() ## 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 @@ -294,9 +349,12 @@ write_captions <- function(dat, # converted model output object # mod.fit.catch.min <- # minimum catch # mod.fit.catch.max <- # maximum catch - ## mod_fit_abun (model fit to abundance indices plot) - # mod.fit.abun.start.year <- # start year of model fit to abundance indices plot - # mod.fit.abun.end.year <- # end year of model fit to abundance indices plot + ## 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 @@ -320,7 +378,11 @@ write_captions <- function(dat, # converted model output object ## estimated stock recruitment # youngest-age recruited fish (instead of age-0) - # sr.age.min <- min(age) (usually 1) + # 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 <- # this will take some thought, since @@ -509,8 +571,6 @@ write_captions <- function(dat, # converted model output object # biomass.units <- # biomass units (plural) # biomass.min <- # minimum biomass # biomass.max <- # maximum biomass - # biomass.ref.pt <- # biomass reference point - # biomass.ref.pt.units <- # biomass reference point units ## spawning_biomass (ssb) # start year of ssb plot @@ -817,8 +877,6 @@ write_captions <- function(dat, # converted model output object # 'biomass.units' = as.character(biomass.units), # 'biomass.min' = as.character(biomass.min), # 'biomass.max' = as.character(biomass.max), - # 'biomass.ref.pt' = as.character(biomass.ref.pt), - # 'biomass.ref.pt.units' = as.character(biomass.ref.pt.units), # relative ssb # NOTE: moving this above recruitment so rel.ssb.min isn't changed to From 5c67b309adfc7f19063ed6541e5901228b4b7a2a Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 13 Jan 2025 16:12:55 -0500 Subject: [PATCH 34/54] Continue updating key quantities in write_captions and alt text/caps csv --- R/write_captions.R | 128 ++++++++++-------- inst/resources/captions_alt_text_template.csv | 10 +- 2 files changed, 74 insertions(+), 64 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index cd5ce14..37f2902 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -129,10 +129,23 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot - # F.ref.pt <- # F reference point - # --(use to_lower): F_targ if it's present. if not, use F_proxy; if not, Fmsy (these are labels in dat) - # F.start.year. <- # start year of F plot - # --like Fend, but minimum year + # 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") + ) |> + 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 # TODO: Put this in utils function, since F.end.year needs end_year @@ -146,11 +159,21 @@ write_captions <- function(dat, # converted model output object # as.numeric() |> # round(digits = 2) - # F.min <- # minimum F + # minimum F + # F.min <- dat |> + # dplyr::filter(label == 'fishing_mortality') + # + # if (F.min$age) + # + # dplyr::slice(which.min(year)) |> + # as.numeric() |> + # round(digits = 2) # --if age = na, then take min(time series) # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) |> # pull(min(val)) - # F.max <- # maximum F + + # maximum F + # F.max <- # --if age = na, then take max(time series) # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) # pull(max(val)) @@ -491,20 +514,14 @@ write_captions <- function(dat, # converted model output object # recruitment.end.year <- # this will take some thought, since end_year is # either entered by the user or calculated in plot_recruitment.R - # minimum recruitment - recruitment.min <- sr.min - - # maximum recruitment - recruitment.max <- sr.max - ## relative recruitment # minimum relative recruitment - rel.recruitment.min <- (recruitment.min / R0) |> + rel.recruitment.min <- (sr.min / R0) |> round(digits = 2) # maximum relative recruitment - rel.recruitment.max <- (recruitment.max / R0) |> + rel.recruitment.max <- (sr.max / R0) |> round(digits = 2) @@ -564,13 +581,7 @@ write_captions <- function(dat, # converted model output object as.numeric() |> round(digits = 2) - ## tot_b (total biomass) - ## -same as B plot above - # biomass.start.year <- # start year of biomass plot - # biomass.end.year <- # end year of biomass plot - # biomass.units <- # biomass units (plural) - # biomass.min <- # minimum biomass - # biomass.max <- # maximum biomass + ## tot_b (total biomass): same as B plot above ## spawning_biomass (ssb) # start year of ssb plot @@ -627,9 +638,6 @@ write_captions <- function(dat, # converted model output object # ssb.ref.pt <- # this will take some thought, since ref_line_val is # calculated in plot_spawning_biomass.R - # ssb reference point units - # ssb.ref.pt.units <- ssb_units - ssbtarg <- dat |> dplyr::filter(c(grepl('spawning_biomass', label) & grepl('msy$', label) & estimate >1) | label == 'spawning_biomass_msy$') |> dplyr::pull(estimate) |> @@ -647,16 +655,30 @@ write_captions <- function(dat, # converted model output object ## spr (spawning potential ratio) - # spr.start.year <- # start year of spr plot - # -same as ssb plot - # spr.end.year <- # end year of spr plot - # -same as ssb plot - # spr.min <- # minimum spr - # -label = spr (min) & end.year != na ; OR grepl("spr")- could be iffy - # spr.max <- # maximum spr - # - " - # spr.ref.pt <- # spr reference point - # -spr_msy + # 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 reference point + spr.ref.pt <- dat |> + dplyr::filter(label == "spr_msy") |> + dplyr::select(estimate) |> + as.numeric() |> + round(digits = 2) #TODO: LEFT OFF HERE (identifying how to extract quantities with Sam, Steve) ## pop_naa_baa (population numbers at age and population biomass at age) @@ -739,12 +761,12 @@ write_captions <- function(dat, # converted model output object # 'Bmsy' = as.character(Bmsy), ## mortality (F) plot - # 'F.ref.pt' = as.character(F.ref.pt), - # 'F.start.year' = as.character(F.start.year), + 'F.ref.pt' = as.character(F.ref.pt), + 'F.start.year' = as.character(F.start.year), # 'F.end.year' = as.character(F.end.year), # 'F.min' = as.character(F.min), # 'F.max' = as.character(F.max), - # 'Ftarg' = as.character(Ftarg), + 'Ftarg' = as.character(Ftarg), # 'F.Ftarg' = as.character(F.Ftarg), ## landings plot @@ -813,10 +835,10 @@ write_captions <- function(dat, # converted model output object # 'cpue.max' = as.character(cpue.max), ## NAA (numbers at age) - # 'bubble.start.year.min' = as.character(bubble.start.year.min), - # 'bubble.end.year.max' = as.character(bubble.end.year.max), - # 'bubble.age.min' = as.character(bubble.age.min), - # 'bubble.age.max' = as.character(bubble.age.max), + 'bubble.start.year.min' = as.character(bubble.start.year.min), + 'bubble.end.year.max' = as.character(bubble.end.year.max), + 'bubble.age.min' = as.character(bubble.age.min), + 'bubble.age.max' = as.character(bubble.age.max), ## mod_fit_catch (model fit to catch ts) # 'mod.fit.catch.start.year' = as.character(mod.fit.catch.start.year), @@ -862,8 +884,6 @@ write_captions <- function(dat, # converted model output object # 'recruitment.units' = as.character(recruitment.units), 'recruitment.start.year' = as.character(recruitment.start.year), # 'recruitment.end.year' = as.character(recruitment.end.year), - 'recruitment.min' = as.character(recruitment.min), - 'recruitment.max' = as.character(recruitment.max), ## recruitment deviations 'recruit.dev.start.year' = as.character(recruit.dev.start.year), @@ -871,13 +891,6 @@ write_captions <- function(dat, # converted model output object 'recruit.dev.min' = as.character(recruit.dev.min), 'recruit.dev.max' = as.character(recruit.dev.max), - ## tot_b (total biomass) - # 'biomass.start.year' = as.character(biomass.start.year), - # 'biomass.end.year' = as.character(biomass.end.year), - # 'biomass.units' = as.character(biomass.units), - # 'biomass.min' = as.character(biomass.min), - # 'biomass.max' = as.character(biomass.max), - # relative ssb # NOTE: moving this above recruitment so rel.ssb.min isn't changed to # "rel." + ssb.min), etc. @@ -889,19 +902,16 @@ write_captions <- function(dat, # converted model output object # 'ssb.end.year' = as.character(ssb.end.year), # 'ssb.units' = as.character(ssb.units), 'ssb.min' = as.character(ssb.min), - 'ssb.max' = as.character(ssb.max)#, + 'ssb.max' = as.character(ssb.max), # 'ssb.ref.pt' = as.character(ssb.ref.pt), - # 'ssb.ref.pt.units' = as.character(ssb.ref.pt.units), # 'ssbtarg' = as.character(ssbtarg), - # ## spr (spawning potential ratio) - # 'spr.start.year' = as.character(spr.start.year), - # 'spr.end.year' = as.character(spr.end.year), - # 'spr.min' = as.character(spr.min), - # 'spr.max' = as.character(spr.max), - # 'spr.ref.pt' = as.character(spr.ref.pt), - # + ## 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_naa_baa (population numbers at age and population biomass at age) # 'pop.naa.baa.start.year' = as.character(pop.naa.baa.start.year), # 'pop.naa.baa.end.year' = as.character(pop.naa.baa.end.year), diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index e61e96b..b63ab50 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -19,13 +19,13 @@ mod.fit.abun,figure,Assessment model fits to input catch per unit of effort inde mod.fit.discards,figure,Observed discards (points) and model-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." sr,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, while 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 sr.units for each year. The x axis shows years, which spans from recruitment.start.year to recruitment.end.year, while 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.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,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from biomass.start.year to biomass.end.year . The y axis shows model-estimated biomass in biomass.units, which spans from biomass.min to biomass.max." -spawning.biomass,figure,Model-estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point at ssb.ref.pt ssb.ref.pt.units.,"Line graph showing model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." -relative.spawning.biomass,figure,"Model-estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point), where SSB(reference point) is ssb.ref.pt ssb.ref.pt.units.","Line graph showing model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." -spr,figure,Model-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 model-estimated spawning potential ratio over time. The x axis shows the year, which spans from spr.start.year to spr.end.year . The y axis shows model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." +tot.b,figure,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows model-estimated biomass in B.units, which spans from B.min to B.max." +spawning.biomass,figure,Model-estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point at ssb.ref.pt ssb.units.,"Line graph showing model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." +relative.spawning.biomass,figure,"Model-estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point), where SSB(reference point) is ssb.ref.pt ssb.units.","Line graph showing model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." +spr,figure,Model-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 model-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 model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." pop.naa.baa,figure,Model-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 model-estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.naa.baa.start.year to pop.naa.baa.end.year . The y axis shows numbers of fish, which spans from pop.naa.baa.fish.min to pop.naa.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,figure,Forecasted biomass in proj.biomass.units over future years for different fishing mortality scenarios as indicated in the legend. The horizontal dashed line represents the biomass limit reference point. ,"Time series line graph showing forecasted biomass over future years for different fishing mortality scenarios. The horizontal dashed line represents the biomass limit reference point. The x axis shows the year, which spans from proj.biomass.start.year to proj.biomass.end.year . The y axis shows biomass in proj.biomass.units, which spans from proj.biomass.min to proj.biomass.max." From ceabe800df86b38ecc1a610f148f22c9df7e25e5 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 11:04:04 -0500 Subject: [PATCH 35/54] Finished first working draft of add_more_key_quants fxn; updated alt text/caps template to facilitate regex pattern recognition --- R/utils.R | 85 +++++++++++++------ inst/resources/captions_alt_text_template.csv | 10 +-- 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/R/utils.R b/R/utils.R index 446c217..be04d4a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -61,8 +61,10 @@ export_rda <- function(final = final, add_more_key_quants <- function( topic = topic_label, fig_or_table = fig_or_table, + dir = NULL, end_year = NULL, - units = NULL){ + units = NULL, + ref_pt = NULL){ # import csv caps_alt_df <- read.csv(fs::path(getwd(), "captions_alt_text.csv")) @@ -77,34 +79,69 @@ add_more_key_quants <- function( dplyr::filter(label == topic, type == fig_or_table) - # select caption - # topic_cap <- topic_cap_alt |> - # dplyr::select(caption) - - # select alt text - topic_alt <- topic_cap_alt |> - dplyr::select(alt_text) - - # replace placeholders in alt text + # replace placeholders ## end_year - # if "end.year" is found in topic_alt, replace it with end_year - topic_alt <- sub("\\S*end\\.year\\S*", - as.character(end_year), - topic_alt) + ### alt text + # if "end.year " is found in topic_alt, replace it with 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)) ## units - topic_alt <- sub("\\S*units\\S*", - as.character(units), - topic_alt) + ### 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))) + + ### 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?,)"), + 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*"), + as.character(units))) ## reference points - ## TODO: ADD HERE - - # replace old alt text with new alt text - test <- caps_alt_df[caps_alt_df$type == fig_or_table, - # caps_alt_df$label == topic - ] - + ### 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)))) + + # 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) } diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index b63ab50..eaf9b4b 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -1,7 +1,7 @@ label,type,caption,alt_text kobe,figure,"Kobe plot showing stock status. Triangles delineate start and end years. Horizontal and vertical dashed lines delineate the proportion of F/F~MSY~ where F~MSY~ = B.ref.pt, and B/B~MSY~ where B~MSY~ = B.ref.pt. Overfishing is occurring when F/F~MSY~ > 1 and an overfished status is indicated where B/B~MSY~ > 1.","A Kobe plot showing stock status. The x axis shows overfished status (i.e., B/B~MSY~) and the y axis shows overfishing (i.e., F/F~MSY~). The B/B~MSY~ and F/F~MSY~ for kobe.end.year were B.BMSY.end.yr and F.FMSY.end.yr, respectively, indicating that the stock overfished.status.is.isnot overfished and overfishing.status.is.is.not experiencing overfishing." biomass,figure,Biomass (B) time series. The horizontal dashed line represents the limit reference point (B.ref.pt B.units).,"Line graph showing biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows biomass in B.units, which spans from B.min to B.max." -relative.biomass,figure,"Relative biomass (B) time series. The horizontal dashed line represents the limit reference point calculated as B/B(reference point), where B(reference point) is 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." +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." 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." 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." @@ -9,7 +9,7 @@ vonb.laa,figure,Aged and measured fish (points) and von Bertalanffy growth funct 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." weight.length.converstion,figure,Weight-length conversion relationship where the points represent individual fish observations and the line is the fitted relationship. ,"Point and line graph showing observations of measured length and measured weight from the same fish for conversion purposes as points, and an exponential fit through those points as a line. The x axis shows length in wl.length.units, which spans from wl.length.min to wl.length.max. The y axis shows weight in wl.weight.units, which spans from wl.weight.min to wl.weight.max." maturity.schedule,figure,The relationship between the proportion mature and fish length. ,"Graph showing the relationship between fish maturity and length. Points, if present, show observations of maturity at different fish lengths and the line represents either the fit to the points, or the assumed or known maturity schedule. The x axis shows length in prop.mat.length.units, which spans from prop.mat.length.min to prop.mat.length.max. The y axis shows the proportion mature, which spans from 0--1." -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." +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." CAA,figure,"Histograms showing the proportion of the catch in each age group for each year in 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 age group. The x axis of each histogram shows age groups that span from caa.age.min to caa.age.max. The y axis of each histogram shows the proportion of the catch and spans from 0--1." 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." CPUE.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 for fleet or survey from cpue.start.year to cpue.end.year, 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." @@ -19,12 +19,12 @@ mod.fit.abun,figure,Assessment model fits to input catch per unit of effort inde mod.fit.discards,figure,Observed discards (points) and model-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." sr,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, while the y axis shows recruitment in recruitment.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." 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.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,Model-estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing model-estimated biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year . The y axis shows model-estimated biomass in B.units, which spans from B.min to B.max." -spawning.biomass,figure,Model-estimated spawning stock biomass (SSB) time series. The horizontal dashed line represents the spawning stock biomass associated with the biomass limit reference point at ssb.ref.pt ssb.units.,"Line graph showing model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." -relative.spawning.biomass,figure,"Model-estimated relative spawning stock biomass time series. The horizontal dashed line represents the limit reference point calculated as SSB/SSB(reference point), where SSB(reference point) is ssb.ref.pt ssb.units.","Line graph showing model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." +spawning.biomass,figure,Model-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 model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." +relative.spawning.biomass,figure,Model-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 model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." spr,figure,Model-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 model-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 model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." pop.naa.baa,figure,Model-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 model-estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.naa.baa.start.year to pop.naa.baa.end.year . The y axis shows numbers of fish, which spans from pop.naa.baa.fish.min to pop.naa.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." From fb42b6d4752fc0f23f37d87c639c3007a38ad073 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 11:22:17 -0500 Subject: [PATCH 36/54] Small updates to fix regex matching --- R/utils.R | 12 ++++++++++-- inst/resources/captions_alt_text_template.csv | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/R/utils.R b/R/utils.R index be04d4a..8c754d8 100644 --- a/R/utils.R +++ b/R/utils.R @@ -79,15 +79,23 @@ add_more_key_quants <- function( dplyr::filter(label == topic, type == fig_or_table) - # replace placeholders + # replace placeholders (e.g., if "end.year" is found in topic_alt, replace it with end_year) ## end_year ### alt text - # if "end.year " is found in topic_alt, replace it with end_year + ### 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)) + + ## units ### caption ### this regex preserves the closing ) after the units diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index eaf9b4b..6394a7a 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -12,7 +12,7 @@ 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." CAA,figure,"Histograms showing the proportion of the catch in each age group for each year in 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 age group. The x axis of each histogram shows age groups that span from caa.age.min to caa.age.max. The y axis of each histogram shows the proportion of the catch and spans from 0--1." 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." -CPUE.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 for fleet or survey from cpue.start.year to cpue.end.year, 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." +CPUE.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." NAA,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 bubble.start.year to bubble.end.year . The y axis shows age in years, which spans from bubble.age.min to bubble.age.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." From caf1b9f227204d52f738a43763174b9efb330513 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 15:14:43 -0500 Subject: [PATCH 37/54] added new fxn (add_more_key_quants) to plotting fxns --- R/plot_biomass.R | 13 ++++++++++++- R/plot_indices.R | 8 ++++++++ R/plot_landings.R | 8 ++++++++ R/plot_recruitment.R | 9 +++++++++ R/plot_recruitment_deviations.R | 8 ++++++++ R/plot_spawning_biomass.R | 10 ++++++++++ 6 files changed, 55 insertions(+), 1 deletion(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 06057c9..3ebcd11 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -179,6 +179,16 @@ plot_biomass <- function( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + end_year = end_year, + units = unit_label, + ref_pt = ref_point + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, @@ -189,6 +199,7 @@ plot_biomass <- function( rda_dir = rda_dir, topic_label = topic_label, fig_or_table = fig_or_table) - } + + } return(final) } diff --git a/R/plot_indices.R b/R/plot_indices.R index bb06476..8d60aed 100644 --- a/R/plot_indices.R +++ b/R/plot_indices.R @@ -143,6 +143,14 @@ plot_indices <- function( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + units = unit_label + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, diff --git a/R/plot_landings.R b/R/plot_landings.R index 46cc6e6..2b8bf19 100644 --- a/R/plot_landings.R +++ b/R/plot_landings.R @@ -84,6 +84,14 @@ plot_landings <- function(dat, year = NULL) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + units = unit_label + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, diff --git a/R/plot_recruitment.R b/R/plot_recruitment.R index 83a9453..49da1f9 100644 --- a/R/plot_recruitment.R +++ b/R/plot_recruitment.R @@ -125,6 +125,15 @@ plot_recruitment <- function( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + end_year = end_year, + units = unit_label + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, diff --git a/R/plot_recruitment_deviations.R b/R/plot_recruitment_deviations.R index 0aaec31..815412c 100644 --- a/R/plot_recruitment_deviations.R +++ b/R/plot_recruitment_deviations.R @@ -102,6 +102,14 @@ plot_recruitment_deviations <- function ( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + end_year = end_year + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 88fca14..fd117ae 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -186,6 +186,16 @@ plot_spawning_biomass <- function( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + end_year = end_year, + units = unit_label, + ref_pt = ref_point + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, From 10a3a365dfb6b9d9f1f8038860c8f84e6a73062a Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 16:07:10 -0500 Subject: [PATCH 38/54] updated add_more_key_quants fxn to handle two units (arguments) specified in plot_spawn_recruitment, and added add_more_key_quants to plot_spawn_recruitment --- R/plot_spawn_recruitment.R | 11 +++++++++++ R/utils.R | 34 +++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/R/plot_spawn_recruitment.R b/R/plot_spawn_recruitment.R index 25593ff..1fc3296 100644 --- a/R/plot_spawn_recruitment.R +++ b/R/plot_spawn_recruitment.R @@ -83,6 +83,17 @@ plot_spawn_recruitment <- function( year = end_year) } + # add more key quantities included as arguments in this fxn + add_more_key_quants( + topic = topic_label, + fig_or_table = fig_or_table, + dir = rda_dir, + end_year = end_year, + sr_ssb_units = spawning_biomass_label, + sr_recruitment_units = recruitment_label, + units = NULL + ) + # extract this plot's caption and alt text caps_alttext <- extract_caps_alttext(topic_label = topic_label, fig_or_table = fig_or_table, diff --git a/R/utils.R b/R/utils.R index 8c754d8..21befc6 100644 --- a/R/utils.R +++ b/R/utils.R @@ -64,6 +64,8 @@ add_more_key_quants <- function( dir = NULL, end_year = NULL, units = NULL, + sr_ssb_units = NULL, + sr_recruitment_units = NULL, ref_pt = NULL){ # import csv @@ -80,7 +82,7 @@ add_more_key_quants <- function( type == fig_or_table) # replace placeholders (e.g., if "end.year" is found in topic_alt, replace it with end_year) - ## end_year + ## end_year----- ### alt text ### this regex preserves the comma after the end year topic_cap_alt <- topic_cap_alt |> @@ -93,10 +95,8 @@ add_more_key_quants <- function( dplyr::mutate(alt_text = stringr::str_replace_all(alt_text, stringr::regex("\\S*end\\.year\\S*\\s*"), end_year)) - - - - ## units + ## units----- + if(!is.null(units)){ ### caption ### this regex preserves the closing ) after the units topic_cap_alt <- topic_cap_alt |> @@ -116,7 +116,27 @@ add_more_key_quants <- function( dplyr::mutate(caption = stringr::str_replace_all(caption, stringr::regex("\\S*units\\S*"), as.character(units))) + } + + if(!is.null(sr_ssb_units)){ + ### this is for plot_spawn_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))) + } + if(!is.null(sr_recruitment_units)){ + ### this is for plot_spawn_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))) + } + + if(!is.null(units)){ ### alt text ### this regex preserves the comma after the units topic_cap_alt <- topic_cap_alt |> @@ -131,7 +151,8 @@ add_more_key_quants <- function( stringr::regex("\\S*units\\S*"), as.character(units))) - ## reference points + } + ## reference points----- ### caption ### this regex preserves the opening ( before the ref pt topic_cap_alt <- topic_cap_alt |> @@ -152,4 +173,3 @@ add_more_key_quants <- function( row.names=FALSE) } - From e3e61f6f1c4b887e04982dea1d24503dc5b50d72 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 16:42:08 -0500 Subject: [PATCH 39/54] minor update to test-plot_landings --- tests/testthat/test-plot_landings.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-plot_landings.R b/tests/testthat/test-plot_landings.R index 29f200f..4695131 100644 --- a/tests/testthat/test-plot_landings.R +++ b/tests/testthat/test-plot_landings.R @@ -30,6 +30,7 @@ test_that("rda file made when indicated",{ # export rda plot_landings(dat, make_rda = TRUE, + unit_label = "metric tons", rda_dir = getwd()) # expect that both rda_files dir and the landings_figure.rda file exist From 2c8de3418dea25c7f5561b7fc983d52f22a0bcde Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 15 Jan 2025 16:43:03 -0500 Subject: [PATCH 40/54] -fixed bug in add_more_key_quants to allow str_replace_all to work even if some args are NULL -updated write_captions to explain which key quantities will be substituted with add_more_key_quants --- R/utils.R | 4 ++ R/write_captions.R | 104 +++++++++++++++++++++++++-------------------- 2 files changed, 62 insertions(+), 46 deletions(-) diff --git a/R/utils.R b/R/utils.R index 21befc6..bcccc4e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -83,6 +83,7 @@ add_more_key_quants <- function( # 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 |> @@ -95,6 +96,7 @@ add_more_key_quants <- function( dplyr::mutate(alt_text = stringr::str_replace_all(alt_text, stringr::regex("\\S*end\\.year\\S*\\s*"), end_year)) + } ## units----- if(!is.null(units)){ ### caption @@ -153,12 +155,14 @@ add_more_key_quants <- function( } ## 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)))) + } # remove row with old caption/alt text, then add new row replaced_df <- dplyr::anti_join(caps_alt_df, diff --git a/R/write_captions.R b/R/write_captions.R index 37f2902..0216dc0 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -54,7 +54,8 @@ write_captions <- function(dat, # converted model output object ## Biomass plot - # B.ref.pt <- # biomass reference point + # biomass reference point + # B.ref.pt : added with add_more_key_quants # start year of biomass plot B.start.year <- dat |> @@ -68,12 +69,10 @@ write_captions <- function(dat, # converted model output object as.numeric() # end year of biomass plot - # B.end.year <- # this will take some thought, since end_year is - # either entered by the user or calculated in plot_biomass.R + # B.end.year : added with add_more_key_quants # units of B (plural) - # B.units <- # this will take some thought, since units is - # entered by the user in plot_biomass.R + # B.units : added with add_more_key_quants # minimum B B.min <- dat |> @@ -223,8 +222,7 @@ write_captions <- function(dat, # converted model output object as.numeric() # units of landings (plural) - # landings.units <- # this will take some thought, since units is - # entered by the user in plot_landings.R + # landings.units : added with add_more_key_quants # minimum landings landings.min <- dat |> @@ -284,42 +282,67 @@ write_captions <- function(dat, # converted model output object # M.rate.max <- # maximum M rate # -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 <- # vonB length units (plural) + + # vonB length units (plural) + # vonb.length.units : added with add_more_key_quants + # vonb.length.min <- # minimum vonB length # vonb.length.max <- # minimum vonB length + ## length-type conversion plot- don't code quantities yet - # total.length.units <- # total length units (plural) + # total length units (plural) + # total.length.units : added with add_more_key_quants + # total.length.min <- # minimum total length # total.length.max <- # maximum total length - # fork.length.units <- # fork length units (plural) + # fork length units (plural) + # fork.length.units : added with add_more_key_quants + # fork.length.min <- # minimum fork length # fork.length.max <- # maximum fork length + ## weight-length conversion plot- don't code quantities yet - # wl.length.units <- # length units (plural) + # length units (plural) + # wl.length.units : added with add_more_key_quants + # wl.length.min <- # minimum length # wl.length.max <- # maximum length - # wl.weight.units <- # weight units (plural) + + # weight units (plural) + # wl.weight.units : added with add_more_key_quants + # wl.weight.min <- # minimum weight # wl.weight.max <- # maximum weight + ## maturity schedule (proportion mature)- don't code quantities yet - # prop.mat.length.units <- # length units (plural) + # length units (plural) + # prop.mat.length.units : added with add_more_key_quants + # prop.mat.length.min <- # minimum length # prop.mat.length.max <- # maximum length + ## fecundity at length- don't code quantities yet - # fecundity.length.units <- # length units (plural) + # length units (plural) + # fecundity.length.units : added with add_more_key_quants + # fecundity.length.min <- # minimum length # fecundity.length.max <- # maximum length - # fecundity.units <- # fecundity units (plural) + + # fecundity units (plural) + # fecundity.units : added with add_more_key_quants + # fecundity.min <- # minimum fecundity # fecundity.max <- # maximum fecundity + ## CAA (catch at age)- don't code quantities yet # fleet.or.survey.name <- # fleet or survey name (SHARED with CAL, below) # caa.age.min <- # minimum age group @@ -329,13 +352,18 @@ write_captions <- function(dat, # converted model output object # cal.length.min <- # minimum length group # cal.length.max <- # maximum length group + ## 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 <- # CPUE units (plural) (SHARED with mod.fit.abun, below) + + # 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 bubble.start.year.min <- dat |> @@ -365,13 +393,18 @@ write_captions <- function(dat, # converted model output object dplyr::select(age) |> as.numeric() + ## 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 - # mod.fit.catch.units <- # catch units (plural) + + # catch units (plural) + # mod.fit.catch.units : added with add_more_key_quants + # 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 <- @@ -408,8 +441,7 @@ write_captions <- function(dat, # converted model output object # as.numeric() # ssb units (plural) - # sr.ssb.units <- # this will take some thought, since - # spawning_biomass_label is entered by the user in plot_spawn_recruitment.R + # sr.ssb.units : added with add_more_key_quants # minimum ssb sr.ssb.min <- dat |> @@ -448,8 +480,7 @@ write_captions <- function(dat, # converted model output object round(digits = 2) # recruitment units (plural) - # sr.units <- # this will take some thought, since recruitment_label - # is entered by the user in plot_spawn_recruitment.R + # sr.units : added with add_more_key_quants # minimum recruitment sr.min <- dat |> @@ -490,8 +521,7 @@ write_captions <- function(dat, # converted model output object ## recruitment ts # recruitment units (plural) - numbers of fish, in thousands - # recruitment.units <- # this will take some thought, since unit_label is - # entered by the user in plot_recruitment.R + # recruitment.units : added with add_more_key_quants # start year of recruitment ts plot recruitment.start.year <- dat |> @@ -511,8 +541,7 @@ write_captions <- function(dat, # converted model output object as.numeric() # end year of recruitment ts plot - # recruitment.end.year <- # this will take some thought, since end_year is - # either entered by the user or calculated in plot_recruitment.R + # recruitment.end.year : added with add_more_key_quants ## relative recruitment @@ -544,8 +573,7 @@ write_captions <- function(dat, # converted model output object as.numeric() # end year of recruitment deviations plot - # recruit.dev.end.year <- # this will take some thought, since end_year is - # either entered by the user or calculated in plot_recruitment_deviations.R + # recruit.dev.end.year : added with add_more_key_quants # minimum recruitment deviation recruit.dev.min <- dat |> @@ -599,12 +627,10 @@ write_captions <- function(dat, # converted model output object round(digits = 2) # end year of ssb plot - # ssb.end.year <- # this will take some thought, since end_year is - # either entered by the user or calculated in plot_spawning_biomass.R + # ssb.end.year : added with add_more_key_quants # ssb units (plural) - # ssb.units <- # this will take some thought, since unit_label is - # entered by the user in plot_spawning_biomass.R + # ssb.units : added with add_more_key_quants # minimum ssb ssb.min <- dat |> @@ -635,8 +661,7 @@ write_captions <- function(dat, # converted model output object round(digits = 2) # ssb reference point - # ssb.ref.pt <- # this will take some thought, since ref_line_val is - # calculated in plot_spawning_biomass.R + # ssb.ref.pt : added with add_more_key_quants ssbtarg <- dat |> dplyr::filter(c(grepl('spawning_biomass', label) & grepl('msy$', label) & estimate >1) | label == 'spawning_biomass_msy$') |> @@ -749,10 +774,7 @@ write_captions <- function(dat, # converted model output object 'rel.B.max' = as.character(rel.B.max), ## Biomass plot - # 'B.ref.pt' = as.character(B.ref.pt), 'B.start.year' = as.character(B.start.year), - # 'B.end.year' = as.character(B.end.year), - # 'B.units' = as.character(B.units), 'B.min' = as.character(B.min), 'B.max' = as.character(B.max), 'R0' = as.character(R0), @@ -772,7 +794,6 @@ write_captions <- function(dat, # converted model output object ## landings plot 'landings.start.year' = as.character(landings.start.year), 'landings.end.year' = as.character(landings.end.year), - # 'landings.units' = as.character(landings.units), 'landings.min' = as.character(landings.min), 'landings.max' = as.character(landings.max), @@ -830,7 +851,6 @@ write_captions <- function(dat, # converted model output object ## CPUE indices plot # 'cpue.start.year' = as.character(cpue.start.year), # 'cpue.end.year' = as.character(cpue.end.year), - # 'cpue.units' = as.character(cpue.units), # 'cpue.min' = as.character(cpue.min), # 'cpue.max' = as.character(cpue.max), @@ -867,10 +887,8 @@ write_captions <- function(dat, # converted model output object ## estimated stock recruitment (aka spawning stock biomass) # 'sr.age.min' = as.character(sr.age.min), - # 'sr.ssb.units' = as.character(sr.ssb.units), 'sr.ssb.min' = as.character(sr.ssb.min), 'sr.ssb.max' = as.character(sr.ssb.max), - # 'sr.units' = as.character(sr.units), 'sr.min' = as.character(sr.min), 'sr.max' = as.character(sr.max), @@ -881,13 +899,10 @@ write_captions <- function(dat, # converted model output object 'rel.recruitment.max' = as.character(rel.recruitment.max), ## recruitment ts - # 'recruitment.units' = as.character(recruitment.units), 'recruitment.start.year' = as.character(recruitment.start.year), - # 'recruitment.end.year' = as.character(recruitment.end.year), ## recruitment deviations 'recruit.dev.start.year' = as.character(recruit.dev.start.year), - # 'recruit.dev.end.year' = as.character(recruit.dev.end.year), 'recruit.dev.min' = as.character(recruit.dev.min), 'recruit.dev.max' = as.character(recruit.dev.max), @@ -899,11 +914,8 @@ write_captions <- function(dat, # converted model output object ## spawning.biomass (ssb) 'ssb.start.year' = as.character(ssb.start.year), - # 'ssb.end.year' = as.character(ssb.end.year), - # 'ssb.units' = as.character(ssb.units), 'ssb.min' = as.character(ssb.min), 'ssb.max' = as.character(ssb.max), - # 'ssb.ref.pt' = as.character(ssb.ref.pt), # 'ssbtarg' = as.character(ssbtarg), From df15d982324852a9aad63efc3c381c01d2967b87 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 16 Jan 2025 11:56:47 -0500 Subject: [PATCH 41/54] coded more key quantities --- R/write_captions.R | 71 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 0216dc0..10b550a 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -159,23 +159,50 @@ write_captions <- function(dat, # converted model output object # round(digits = 2) # minimum F - # F.min <- dat |> - # dplyr::filter(label == 'fishing_mortality') - # - # if (F.min$age) - # - # dplyr::slice(which.min(year)) |> - # as.numeric() |> - # round(digits = 2) - # --if age = na, then take min(time series) - # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) |> - # pull(min(val)) + 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 = min(estimate)) |> + dplyr::slice(which.min(val)) |> + dplyr::select(val) |> + as.numeric() |> + round(digits = 2) + } + # maximum F - # F.max <- - # --if age = na, then take max(time series) - # --if age & year != na, then group_by(age) |> summarize(val = max(estimate)) - # pull(max(val)) + 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() |> + signif(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() |> + signif(2) + } + # fishing mortality at msy Ftarg <- dat |> @@ -434,11 +461,11 @@ write_captions <- function(dat, # converted model output object ## 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() + 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 @@ -786,8 +813,8 @@ write_captions <- function(dat, # converted model output object 'F.ref.pt' = as.character(F.ref.pt), 'F.start.year' = as.character(F.start.year), # 'F.end.year' = as.character(F.end.year), - # 'F.min' = as.character(F.min), - # 'F.max' = as.character(F.max), + 'F.min' = as.character(F.min), + 'F.max' = as.character(F.max), 'Ftarg' = as.character(Ftarg), # 'F.Ftarg' = as.character(F.Ftarg), From 523542d7d89ceef303ad14536226495dc46c0423 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 16 Jan 2025 11:56:58 -0500 Subject: [PATCH 42/54] started "manual" vignette --- .gitignore | 1 + DESCRIPTION | 2 ++ vignettes/.gitignore | 2 ++ vignettes/manual.Rmd | 25 +++++++++++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 vignettes/.gitignore create mode 100644 vignettes/manual.Rmd diff --git a/.gitignore b/.gitignore index 7f7c052..d970e30 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ captions_alt_text.csv converted_file_1126.csv rda_files petrale_sole_std_output.csv +inst/doc diff --git a/DESCRIPTION b/DESCRIPTION index f395671..47904bd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -44,7 +44,9 @@ Remotes: Depends: R (>= 2.10) Suggests: + rmarkdown, testthat (>= 3.0.0) Config/testthat/edition: 3 Config/testthat/parallel: false Language: en-US +VignetteBuilder: knitr diff --git a/vignettes/.gitignore b/vignettes/.gitignore new file mode 100644 index 0000000..097b241 --- /dev/null +++ b/vignettes/.gitignore @@ -0,0 +1,2 @@ +*.html +*.R diff --git a/vignettes/manual.Rmd b/vignettes/manual.Rmd new file mode 100644 index 0000000..12a96da --- /dev/null +++ b/vignettes/manual.Rmd @@ -0,0 +1,25 @@ +--- +title: "manual" +output: + html_document: + toc: true +vignette: > + %\VignetteIndexEntry{manual} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +``` + +# General manual + +This manual is a work in progress. In the meantime, please read the function documentation and refer to the documentation of [the companion package, `asar`](https://github.com/nmfs-ost/asar). + +## Updating alternative text and captions + + From 12327b7a61ddea64c45b22afc90e544cc101e452 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 16 Jan 2025 17:21:44 -0500 Subject: [PATCH 43/54] Changed projected biomass csv entry from figure to table; updated key quantities coding in write_captions --- R/write_captions.R | 187 ++++++++---------- inst/resources/captions_alt_text_template.csv | 2 +- 2 files changed, 85 insertions(+), 104 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 10b550a..6aef8df 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -24,6 +24,11 @@ write_captions <- function(dat, # converted model output object system.file("resources", "captions_alt_text_template.csv", package = "satf") ) + 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. @@ -63,7 +68,6 @@ write_captions <- function(dat, # converted model output object module_name == "TIME_SERIES" | module_name == "t.series", # SS3 and BAM target module names is.na(fleet), is.na(age)) |> - dplyr::mutate(estimate = as.numeric(estimate)) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() @@ -80,7 +84,6 @@ write_captions <- function(dat, # converted model output object module_name == "TIME_SERIES" | module_name == "t.series", # SS3 and BAM target module names is.na(fleet), is.na(age)) |> - dplyr::mutate(estimate = as.numeric(estimate)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -92,7 +95,6 @@ write_captions <- function(dat, # converted model output object module_name == "TIME_SERIES" | module_name == "t.series", # SS3 and BAM target module names is.na(fleet), is.na(age)) |> - dplyr::mutate(estimate = as.numeric(estimate)) |> dplyr::slice(which.max(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -173,7 +175,7 @@ write_captions <- function(dat, # converted model output object } else { F.min <- F.min |> dplyr::group_by(age)|> - dplyr::summarize(val = min(estimate)) |> + dplyr::summarize(val = max(estimate)) |> dplyr::slice(which.min(val)) |> dplyr::select(val) |> as.numeric() |> @@ -191,7 +193,7 @@ write_captions <- function(dat, # converted model output object dplyr::filter(module_name %in% c("TIME_SERIES","t.series")) |> dplyr::slice(which.max(estimate)) |> as.numeric() |> - signif(2) + round(digits = 2) } } else { F.max <- F.max |> @@ -200,7 +202,7 @@ write_captions <- function(dat, # converted model output object dplyr::slice(which.max(val)) |> dplyr::select(val) |> as.numeric() |> - signif(2) + round(digits = 2) } @@ -227,9 +229,6 @@ write_captions <- function(dat, # converted model output object # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output !is.na(fleet) ) |> - dplyr::mutate( - year = as.numeric(year) - ) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() @@ -241,9 +240,6 @@ write_captions <- function(dat, # converted model output object # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output !is.na(fleet) ) |> - dplyr::mutate( - year = as.numeric(year) - ) |> dplyr::slice(which.max(year)) |> dplyr::select(year) |> as.numeric() @@ -258,10 +254,6 @@ write_captions <- function(dat, # converted model output object # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output !is.na(fleet) ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year) - ) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -274,17 +266,13 @@ write_captions <- function(dat, # converted model output object # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output !is.na(fleet) ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year) - ) |> dplyr::slice(which.max(estimate)) |> dplyr::select(estimate) |> as.numeric() |> round(digits = 2) - ## natural mortality (M) - ## Should this be filtered by label = natural_mortality too? + ## natural mortality (M)- bam examples have label as natural_mortality + ## but other formats don't (in input) (use if statement) # minimum age of M # M.age.min <- dat |> # dplyr::select(age) |> @@ -300,13 +288,13 @@ write_captions <- function(dat, # converted model output object # as.numeric() # minimum M rate - # The "petrale_sole_std_output.csv" doesn't have a natural_mortality label... - # doesn't work # M.rate.min <- dat |> # dplyr::filter( # grepl("natural_mortality", label)) |> # -label = natural_mortality (min); est in est col - # M.rate.max <- # maximum M rate + + # maximum M rate + # M.rate.max <- # -label = natural_mortality (min); est in est col @@ -481,8 +469,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -499,8 +485,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.max(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -520,8 +504,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -538,8 +520,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.max(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -561,8 +541,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() @@ -593,8 +571,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() @@ -613,8 +589,6 @@ write_captions <- function(dat, # converted model output object is.na(growth_pattern) | length(unique(growth_pattern)) <= 1, !year %in% year_exclusions ) |> # SS3 and BAM target module names - dplyr::mutate(estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -645,9 +619,6 @@ write_captions <- function(dat, # converted model output object label == "spawning_biomass", module_name %in% c("DERIVED_QUANTITIES", "t.series") ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() |> @@ -665,9 +636,6 @@ write_captions <- function(dat, # converted model output object label == "spawning_biomass", module_name %in% c("DERIVED_QUANTITIES", "t.series") ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -679,9 +647,6 @@ write_captions <- function(dat, # converted model output object label == "spawning_biomass", module_name %in% c("DERIVED_QUANTITIES", "t.series") ) |> - dplyr::mutate( - estimate = as.numeric(estimate), - year = as.numeric(year)) |> dplyr::slice(which.max(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -732,35 +697,67 @@ write_captions <- function(dat, # converted model output object as.numeric() |> round(digits = 2) - #TODO: LEFT OFF HERE (identifying how to extract quantities with Sam, Steve) + ## pop_naa_baa (population numbers at age and population biomass at age) - # pop.naa.baa.start.year <- # start year of spr plot - # pop.naa.baa.end.year <- # end year of spr plot - # pop.naa.baa.fish.min <- # minimum number of fish - # pop.naa.baa.fish.max <- # maximum number of fish + # start year of pop_naa_baa plot + pop.naa.baa.start.year <- dat |> + dplyr::filter(label == "abundance") |> + dplyr::slice(which.min(year)) |> + dplyr::select(year) |> + as.numeric() + + # end year of pop_naa_baa plot + pop.naa.baa.end.year <- landings.end.year + + # minimum number of fish + pop.naa.baa.fish.min <- dat |> + dplyr::filter(label == "abundance") |> + dplyr::group_by(year) |> + dplyr::summarize(val = max(estimate)) |> + dplyr::slice(which.min(val)) |> + dplyr::select(val) |> + as.numeric() |> + round(digits = 2) + + # maximum number of fish + pop.naa.baa.fish.max <- dat |> + dplyr::filter(label == "abundance") |> + dplyr::group_by(year) |> + dplyr::summarize(val = max(estimate)) |> + dplyr::slice(which.max(val)) |> + dplyr::select(val) |> + as.numeric() |> + round(digits = 2) + ## proj_catch (projected catch) - # proj.catch.units <- # projected catch units (plural) - # proj.catch.start.year <- # start year of projected catch plot - # proj.catch.end.year <- # end year of projected catch plot - # proj.catch.min <- # minimum projected catch - # proj.catch.max <- # maximum projected catch - - ## proj_biomass (projected biomass) - # proj.biomass.units <- # projected biomass units (plural) - # proj.biomass.start.year <- # start year of projected biomass plot - # proj.biomass.end.year <- # end year of projected biomass plot - # proj.biomass.min <- # minimum projected biomass - # proj.biomass.max <- # maximum projected biomass - # proj.biomass.ref.pt <- # projected biomass reference point - # proj.biomass.ref.pt.units <- # projected biomass reference point units - - ## Other - # tot.catch <- - # M <- - # steep <- - # SBmsy <- - # fSB <- + # 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 + + # 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 |> + 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----- @@ -949,38 +946,21 @@ write_captions <- function(dat, # converted model output object ## spr (spawning potential ratio) 'spr.min' = as.character(spr.min), 'spr.max' = as.character(spr.max), - 'spr.ref.pt' = as.character(spr.ref.pt)#, + 'spr.ref.pt' = as.character(spr.ref.pt), # ## pop_naa_baa (population numbers at age and population biomass at age) - # 'pop.naa.baa.start.year' = as.character(pop.naa.baa.start.year), - # 'pop.naa.baa.end.year' = as.character(pop.naa.baa.end.year), - # 'pop.naa.baa.fish.min' = as.character(pop.naa.baa.fish.min), - # 'pop.naa.baa.fish.max' = as.character(pop.naa.baa.fish.max), - # - # ## proj_catch (projected catch) + 'pop.naa.baa.start.year' = as.character(pop.naa.baa.start.year), + 'pop.naa.baa.end.year' = as.character(pop.naa.baa.end.year), + 'pop.naa.baa.fish.min' = as.character(pop.naa.baa.fish.min), + 'pop.naa.baa.fish.max' = as.character(pop.naa.baa.fish.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), - # - # ## proj_biomass (projected biomass) - # 'proj.biomass.units' = as.character(proj.biomass.units), - # 'proj.biomass.start.year' = as.character(proj.biomass.start.year), - # 'proj.biomass.end.year' = as.character(proj.biomass.end.year), - # 'proj.biomass.min' = as.character(proj.biomass.min), - # 'proj.biomass.max' = as.character(proj.biomass.max), - # 'proj.biomass.ref.pt' = as.character(proj.biomass.ref.pt), - # 'proj.biomass.ref.pt.units' = as.character(proj.biomass.ref.pt.units), - # - # ## Other - # 'tot.catch' = as.character(tot.catch), - # 'M' = as.character(M), - # 'steep' = as.character(steep), - # 'SBmsy' = as.character(SBmsy), - # 'fSB' = as.character(fSB), - # 'sbtarg' = as.character(sbtarg), - # + '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 @@ -994,6 +974,7 @@ write_captions <- function(dat, # converted model output object # # ## catchability # 'catchability.fleet' = as.character(catchability.fleet) + ) # take the values associated with the quantities and replace the df's diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index 6394a7a..62be9b3 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -28,7 +28,7 @@ relative.spawning.biomass,figure,Model-estimated relative spawning stock biomass spr,figure,Model-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 model-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 model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." pop.naa.baa,figure,Model-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 model-estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.naa.baa.start.year to pop.naa.baa.end.year . The y axis shows numbers of fish, which spans from pop.naa.baa.fish.min to pop.naa.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,figure,Forecasted biomass in proj.biomass.units over future years for different fishing mortality scenarios as indicated in the legend. The horizontal dashed line represents the biomass limit reference point. ,"Time series line graph showing forecasted biomass over future years for different fishing mortality scenarios. The horizontal dashed line represents the biomass limit reference point. The x axis shows the year, which spans from proj.biomass.start.year to proj.biomass.end.year . The y axis shows biomass in proj.biomass.units, which spans from proj.biomass.min to proj.biomass.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. , catch,table,Observed (points) and model estimated (line) catch over time for fleet catch.fleet., From ea6c439a973f053f107803f4b83f87fa3ddcce44 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 17 Jan 2025 11:33:42 -0500 Subject: [PATCH 44/54] Started updating manual with documentation for new fxn --- vignettes/manual.Rmd | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vignettes/manual.Rmd b/vignettes/manual.Rmd index 12a96da..5844e0e 100644 --- a/vignettes/manual.Rmd +++ b/vignettes/manual.Rmd @@ -22,4 +22,16 @@ This manual is a work in progress. In the meantime, please read the function doc ## Updating alternative text and captions +When a plotting function (e.g., `plot_biomass()`, `table_indices()`, `exp_all_figs_tables()`) is run, placeholders within the plot's associated alternative text and captions are replaced with key quantities (see the [`asar` package's Accessibility vignette](https://nmfs-ost.github.io/asar/articles/accessibility_guide.html) and/or the [`satf::write_captions()` function](https://github.com/nmfs-ost/satf/blob/master/R/write_captions.R) for more details). As stated in the Accessibility vignette, *you should always check that these alternative texts and captions have been assembled correctly.* +There are multiple reasons why you may want to edit these. For instance, maybe you entered an incorrect value for the units when running `plot_biomass()`. Maybe you need to add some extra information to the alternative text accurately describe the plot. In any case, **here's what to do if you want to alter the alternative text and/or captions in `satf`**, depending on which plotting function you're using: + +### `exp_all_figs_tables()` + +1. Delete the captions_alt_text.csv file + +2. Rerun `exp_all_figs_tables()` with the correct arguments + +### individual fxns + +-edit the rda directly (easier if editing, for instance, units instead of an extracted key quantity) -replace the fxn's existing row in the caps_alt_text.csv with the analogous row from the alt text/csv template, then rerun the plotting fxn so that the KQs can be replaced correctly From 7f6de5d3429f44ccdc63a8138dfcce4e30e9828f Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 17 Jan 2025 15:18:26 -0500 Subject: [PATCH 45/54] Finished updating new section in manual --- vignettes/manual.Rmd | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/vignettes/manual.Rmd b/vignettes/manual.Rmd index 5844e0e..963df88 100644 --- a/vignettes/manual.Rmd +++ b/vignettes/manual.Rmd @@ -24,14 +24,50 @@ This manual is a work in progress. In the meantime, please read the function doc When a plotting function (e.g., `plot_biomass()`, `table_indices()`, `exp_all_figs_tables()`) is run, placeholders within the plot's associated alternative text and captions are replaced with key quantities (see the [`asar` package's Accessibility vignette](https://nmfs-ost.github.io/asar/articles/accessibility_guide.html) and/or the [`satf::write_captions()` function](https://github.com/nmfs-ost/satf/blob/master/R/write_captions.R) for more details). As stated in the Accessibility vignette, *you should always check that these alternative texts and captions have been assembled correctly.* -There are multiple reasons why you may want to edit these. For instance, maybe you entered an incorrect value for the units when running `plot_biomass()`. Maybe you need to add some extra information to the alternative text accurately describe the plot. In any case, **here's what to do if you want to alter the alternative text and/or captions in `satf`**, depending on which plotting function you're using: +There are multiple reasons why you may want to edit these. For instance, maybe you entered an incorrect value for the units when running `plot_biomass()`. Maybe you need to add some extra information to the alternative text accurately describe the plot. In any case, here's what to do if you want to alter the alternative text and/or captions in `satf`, depending on what you wish to change: -### `exp_all_figs_tables()` +### I want to add or remove text + +Directly edit the rda's caption or alt_text objects. To do so, assign a new value to the text you wish to change. For example: + +1. Locate your rda file. It may be called something like "biomass_figure.rda". + +2. Load it into your R environment. You can do this by clicking the file or by using a command like this: `load("biomass_figure.rda")`. It will probably be called `rda`. + +3. If you want to change the caption to "my new caption", you'd enter the following command: `rda[["cap"]] <- "my new caption"`. To change the alt text, you'd change "cap" to "alt_text" (e.g., `rda[["alt_text"]] <- "my new alt text"`.). + +4. Save the changes to the rda's file (most likely you'll want to overwrite your original rda) by entering the following command: `save(rda, file = 'biomass_figure.rda')`. + + +**NOTE: Your edits will be lost if:** + +- You regenerate (overwrite) the captions_alt_text.csv file +- You rerun `exp_all_figs_tables()` or that plot's function with the `make_rda` argument = T after regenerating the captions_alt_text.csv file. + +We recommend saving a version of your altered text elsewhere to ensure you can reuse it if it were to be overwritten. + +### I want to change/update the arguments or key quantities + +#### `exp_all_figs_tables()` 1. Delete the captions_alt_text.csv file 2. Rerun `exp_all_figs_tables()` with the correct arguments -### individual fxns +**NOTE**: It is possible to directly edit the captions_alt_text.csv file to change the caption or alternative text. However, if you wish to do anything besides adding or removing text, we recommend using the two-step workflow, above, to avoid potential issues where an incorrect argument was used to calculate other (now incorrect) key quantities. For instance, `end_year` may be used to calculate values associated with F, which are then used to calculate other values. + +#### Individual functions (e.g., `plot_biomass()`) + +1. Open your function's R file and find the `topic_label` object. If you set the `relative` argument to TRUE, then choose the `topic_label` with "relative" in it (e.g., "relative.biomass"). + +2. Open your caps_alt_text.csv file. + +3. Find the row in the caps_alt_text.csv file where the "label" column is the `topic_label`. Delete it. + +4. Open the inst/resources/captions_alt_text_template.csv file. Find the row where the "label" column is the `topic_label`. Copy that row. + +5. Paste the row into the caps_alt_text.csv file. + +6. Rerun the function so that the key quantities are replaced correctly. --edit the rda directly (easier if editing, for instance, units instead of an extracted key quantity) -replace the fxn's existing row in the caps_alt_text.csv with the analogous row from the alt text/csv template, then rerun the plotting fxn so that the KQs can be replaced correctly +This process ensures that only the alt text and/or captions for your specific function are being changed, and not those associated with other plotting functions. From 7bae83714094e0f1ce13fa5ab1c30a29a36e108b Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 17 Jan 2025 17:00:58 -0500 Subject: [PATCH 46/54] Updating F-related key quantities' calculations --- R/utils.R | 39 +++++++++++++++++++++++++++++++++++++++ R/write_captions.R | 25 ++++++++++--------------- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/R/utils.R b/R/utils.R index bcccc4e..23bad4b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -81,6 +81,45 @@ add_more_key_quants <- function( dplyr::filter(label == topic, type == fig_or_table) + + # calculate key quantities that rely on end_year for calculation + ## terminal fishing mortality + if (topic_cap_alt$label == "fishing.mortality") { + + 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) + } + } + + # replace placeholders (e.g., if "end.year" is found in topic_alt, replace it with end_year) ## end_year----- if(!is.null(end_year)){ diff --git a/R/write_captions.R b/R/write_captions.R index 6aef8df..00b5410 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -135,8 +135,15 @@ write_captions <- function(dat, # converted model output object dplyr::filter( label == stringr::str_to_lower("F_targ") | label == stringr::str_to_lower("F_proxy") | - label == stringr::str_to_lower("F_msy") + 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) @@ -149,16 +156,7 @@ write_captions <- function(dat, # converted model output object as.numeric() # terminal fishing mortality - # TODO: Put this in utils function, since F.end.year needs end_year - # 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) + # F.end.year : added with add_more_key_quants # minimum F F.min <- dat |> @@ -216,8 +214,7 @@ write_captions <- function(dat, # converted model output object round(digits = 2) # Terminal year F respective to F target - # TODO: Put this in utils function, since F.end.year needs end_year - # F.Ftarg <- F.end.year / Ftarg + # F.Ftarg : added with add_more_key_quants ## landings plot @@ -809,11 +806,9 @@ write_captions <- function(dat, # converted model output object ## mortality (F) plot 'F.ref.pt' = as.character(F.ref.pt), 'F.start.year' = as.character(F.start.year), - # 'F.end.year' = as.character(F.end.year), 'F.min' = as.character(F.min), 'F.max' = as.character(F.max), 'Ftarg' = as.character(Ftarg), - # 'F.Ftarg' = as.character(F.Ftarg), ## landings plot 'landings.start.year' = as.character(landings.start.year), From 63b056883a30f73c965cc9632f9d78b7a34789fc Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 22 Jan 2025 10:46:27 -0500 Subject: [PATCH 47/54] Update manual to emphasize importance of checking/editing alt text --- vignettes/manual.Rmd | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/vignettes/manual.Rmd b/vignettes/manual.Rmd index 963df88..4ae9bf7 100644 --- a/vignettes/manual.Rmd +++ b/vignettes/manual.Rmd @@ -22,9 +22,15 @@ This manual is a work in progress. In the meantime, please read the function doc ## Updating alternative text and captions -When a plotting function (e.g., `plot_biomass()`, `table_indices()`, `exp_all_figs_tables()`) is run, placeholders within the plot's associated alternative text and captions are replaced with key quantities (see the [`asar` package's Accessibility vignette](https://nmfs-ost.github.io/asar/articles/accessibility_guide.html) and/or the [`satf::write_captions()` function](https://github.com/nmfs-ost/satf/blob/master/R/write_captions.R) for more details). As stated in the Accessibility vignette, *you should always check that these alternative texts and captions have been assembled correctly.* +When a plotting function (e.g., `plot_biomass()`, `table_indices()`, `exp_all_figs_tables()`) is run, placeholders within the plot's associated alternative text and captions are replaced with key quantities (see the [`asar` package's Accessibility vignette](https://nmfs-ost.github.io/asar/articles/accessibility_guide.html) and/or the [`satf::write_captions()` function](https://github.com/nmfs-ost/satf/blob/master/R/write_captions.R) for more details). -There are multiple reasons why you may want to edit these. For instance, maybe you entered an incorrect value for the units when running `plot_biomass()`. Maybe you need to add some extra information to the alternative text accurately describe the plot. In any case, here's what to do if you want to alter the alternative text and/or captions in `satf`, depending on what you wish to change: +As stated in the [Accessibility vignette's "Your to-do list" section](https://nmfs-ost.github.io/asar/articles/accessibility_guide.html#your-to-do-list), **you should always:** + +**1. Check that these alternative texts and captions have been assembled correctly.** + +**2. Write the final component of each figure’s alt text.** + +Here's how to alter the alternative text and/or captions in `satf`, depending on what you wish to change: ### I want to add or remove text From 1f01f5e8bddb7fbfa2c3079660cbd0e1b966231c Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 23 Jan 2025 14:13:08 -0500 Subject: [PATCH 48/54] Updated calculation for landings plot's y axis max, since it's a stacked area plot --- R/write_captions.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 00b5410..3b7ad96 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -263,8 +263,13 @@ write_captions <- function(dat, # converted model output object # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output !is.na(fleet) ) |> - dplyr::slice(which.max(estimate)) |> - dplyr::select(estimate) |> + 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) From f1c8ad83bb03158a2a7e98b2e73ba9d947f0a9d9 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 23 Jan 2025 15:06:35 -0500 Subject: [PATCH 49/54] re-removed export_rda and extract_caps_alttext from utils R script (error with rebasing?) --- R/utils.R | 55 ------------------------------------------------------- 1 file changed, 55 deletions(-) diff --git a/R/utils.R b/R/utils.R index 23bad4b..5a54d0b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -2,61 +2,6 @@ # General utility functions #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - - - - # extract plot or table's caption and alt text - cap <- captions_alttext_df |> - dplyr::filter(label == topic_label, - type == fig_or_table) |> - dplyr::select(caption) |> - as.character() - - if (fig_or_table == "figure"){ - alt_text <- captions_alttext_df |> - dplyr::filter(label == topic_label, - type == "figure") |> - dplyr::select(alt_text) |> - as.character() - - caps_alttext_list <- list(cap, - alt_text) - } else { - caps_alttext_list <- list(cap) - } - - return(caps_alttext_list) -} - -# export ggplot, alt text, and caption to rda if indicated in arguments -export_rda <- function(final = final, - caps_alttext = caps_alttext, - rda_dir = rda_dir, - topic_label = topic_label, - fig_or_table = fig_or_table){ - - # make rda for figures - if (fig_or_table == "figure") { - rda <- list("figure" = final, - "cap" = caps_alttext[[1]], - "alt_text" = caps_alttext[[2]]) - # make rda for tables - } else if (fig_or_table == "table"){ - rda <- list("table" = final, - "cap" = caps_alttext[[1]]) - } - - # check if an rda_files folder already exists; if not, make one - if (!dir.exists(fs::path(rda_dir, "rda_files"))) { - dir.create(fs::path(rda_dir, "rda_files")) - } - - save(rda, - file = fs::path(rda_dir, - "rda_files", - paste0(topic_label, "_", fig_or_table, ".rda"))) -} - # substitute in more key quantities (units, end_years, reference points) to captions/alt text add_more_key_quants <- function( topic = topic_label, From c4848f84c60e5339f096a04091272114ae89987b Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Fri, 24 Jan 2025 11:51:41 -0500 Subject: [PATCH 50/54] Updating quantities calculations and naming conventions as per meeting with Sam --- R/write_captions.R | 73 ++++++++++--------- inst/resources/captions_alt_text_template.csv | 4 +- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 3b7ad96..586d8d7 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -102,8 +102,15 @@ write_captions <- function(dat, # converted model output object # R0 R0 <- dat |> - dplyr::filter(grepl('R0', label) | grepl('recruitment_virgin', label)) |> + 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) @@ -383,28 +390,28 @@ write_captions <- function(dat, # converted model output object ## NAA (numbers at age) # start year of NAA plot - bubble.start.year.min <- dat |> + pop.naa.start.year.min <- dat |> dplyr::filter(label == "abundance" & !is.na(year)) |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() # end year of NAA plot - bubble.end.year.max <- dat |> + pop.naa.end.year.max <- dat |> dplyr::filter(label == "abundance" & !is.na(year)) |> dplyr::slice(which.max(year)) |> dplyr::select(year) |> as.numeric() # minimum age - bubble.age.min <- dat |> + pop.naa.age.min <- dat |> dplyr::filter(label == "abundance" & !is.na(year)) |> dplyr::slice(which.min(age)) |> dplyr::select(age) |> as.numeric() # maximum age - bubble.age.max <- dat |> + pop.naa.age.max <- dat |> dplyr::filter(label == "abundance" & !is.na(year)) |> dplyr::slice(which.max(age)) |> dplyr::select(age) |> @@ -677,7 +684,9 @@ write_captions <- function(dat, # converted model output object # minimum spr spr.min <- dat |> dplyr::filter(c(grepl('spr', label) | - label == "spr") & !is.na(year) & !is.na(estimate)) |> + label == "spr") & + !is.na(year) & + !is.na(estimate)) |> dplyr::slice(which.min(estimate)) |> dplyr::select(estimate) |> as.numeric() |> @@ -696,38 +705,35 @@ write_captions <- function(dat, # converted model output object spr.ref.pt <- dat |> dplyr::filter(label == "spr_msy") |> dplyr::select(estimate) |> - as.numeric() |> + as.numeric()# |> round(digits = 2) - ## pop_naa_baa (population numbers at age and population biomass at age) - # start year of pop_naa_baa plot - pop.naa.baa.start.year <- dat |> + ## pop.baa (population biomass at age) + # start year of pop.baa plot + pop.baa.start.year <- dat |> dplyr::filter(label == "abundance") |> dplyr::slice(which.min(year)) |> dplyr::select(year) |> as.numeric() - # end year of pop_naa_baa plot - pop.naa.baa.end.year <- landings.end.year + # end year of pop.baa plot + pop.baa.end.year <- landings.end.year # minimum number of fish - pop.naa.baa.fish.min <- dat |> - dplyr::filter(label == "abundance") |> - dplyr::group_by(year) |> - dplyr::summarize(val = max(estimate)) |> - dplyr::slice(which.min(val)) |> - dplyr::select(val) |> + pop.baa.fish.min <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.min(estimate)) |> + dplyr::select(estimate) |> as.numeric() |> round(digits = 2) + # maximum number of fish - pop.naa.baa.fish.max <- dat |> - dplyr::filter(label == "abundance") |> - dplyr::group_by(year) |> - dplyr::summarize(val = max(estimate)) |> - dplyr::slice(which.max(val)) |> - dplyr::select(val) |> + pop.baa.fish.max <- dat |> + dplyr::filter(label == "abundance" & !is.na(year)) |> + dplyr::slice(which.max(estimate)) |> + dplyr::select(estimate) |> as.numeric() |> round(digits = 2) @@ -749,6 +755,7 @@ write_captions <- function(dat, # converted model output object # 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) |> @@ -879,10 +886,10 @@ write_captions <- function(dat, # converted model output object # 'cpue.max' = as.character(cpue.max), ## NAA (numbers at age) - 'bubble.start.year.min' = as.character(bubble.start.year.min), - 'bubble.end.year.max' = as.character(bubble.end.year.max), - 'bubble.age.min' = as.character(bubble.age.min), - 'bubble.age.max' = as.character(bubble.age.max), + 'pop.naa.start.year.min' = as.character(pop.naa.start.year.min), + 'pop.naa.end.year.max' = as.character(pop.naa.end.year.max), + 'pop.naa.age.min' = as.character(pop.naa.age.min), + 'pop.naa.age.max' = as.character(pop.naa.age.max), ## mod_fit_catch (model fit to catch ts) # 'mod.fit.catch.start.year' = as.character(mod.fit.catch.start.year), @@ -948,11 +955,11 @@ write_captions <- function(dat, # converted model output object 'spr.max' = as.character(spr.max), 'spr.ref.pt' = as.character(spr.ref.pt), - # ## pop_naa_baa (population numbers at age and population biomass at age) - 'pop.naa.baa.start.year' = as.character(pop.naa.baa.start.year), - 'pop.naa.baa.end.year' = as.character(pop.naa.baa.end.year), - 'pop.naa.baa.fish.min' = as.character(pop.naa.baa.fish.min), - 'pop.naa.baa.fish.max' = as.character(pop.naa.baa.fish.max), + # ## 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), ## proj_catch (projected catch) # 'proj.catch.units' = as.character(proj.catch.units), diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index 62be9b3..ec682c2 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -13,7 +13,7 @@ fecundity.at.length,figure,Fecundity (in number of eggs) as a function of fish l CAA,figure,"Histograms showing the proportion of the catch in each age group for each year in 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 age group. The x axis of each histogram shows age groups that span from caa.age.min to caa.age.max. The y axis of each histogram shows the proportion of the catch and spans from 0--1." 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." CPUE.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." -NAA,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 bubble.start.year to bubble.end.year . The y axis shows age in years, which spans from bubble.age.min to bubble.age.max." +pop.naa,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." 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 model-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." @@ -26,7 +26,7 @@ tot.b,figure,Model-estimated biomass (B) time series. The horizontal dashed line spawning.biomass,figure,Model-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 model-estimated spawning stock biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year . The y axis shows model-estimated spawning stock biomass in ssb.units, which spans from ssb.min to ssb.max." relative.spawning.biomass,figure,Model-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 model-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 model-estimated relative spawning stock biomass (SSB/SSB~target~), which spans from rel.ssb.min to rel.ssb.max." spr,figure,Model-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 model-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 model-estimated spawning potential ratio in SSB~current~/SSB~target~, which spans from spr.min to spr.max." -pop.naa.baa,figure,Model-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 model-estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.naa.baa.start.year to pop.naa.baa.end.year . The y axis shows numbers of fish, which spans from pop.naa.baa.fish.min to pop.naa.baa.fish.max." +pop.baa,figure,Model-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 model-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 numbers of fish, which spans 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., From 32c75e75c6a2ba4cc29e358020de4b0afc9036f6 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 27 Jan 2025 10:03:58 -0500 Subject: [PATCH 51/54] Uncommented key quantity-extracting code that didn't work for multiple model results (SS3 & BAM). Also added code to replace NA values with "NA" so that placeholders in captions and alt text are still updated accurately (instead of the entire caption/alt text being changed to NA) --- R/write_captions.R | 178 ++++++++++++++++++++++++--------------------- 1 file changed, 95 insertions(+), 83 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 586d8d7..9b58ff1 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -100,60 +100,63 @@ write_captions <- function(dat, # converted model output object 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) + # 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 <- + # 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) + # 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 <- + # 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) - + # 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) + # 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 |> @@ -210,15 +213,15 @@ write_captions <- function(dat, # converted model output object 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) + # 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 @@ -557,15 +560,15 @@ write_captions <- function(dat, # converted model output object # 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) + # # 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 @@ -664,20 +667,22 @@ write_captions <- function(dat, # converted model output object # ssb reference point # ssb.ref.pt : added with add_more_key_quants - ssbtarg <- dat |> - dplyr::filter(c(grepl('spawning_biomass', label) & grepl('msy$', label) & estimate >1) | label == 'spawning_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 + # ssbtarg <- dat |> + # dplyr::filter(c(grepl('spawning_biomass', label) & grepl('msy$', label) & estimate >1) | label == 'spawning_biomass_msy$') |> + # dplyr::pull(estimate) |> + # as.numeric() |> + # round(digits = 2) + # TODO: uncomment and recode once we get clarity about how to extract ssbtarg properly ## 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) + # rel.ssb.min <- (ssb.min / ssbtarg) |> + # round(digits = 2) + # + # # relative ssb max + # rel.ssb.max <- (ssb.max / ssbtarg) |> + # round(digits = 2) ## spr (spawning potential ratio) @@ -701,12 +706,13 @@ write_captions <- function(dat, # converted model output object 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) + # spr.ref.pt <- dat |> + # dplyr::filter(label == "spr_msy") |> + # dplyr::select(estimate) |> + # as.numeric()# |> + # round(digits = 2) ## pop.baa (population biomass at age) @@ -789,6 +795,8 @@ write_captions <- function(dat, # converted model output object # the real values, extracted above + + # make list with all placeholders # uncomment placeholders once uncommented, above patterns_replacements <- c( @@ -803,24 +811,24 @@ write_captions <- function(dat, # converted model output object ## 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), + # '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), 'B.min' = as.character(B.min), 'B.max' = as.character(B.max), - 'R0' = as.character(R0), + # 'R0' = as.character(R0), # 'Bend' = as.character(Bend), - 'Btarg' = as.character(Btarg), + # 'Btarg' = as.character(Btarg), # 'Bmsy' = as.character(Bmsy), ## mortality (F) plot - 'F.ref.pt' = as.character(F.ref.pt), + # '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), + # 'Ftarg' = as.character(Ftarg), ## landings plot 'landings.start.year' = as.character(landings.start.year), @@ -926,8 +934,8 @@ write_captions <- function(dat, # converted model output object # 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), + # '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), @@ -940,8 +948,8 @@ write_captions <- function(dat, # converted model output object # relative ssb # NOTE: moving this above recruitment so rel.ssb.min isn't changed to # "rel." + ssb.min), etc. - 'rel.ssb.min' = as.character(rel.ssb.min), - 'rel.ssb.max' = as.character(rel.ssb.max), + # 'rel.ssb.min' = as.character(rel.ssb.min), + # 'rel.ssb.max' = as.character(rel.ssb.max), ## spawning.biomass (ssb) 'ssb.start.year' = as.character(ssb.start.year), @@ -953,7 +961,7 @@ write_captions <- function(dat, # converted model output object ## spr (spawning potential ratio) 'spr.min' = as.character(spr.min), 'spr.max' = as.character(spr.max), - 'spr.ref.pt' = as.character(spr.ref.pt), + # 'spr.ref.pt' = as.character(spr.ref.pt), # ## pop.baa (population biomass at age) 'pop.baa.start.year' = as.character(pop.baa.start.year), @@ -984,6 +992,10 @@ write_captions <- function(dat, # converted model output object ) + # 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". From 2d42cf1788c43a0b4a76db6c33afa583f83c75cb Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Mon, 27 Jan 2025 11:00:12 -0500 Subject: [PATCH 52/54] Extracted 2 more key quantities (min, max M rate) --- R/write_captions.R | 56 +++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/R/write_captions.R b/R/write_captions.R index 9b58ff1..563f4fe 100644 --- a/R/write_captions.R +++ b/R/write_captions.R @@ -284,28 +284,48 @@ write_captions <- function(dat, # converted model output object round(digits = 2) ## natural mortality (M)- bam examples have label as natural_mortality - ## but other formats don't (in input) (use if statement) + ## but other formats don't (in input) # minimum age of M - # M.age.min <- dat |> - # dplyr::select(age) |> - # dplyr::filter(!is.na(age)) |> - # dplyr::slice(which.min(age)) |> - # as.numeric() - # - # # maximum age of M - # M.age.max <- dat |> - # dplyr::select(age) |> - # dplyr::filter(!is.na(age)) |> - # dplyr::slice(which.max(age)) |> - # as.numeric() - - # minimum M rate + 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 + # 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 @@ -837,8 +857,8 @@ write_captions <- function(dat, # converted model output object '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.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), From f004ea394b5d6ca800ff5e65254cf1cff7e71b35 Mon Sep 17 00:00:00 2001 From: Sophie Breitbart Date: Mon, 27 Jan 2025 14:36:07 -0500 Subject: [PATCH 53/54] Update vignettes/manual.Rmd Co-authored-by: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> --- vignettes/manual.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/manual.Rmd b/vignettes/manual.Rmd index 4ae9bf7..67be6ee 100644 --- a/vignettes/manual.Rmd +++ b/vignettes/manual.Rmd @@ -32,7 +32,7 @@ As stated in the [Accessibility vignette's "Your to-do list" section](https://nm Here's how to alter the alternative text and/or captions in `satf`, depending on what you wish to change: -### I want to add or remove text +### Add or Remove Text Directly edit the rda's caption or alt_text objects. To do so, assign a new value to the text you wish to change. For example: From 156c70520ae4abe2b1f4c6257d615d4d1c8b5959 Mon Sep 17 00:00:00 2001 From: Sophie Breitbart Date: Mon, 27 Jan 2025 14:36:34 -0500 Subject: [PATCH 54/54] Update vignettes/manual.Rmd Co-authored-by: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> --- vignettes/manual.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/manual.Rmd b/vignettes/manual.Rmd index 67be6ee..06d6cfe 100644 --- a/vignettes/manual.Rmd +++ b/vignettes/manual.Rmd @@ -52,7 +52,7 @@ Directly edit the rda's caption or alt_text objects. To do so, assign a new valu We recommend saving a version of your altered text elsewhere to ensure you can reuse it if it were to be overwritten. -### I want to change/update the arguments or key quantities +### Change/Update Arguments or Key Quantities #### `exp_all_figs_tables()`