Skip to content

Commit

Permalink
Merge branch 'master' into reformat-fxns
Browse files Browse the repository at this point in the history
  • Loading branch information
Schiano-NOAA committed Dec 17, 2024
2 parents 44215d8 + 742c212 commit 0c06efd
Show file tree
Hide file tree
Showing 28 changed files with 624 additions and 271 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
.Ruserdata
petrale_convert_output.csv
captions_alt_text.csv
converted_file_1126.csv
rda_files
4 changes: 4 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.2
Imports:
data.table,
dplyr,
flextable,
ggplot2,
Expand All @@ -34,3 +35,6 @@ Remotes:
nmfs-fish-tools/nmfspalette
Depends:
R (>= 2.10)
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(add_theme)
export(exp_fig_accessible)
export(exp_all_figs_tables)
export(plot_biomass)
export(plot_landings)
export(plot_recruitment)
Expand Down
133 changes: 133 additions & 0 deletions R/exp_all_figs_tables.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#' Export all figures and tables
#'
#' Export all figures and tables to Rda files within one function.
#'
#' @inheritParams plot_recruitment
#' @param ref_line @inheritParams plot_biomass
#' @param spawning_biomass_label @inheritParams plot_spawn_recruitment
#' @param recruitment_label @inheritParams plot_spawn_recruitment
#' @param ref_line_sb Identical definition as `ref_line`, but this argument is
#' applied to plot_spawning_biomass.
#'
#' @return
#'
#' @export
#'
#' @examples
#' \dontrun{
#' exp_all_figs_tables(dat, ref_line = "unfished",
#' ref_line_sb = "target", make_rda = TRUE)
#' }

exp_all_figs_tables <- function(

# imported from plot_recruitment
dat,
unit_label = "metric tons",
scale_amount = 1,
end_year = NULL,
n_projected_years = 10,
relative = FALSE,
rda_dir = getwd(),

# imported from plot_biomass
ref_line = c("target", "MSY", "msy", "unfished"),

# imported from plot_landings- zero unique arguments

# imported from plot_recruitment_deviations- zero unique arguments

# imported from plot_spawn_recruitment
spawning_biomass_label = "metric tons",
recruitment_label = "metric tons",

# imported from plot_spawning_biomass
# changed ref_line to ref_line_sb to differentiate from plot_biomass arg
ref_line_sb = c("target", "MSY", "msy", "unfished")

# imported from table_afsc_tier- add potential unique arguments after dev
# imported from table_bnc- add potential unique arguments after dev
# imported from table_harvest_projection- add potential unique arguments after dev
# imported from table_indices- zero unique arguments

) {

make_rda = TRUE

# figures

satf::plot_recruitment(dat,
unit_label,
scale_amount,
end_year,
n_projected_years,
relative,
make_rda,
rda_dir) |> suppressWarnings() |> invisible()

message("Exported plot_recruitment")

satf::plot_biomass(dat,
unit_label,
scale_amount,
ref_line,
end_year,
relative,
make_rda,
rda_dir) |> suppressWarnings() |> invisible()

message("Exported plot_biomass")

satf::plot_landings(dat,
unit_label,
make_rda,
rda_dir) |> suppressWarnings() |> invisible()

message("Exported plot_landings")

satf::plot_recruitment_deviations(dat,
end_year,
n_projected_years,
make_rda,
rda_dir) |> suppressWarnings() |> invisible()

message("Exported plot_recruitment_deviations")

satf::plot_spawn_recruitment(dat,
spawning_biomass_label,
recruitment_label,
end_year,
make_rda,
rda_dir) |> suppressWarnings() |> invisible()

message("Exported plot_spawn_recruitment")

satf::plot_spawning_biomass(dat,
unit_label,
scale_amount,
ref_line_sb,
end_year,
relative,
n_projected_years,
make_rda,
rda_dir) |>
# suppressWarnings() |>
invisible()

message("Exported plot_spawning_biomass")

# tables

# uncomment when this is working properly
# table_indices(dat,
# make_rda,
# rda_dir) |> suppressWarnings() |> invisible()
#
# message("Exported table_indices")
#
# undeveloped tables - add arguments after more development
# table_afsc_tier() |> suppressWarnings() |> invisible()
# table_bnc() |> suppressWarnings() |> invisible()
# table_harvest_projection() |> suppressWarnings() |> invisible()

}
69 changes: 0 additions & 69 deletions R/exp_fig_accessible.R

This file was deleted.

