Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting more key quantities #42

Merged
merged 56 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
ddb7c8d
first commit
sbreitbart-NOAA Jan 10, 2025
fa94b09
added period after end.year in template csv so that period is preserv…
sbreitbart-NOAA Jan 13, 2025
eb17968
Added notes and edits from Friday's coworking meeting. Notes explain …
sbreitbart-NOAA Jan 13, 2025
f0911ef
Small updates to broaden new fxn that inserts more key quantities- in…
sbreitbart-NOAA Jan 13, 2025
92e98e0
Continue updating key quantities in write_captions and alt text/caps csv
sbreitbart-NOAA Jan 13, 2025
34d21f8
first commit
sbreitbart-NOAA Jan 10, 2025
231c142
Added notes and edits from Friday's coworking meeting. Notes explain …
sbreitbart-NOAA Jan 13, 2025
29a83b0
Small updates to broaden new fxn that inserts more key quantities- in…
sbreitbart-NOAA Jan 13, 2025
e6d2762
Continue updating key quantities in write_captions and alt text/caps csv
sbreitbart-NOAA Jan 13, 2025
400c298
Continue updating key quantities in write_captions and alt text/caps csv
sbreitbart-NOAA Jan 13, 2025
6075458
Merge branch 'key-quants-2' of https://github.com/nmfs-ost/satf into …
sbreitbart-NOAA Jan 13, 2025
2763323
Finished first working draft of add_more_key_quants fxn; updated alt …
sbreitbart-NOAA Jan 15, 2025
f484780
Small updates to fix regex matching
sbreitbart-NOAA Jan 15, 2025
ba4a93d
added new fxn (add_more_key_quants) to plotting fxns
sbreitbart-NOAA Jan 15, 2025
8077ef2
updated add_more_key_quants fxn to handle two units (arguments) speci…
sbreitbart-NOAA Jan 15, 2025
aa6222f
minor update to test-plot_landings
sbreitbart-NOAA Jan 15, 2025
4f2a362
-fixed bug in add_more_key_quants to allow str_replace_all to work ev…
sbreitbart-NOAA Jan 15, 2025
42be843
Bring dependency and use of nmfspalette back into package
Schiano-NOAA Jan 15, 2025
8cab783
Missed uncommenting + in code - resulted in failed tests
Schiano-NOAA Jan 16, 2025
e5e0c2b
coded more key quantities
sbreitbart-NOAA Jan 16, 2025
a1f3989
started "manual" vignette
sbreitbart-NOAA Jan 16, 2025
442e89c
Changed projected biomass csv entry from figure to table; updated key…
sbreitbart-NOAA Jan 16, 2025
24679ed
Started updating manual with documentation for new fxn
sbreitbart-NOAA Jan 17, 2025
b5dc676
fix(table_indices): add missing extract_caps_alttext argument
sbreitbart-NOAA Jan 17, 2025
5ab2a8f
Finished updating new section in manual
sbreitbart-NOAA Jan 17, 2025
59be9e1
Updating F-related key quantities' calculations
sbreitbart-NOAA Jan 17, 2025
a5191ee
Moving utils functions into their own .R files as an attempt towards …
sbreitbart-NOAA Jan 21, 2025
9682059
Update manual to emphasize importance of checking/editing alt text
sbreitbart-NOAA Jan 22, 2025
b45412c
Created tests for new functions (export_rda and extract_caps_alttext)
sbreitbart-NOAA Jan 22, 2025
ac9bdd9
Updated calculation for landings plot's y axis max, since it's a stac…
sbreitbart-NOAA Jan 23, 2025
e731930
first commit
sbreitbart-NOAA Jan 10, 2025
5042e73
Added notes and edits from Friday's coworking meeting. Notes explain …
sbreitbart-NOAA Jan 13, 2025
007e0b7
Small updates to broaden new fxn that inserts more key quantities- in…
sbreitbart-NOAA Jan 13, 2025
5ea8a4d
Continue updating key quantities in write_captions and alt text/caps csv
sbreitbart-NOAA Jan 13, 2025
5c67b30
Continue updating key quantities in write_captions and alt text/caps csv
sbreitbart-NOAA Jan 13, 2025
ceabe80
Finished first working draft of add_more_key_quants fxn; updated alt …
sbreitbart-NOAA Jan 15, 2025
fb42b6d
Small updates to fix regex matching
sbreitbart-NOAA Jan 15, 2025
caf1b9f
added new fxn (add_more_key_quants) to plotting fxns
sbreitbart-NOAA Jan 15, 2025
10a3a36
updated add_more_key_quants fxn to handle two units (arguments) speci…
sbreitbart-NOAA Jan 15, 2025
e3e61f6
minor update to test-plot_landings
sbreitbart-NOAA Jan 15, 2025
2c8de34
-fixed bug in add_more_key_quants to allow str_replace_all to work ev…
sbreitbart-NOAA Jan 15, 2025
df15d98
coded more key quantities
sbreitbart-NOAA Jan 16, 2025
523542d
started "manual" vignette
sbreitbart-NOAA Jan 16, 2025
12327b7
Changed projected biomass csv entry from figure to table; updated key…
sbreitbart-NOAA Jan 16, 2025
ea6c439
Started updating manual with documentation for new fxn
sbreitbart-NOAA Jan 17, 2025
7f6de5d
Finished updating new section in manual
sbreitbart-NOAA Jan 17, 2025
7bae837
Updating F-related key quantities' calculations
sbreitbart-NOAA Jan 17, 2025
63b0568
Update manual to emphasize importance of checking/editing alt text
sbreitbart-NOAA Jan 22, 2025
1f01f5e
Updated calculation for landings plot's y axis max, since it's a stac…
sbreitbart-NOAA Jan 23, 2025
1805490
Merge branch 'key-quants-2' of https://github.com/nmfs-ost/satf into …
sbreitbart-NOAA Jan 23, 2025
f1c8ad8
re-removed export_rda and extract_caps_alttext from utils R script (e…
sbreitbart-NOAA Jan 23, 2025
c4848f8
Updating quantities calculations and naming conventions as per meetin…
sbreitbart-NOAA Jan 24, 2025
32c75e7
Uncommented key quantity-extracting code that didn't work for multipl…
sbreitbart-NOAA Jan 27, 2025
2d42cf1
Extracted 2 more key quantities (min, max M rate)
sbreitbart-NOAA Jan 27, 2025
f004ea3
Update vignettes/manual.Rmd
sbreitbart-NOAA Jan 27, 2025
156c705
Update vignettes/manual.Rmd
sbreitbart-NOAA Jan 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ captions_alt_text.csv
converted_file_1126.csv
rda_files
petrale_sole_std_output.csv
inst/doc
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion R/plot_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -189,6 +199,7 @@ plot_biomass <- function(
rda_dir = rda_dir,
topic_label = topic_label,
fig_or_table = fig_or_table)
}

}
return(final)
}
8 changes: 8 additions & 0 deletions R/plot_indices.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions R/plot_landings.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions R/plot_recruitment.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions R/plot_recruitment_deviations.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions R/plot_spawn_recruitment.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions R/plot_spawning_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
121 changes: 121 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,124 @@ export_rda <- function(final = final,
"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,
fig_or_table = fig_or_table,
dir = NULL,
end_year = NULL,
units = NULL,
sr_ssb_units = NULL,
sr_recruitment_units = NULL,
ref_pt = 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)

