Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions R/convert_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -2190,9 +2190,9 @@ convert_output <- function(
# temporarily add call to local csv so I can test
# con_file <- glue::glue("~/GitHub/stockplotr/inst/resources/{model}_var_names.csv")
var_names_sheet <- utils::read.csv(con_file, na.strings = "")

if (tolower(model) == "bam") {
var_names_sheet <- var_names_sheet |>
var_names_sheet <- var_names_sheet |>
dplyr::mutate(label = tolower(label))
}

Expand Down
96 changes: 62 additions & 34 deletions R/plot_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ plot_biomass <- function(
cli::cli_alert_warning("Scale amount is not applicable when relative = TRUE. Resetting scale_amount to 1.")
scale_amount <- 1
}

orig_group <- group
orig_facet <- facet

# Filter data for biomass
# TODO: determine method to ID that first point in the timeseries is actually Bunfished ref pt
prepared_data <- filter_data(
Expand Down Expand Up @@ -177,63 +177,91 @@ plot_biomass <- function(
### Make RDA ----
if (make_rda) {
if (relative) {
rel.B.min <- calc_kqs(returned_kq = "rel.B.min",
final = final)
rel.B.max <- calc_kqs(returned_kq = "rel.B.max",
final = final)
rel.B.min <- calc_kqs(
returned_kq = "rel.B.min",
final = final
)
rel.B.max <- calc_kqs(
returned_kq = "rel.B.max",
final = final
)

# calculate & export key quantities
export_kqs(rel.B.min, rel.B.max)

# Add key quantities to captions/alt text
insert_kqs(rel.B.min, rel.B.max)
} else {
B.min <- calc_kqs(returned_kq = "B.min",
prepared_data = prepared_data)
B.max <- calc_kqs(returned_kq = "B.max",
prepared_data = prepared_data)
B.min <- calc_kqs(
returned_kq = "B.min",
prepared_data = prepared_data
)
B.max <- calc_kqs(
returned_kq = "B.max",
prepared_data = prepared_data
)

export_kqs(B.min, B.max)
insert_kqs(B.min, B.max)
}

B.ref.pt <- as.character(ref_line)
B.units <- as.character(unit_label)
B.start.year <- calc_kqs(returned_kq = "B.start.year",
prepared_data = prepared_data)
B.end.year <- calc_kqs(returned_kq = "B.end.year",
prepared_data = prepared_data)
B.terminal.year <- calc_kqs(returned_kq = "B.terminal.year",
dat = dat,
relative = relative)
B.start.year <- calc_kqs(
returned_kq = "B.start.year",
prepared_data = prepared_data
)
B.end.year <- calc_kqs(
returned_kq = "B.end.year",
prepared_data = prepared_data
)
B.terminal.year <- calc_kqs(
returned_kq = "B.terminal.year",
dat = dat,
relative = relative
)

# SS3, FIMS
if ("spawning_biomass_msy" %in% dat$label) {
sb_msy <- calc_kqs(returned_kq = "sb_msy",
dat = dat)
b_sb_msy <- calc_kqs(returned_kq = "b_sb_msy",
dat = dat)
B.msy <- calc_kqs(returned_kq = "B.msy",
dat = dat)
sb_msy <- calc_kqs(
returned_kq = "sb_msy",
dat = dat
)
b_sb_msy <- calc_kqs(
returned_kq = "b_sb_msy",
dat = dat
)
B.msy <- calc_kqs(
returned_kq = "B.msy",
dat = dat
)
B.msy.min <- NA
B.msy.max <- NA
# BAM
# BAM
} else if ("bmsy" %in% dat$label) {
B.msy <- calc_kqs(returned_kq = "B.msy",
dat = dat)
B.msy.min_uncert <- calc_kqs(returned_kq = "B.msy.min_uncert",
dat = dat)
B.msy.min <- calc_kqs(returned_kq = "B.msy.min",
dat = dat)
B.msy.max <- calc_kqs(returned_kq = "B.msy.max",
dat = dat)
# Rceattle
B.msy <- calc_kqs(
returned_kq = "B.msy",
dat = dat
)
B.msy.min_uncert <- calc_kqs(
returned_kq = "B.msy.min_uncert",
dat = dat
)
B.msy.min <- calc_kqs(
returned_kq = "B.msy.min",
dat = dat
)
B.msy.max <- calc_kqs(
returned_kq = "B.msy.max",
dat = dat
)
# Rceattle
} else {
B.msy <- NA
B.msy.min <- NA
B.msy.max <- NA
}

export_kqs(
B.ref.pt,
B.units,
Expand Down
31 changes: 20 additions & 11 deletions R/plot_fishing_mortality.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,35 @@ plot_fishing_mortality <- function(

### Make RDA ----
if (make_rda) {
F.min <- calc_kqs(returned_kq = "F.min",
prepared_data = prepared_data)
F.max <- calc_kqs(returned_kq = "F.max",
prepared_data = prepared_data)
F.min <- calc_kqs(
returned_kq = "F.min",
prepared_data = prepared_data
)
F.max <- calc_kqs(
returned_kq = "F.max",
prepared_data = prepared_data
)

export_kqs(F.min, F.max)
insert_kqs(F.min, F.max)

F.ref.pt <- as.character(ref_line)
F.start.year <- min(prepared_data$year)
F.end.year <- max(prepared_data$year)
F.terminal.year <- calc_kqs(returned_kq = "F.terminal.year",
F.terminal.year <- calc_kqs(
returned_kq = "F.terminal.year",
dat = dat
)

F.target <- calc_kqs(
returned_kq = "F.target",
dat = dat
)
f.limit <- calc_kqs(
returned_kq = "f.limit",
dat = dat
)

F.target <- calc_kqs(returned_kq = "F.target",
dat = dat)
f.limit <- calc_kqs(returned_kq = "f.limit",
dat = dat)


export_kqs(
F.ref.pt,
F.start.year,
Expand Down
18 changes: 11 additions & 7 deletions R/plot_spawning_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,17 @@ plot_spawning_biomass <- function(
### Make RDA ----
if (make_rda) {
if (relative) {
rel.ssb.min <- calc_kqs(returned_kq = "rel.ssb.min",
final = final)


rel.ssb.max <- calc_kqs(returned_kq = "rel.ssb.max",
final = final)

rel.ssb.min <- calc_kqs(
returned_kq = "rel.ssb.min",
final = final
)


rel.ssb.max <- calc_kqs(
returned_kq = "rel.ssb.max",
final = final
)

# calculate & export key quantities
export_kqs(rel.ssb.min, rel.ssb.max)

Expand Down
6 changes: 4 additions & 2 deletions R/plot_stock_recruitment.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ plot_stock_recruitment <- function(
# Make RDA
if (make_rda) {
# Obtain relevant key quantities for captions/alt text
sr.age.min <- calc_kqs(returned_kq = "sr.age.min",
dat = dat)
sr.age.min <- calc_kqs(
returned_kq = "sr.age.min",
dat = dat
)
sr.ssb.units <- spawning_biomass_label
sr.ssb.min <- min(sr$spawning_biomass, na.rm = TRUE) |> round(digits = 3)
sr.ssb.max <- max(sr$spawning_biomass, na.rm = TRUE) |> round(digits = 3)
Expand Down
2 changes: 1 addition & 1 deletion R/process_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ process_table <- function(
dplyr::rename(
!!mod_uncert_lab := uncertainty
) |>
# set values to strings to include trailing zeros from rounding and # format large estimate values with commas
# set values to strings to include trailing zeros from rounding and # format large estimate values with commas
dplyr::mutate(estimate = formatC(estimate, format = "f", digits = digits, big.mark = ",")) |>
tidyr::pivot_wider(
id_cols = dplyr::all_of(c(stringr::str_to_title(mod_cols))),
Expand Down
68 changes: 34 additions & 34 deletions R/table_index.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,17 @@
#' )
#' }
table_index <- function(
dat,
era = NULL,
interactive = TRUE,
group = NULL,
method = "sum",
module = NULL,
label = NULL,
digits = 2,
make_rda = FALSE,
tables_dir = getwd()
) {

dat,
era = NULL,
interactive = TRUE,
group = NULL,
method = "sum",
module = NULL,
label = NULL,
digits = 2,
make_rda = FALSE,
tables_dir = getwd()
) {
# TODO: do group and facet need to be uncommented and updated?
# Filter data for landings
prepared_data <- filter_data(
Expand All @@ -71,37 +70,39 @@ table_index <- function(
scale_amount = 1,
interactive = interactive
) |>
dplyr::mutate(estimate = round(as.numeric(estimate), digits = digits),
uncertainty = round(as.numeric(uncertainty), digits = digits))

dplyr::mutate(
estimate = round(as.numeric(estimate), digits = digits),
uncertainty = round(as.numeric(uncertainty), digits = digits)
)

# Add check if there is any data
if (nrow(prepared_data) == 0) {
cli::cli_abort("No index data found.")
}

# get uncertainty label by model
uncert_lab <- prepared_data |>
dplyr::filter(!is.na(uncertainty_label)) |>
dplyr::group_by(model) |>
dplyr::reframe(unique_uncert = unique(uncertainty_label)) # changed to reframe -- may cause errors
uncert_lab <- stats::setNames(uncert_lab$unique_uncert, uncert_lab$model)
# if (length(unique(uncert_lab)) == 1) uncert_lab <- unique(uncert_lab) # might need this line

# This needs to be adjusted when comparing different models and diff error
if (length(uncert_lab) > 1 & length(unique(uncert_lab)) == 1 | length(names(uncert_lab)) == 1) { # prepared_data$model
# cli::cli_alert_warning("More than one value for uncertainty exists: {uncert_lab}")
uncert_lab <- uncert_lab[[1]]
# cli::cli_alert_warning("The first value ({uncert_lab}) will be chosen.")
}

if (is.na(uncert_lab)) uncert_lab <- "uncertainty"

# get fleet names
# TODO: change from fleets to id_group AFTER the process data step and adjust throughout the table based on indexing
fleets <- unique(prepared_data$fleet) |>
# sort numerically even if fleets are 100% characters
stringr::str_sort(numeric = TRUE)

# TODO: fix this so that fleet names aren't removed if, e.g., group = "fleet"
table_data_info <- process_table(
dat = prepared_data,
Expand All @@ -113,44 +114,43 @@ table_index <- function(
table_data <- table_data_info[[1]]
indexed_vars <- table_data_info[[2]]
id_col_vals <- table_data_info[[3]]

# id_group_vals <- sapply(id_cols, function(x) unique(prepared_data[[x]]), simplify = FALSE)
# TODO: add check if there is a index column for every error column -- if not remove the error (can keep index)

# if (uncert_lab != "") uncert_lab <- glue::glue("({uncert_lab})")

# merge error and index columns and rename
df_list <- merge_error(
table_data,
id_col_vals,
unit_label = "", # should this be CPUE?
uncert_lab
)

# transform dfs into tables
final <- lapply(df_list, function(df) {
df |>
gt::gt() |>
add_theme()
})

# export figure to rda if argument = T
if (make_rda == TRUE) {

# Caption contains no key quantities for index table
# So, export captions/alt text csv if absent
if (!file.exists(fs::path(getwd(), "captions_alt_text.csv"))) {
caps_alttext <- utils::read.csv(
system.file("resources", "captions_alt_text_template.csv", package = "stockplotr")
)
# export df with captions and alt text to csv
utils::write.csv(
x = caps_alttext,
file = fs::path(getwd(), "captions_alt_text.csv"),
row.names = FALSE
)
# export df with captions and alt text to csv
utils::write.csv(
x = caps_alttext,
file = fs::path(getwd(), "captions_alt_text.csv"),
row.names = FALSE
)
}

if (length(df_list) == 1) {
create_rda(
object = final$label,
Expand All @@ -168,7 +168,7 @@ table_index <- function(
cli::cli_alert_warning("Multiple tables cannot be exported at this time.")
cli::cli_alert_info("We are currently developing this feature.")
}

# Send table(s) to viewer
if (!is.data.frame(table_data)) {
for (t in final) {
Expand Down
Loading