106 changes: 70 additions & 36 deletions R/plot_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#' lower-case letters but you must use one of the options specified in the
#' default list to ensure that the label on the figure looks correct
#' regardless of how it is specified in `dat`.
#' @param ref_point A known value of the reference point along with the label
#' for the reference point as specified in the output file. Please use this
#' option if the ref_line cannot find your desired point. Indicate the
#' reference point in the form c("label" = value).
#' @return Plot total biomass from a stock assessment model as found in a NOAA
#' stock assessment report. Units of total biomass can either be manually added
#' or will be extracted from the provided file if possible. In later releases, model will not
Expand All @@ -18,12 +22,17 @@ plot_biomass <- function(
unit_label = "metric tons",
scale_amount = 1,
ref_line = c("target", "MSY", "msy", "unfished"),
ref_point = NULL,
end_year = NULL,
relative = FALSE
relative = FALSE,
make_rda = FALSE,
rda_dir = getwd()
){

if(length(ref_line)>1){
ref_line = "target"
if (!is.null(ref_point)) {
ref_line <- names(ref_point)
} else if(length(ref_line)>1){
ref_line <- "target"
} else {
ref_line <- match.arg(ref_line, several.ok = FALSE)
}
Expand All @@ -37,22 +46,45 @@ plot_biomass <- function(
# Select value for reference line and label
# update the target option later
# TODO: add option to indicate the reference pt
ref_line_val <- as.numeric(dat[
grep(
pattern = glue::glue("^biomass.*{tolower(ref_line)}$"),
x = dat[["label"]]
),
"estimate"
])
if (!is.null(ref_point)) {
ref_line_val <- as.numeric(ref_point)
} else {
if ( inherits( try( solve(as.numeric(dat[
grep(
pattern = glue::glue("^biomass.*{tolower(ref_line)}$"),
x = dat[["label"]]
),
"estimate"
])), silent = TRUE), "try-error")) {
ref_line_val <- NULL
} else {
ref_line_val <- as.numeric(dat[
grep(
pattern = glue::glue("^biomass.*{tolower(ref_line)}$"),
x = dat[["label"]]
),
"estimate"
])
}
# ref_line_val <- as.numeric(dat[
# grep(
# pattern = glue::glue("^biomass.*{tolower(ref_line)}$"),
# x = dat[["label"]]
# ),
# "estimate"
# ])
}

if (length(ref_line_val) == 0) {
stop(glue::glue(
warning(glue::glue(
"The resulting reference value of `biomass_{ref_line}` was
not found in `dat[[\"label\"]]`."
))
warning("Reference line will not be plotted on the figure.")
} else if (length(ref_line_val) > 1) {
warning(glue::glue(
"More than one of the resulting reference value of 'biomass_{ref_line}` was
not in `dat[[\"label\"]]`."
not in `dat[[\"label\"]]`. \n Both reference points will be plotted on the figure."
))
}

Expand Down Expand Up @@ -97,47 +129,49 @@ plot_biomass <- function(
ymax = estimate_upper),
colour = "grey",
alpha = 0.3) +
ggplot2::geom_hline(
yintercept = ref_line_val / ifelse(relative, ref_line_val, scale_amount),
linetype = 2) +
{if(!is.null(ref_line_val)) ggplot2::geom_hline(yintercept = ref_line_val / ifelse(relative, ref_line_val, scale_amount),linetype = 2)} +
ggplot2::labs(
x = "Year",
y = biomass_label) +
ggplot2::scale_x_continuous(
n.breaks = x_n_breaks,
guide = ggplot2::guide_axis(minor.ticks = TRUE))
guide = ggplot2::guide_axis(minor.ticks = TRUE)) +
ggplot2::annotate(
geom = "text",
x = end_year + 0.05,
y = ref_line_val / ifelse(relative, ref_line_val, scale_amount),
label = list(bquote(B[.(ref_line)])),
parse = TRUE
)

plt_fin <- add_theme(plt)

# create plot-specific variables to use throughout fxn for naming and IDing
topic_label <- "biomass"

# identify output
fig_or_table <- "figure"
# export figure to rda if argument = T
if (make_rda == TRUE){
# create plot-specific variables to use throughout fxn for naming and IDing
topic_label <- "biomass"

# run write_captions.R if its output doesn't exist
if (!file.exists(
fs::path(getwd(), "captions_alt_text.csv"))
) {
satf::write_captions(dat = dat,
dir = getwd(),
year = end_year)
}
# identify output
fig_or_table <- "figure"

# extract this plot's caption and alt text
caps_alttext <- extract_caps_alttext(topic_label = topic_label,
fig_or_table = fig_or_table)
# run write_captions.R if its output doesn't exist
if (!file.exists(
fs::path(getwd(), "captions_alt_text.csv"))
) {
satf::write_captions(dat = dat,
dir = rda_dir,
year = end_year)
}

# export figure to rda if argument = T
if (make_rda == TRUE){
# extract this plot's caption and alt text
caps_alttext <- extract_caps_alttext(topic_label = topic_label,
fig_or_table = fig_or_table)

export_rda(plt_fin = plt_fin,
caps_alttext = caps_alttext,
rda_dir = rda_dir,
topic_label = topic_label,
fig_or_table = fig_or_table)

}

return(plt_fin)
}
Loading

0 comments on commit 0c06efd

Please sign in to comment.