# replace placeholders (e.g., if "end.year" is found in topic_alt, replace it with end_year)
## end_year-----
if(!is.null(end_year)){
### alt text
### this regex preserves the comma after the end year
topic_cap_alt <- topic_cap_alt |>
dplyr::mutate(alt_text = stringr::str_replace_all(alt_text,
stringr::regex("(\\S*end\\.year\\S*)(?=\\s?,)"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever I use the str_replace_all fxn, I haven't identified the regex as one using the regex() function. Have you found this is necessary for the code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested another regex option without stringr::regex. Would you recommend something different?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I gotcha. I was just curious because I haven't had any issues with just using a regex there without making it explicit through that function

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-----
Schiano-NOAA marked this conversation as resolved.
Show resolved Hide resolved
if(!is.null(units)){
### caption
### this regex preserves the closing ) after the units
topic_cap_alt <- topic_cap_alt |>
dplyr::mutate(caption = stringr::str_replace_all(caption,
stringr::regex("(\\S*units\\S*)(?=\\s?\\))"),
as.character(units)))

### this regex preserves the period after the units
topic_cap_alt <- topic_cap_alt |>
dplyr::mutate(caption = stringr::str_replace_all(caption,
stringr::regex("(\\S*units\\S*)(?=\\s?.)"),
as.character(units)))

### this regex replaces the units if it's not found with the previous two commands
### (i.e., there's no parenthesis or period adjacent to the units variable)
topic_cap_alt <- topic_cap_alt |>
dplyr::mutate(caption = stringr::str_replace_all(caption,
stringr::regex("\\S*units\\S*"),
as.character(units)))
}

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 |>
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-----
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,
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)

}
Loading
Loading