diff --git a/R/convert_output.R b/R/convert_output.R index 745b6f0d..54573788 100644 --- a/R/convert_output.R +++ b/R/convert_output.R @@ -2,7 +2,7 @@ #' #' Format stock assessment output file to a standardized tibble. #' -#' @param file Path. Assessment model output file path. Currently accepts SS3 +#' @param file Path. Assessment model output file path. Currently accepts SS3 #' Report.sso file, BAM .rda file, Rceattle model object, and FIMS model object. #' @param model String. Assessment model used in evaluation #' @@ -10,13 +10,13 @@ #' #' Options: "ss3", "bam", "fims", "rceattle" #' @param fleet_names Character vector. Names of fleets in the assessment model as -#' shortened in the output file or names of fleets to replace the native fleet -#' names in the output. For example, if fleet names in the model are 1, 2, 3 +#' shortened in the output file or names of fleets to replace the native fleet +#' names in the output. For example, if fleet names in the model are 1, 2, 3 #' then fleet_names = c("fleet1" = 1, "fleet2" = 2, "fleet3" = 3). #' #' Default: NULL -#' @param save_dir Path. Full file path to save the converted output file. If -#' saved file name is not indicated, the file will be saved as "std_output.rda" +#' @param save_dir Path. Full file path to save the converted output file. If +#' saved file name is not indicated, the file will be saved as "std_output.rda" #' in the file path provided. #' #' Default: NULL @@ -297,7 +297,7 @@ convert_output <- function( # Extract start year and end year start_year <- file$startyr end_year <- file$endyr - + param_names <- names(dat) std <- c( @@ -667,10 +667,10 @@ convert_output <- function( TRUE ~ label ), estimate = suppressWarnings(as.numeric(estimate)) - # dplyr::if_else( - # grepl("-|_", as.numeric(estimate)), - # NA, - # estimate + # dplyr::if_else( + # grepl("-|_", as.numeric(estimate)), + # NA, + # estimate # ) ) @@ -1168,8 +1168,8 @@ convert_output <- function( # df2[setdiff(tolower(names(out_new)), tolower(names(df2)))] <- NA # # Add to out list # out_list[[parm_sel]] <- df2 - # } else - if (parm_sel == "DEFINITIONS") { + # } else + if (parm_sel == "DEFINITIONS") { # Extract start year and end year for iding era start_year <- as.numeric(extract[which(apply(extract, 1, function(row) any(row == "Start_year:"))), 2]) end_year <- as.numeric(extract[which(apply(extract, 1, function(row) any(row == "End_year:"))), 2]) @@ -1430,7 +1430,7 @@ convert_output <- function( year < start_year ~ "init", year >= start_year & year <= end_year ~ "time", year > end_year ~ "fore", - TRUE ~ NA_character_ + TRUE ~ NA_character_ ) ) } else if (model %in% c("bam", "BAM")) { diff --git a/R/html_all_figs_tables.R b/R/html_all_figs_tables.R index df1f1a7c..1d0f13e5 100644 --- a/R/html_all_figs_tables.R +++ b/R/html_all_figs_tables.R @@ -28,14 +28,14 @@ #' cli::cli_alert_info("Did you enter the correct argument for figures_tables_dir?") #' cli::cli_alert_info("figures_tables_dir entered as {figures_tables_dir}") #' } -#' +#' #' if (!dir.exists(fs::path(figures_tables_dir, "tables"))) { #' cli::cli_alert_danger("'tables' folder not found.") #' cli::cli_alert_warning("Tables will not be included in the html.") #' cli::cli_alert_info("Did you enter the correct argument for figures_tables_dir?") #' cli::cli_alert_info("figures_tables_dir entered as {figures_tables_dir}") #' } -#' +#' #' # check if dir exists and present warning message/option message #' if (dir.exists(fs::path(figures_tables_dir, "all_tables_figures"))) { #' question1 <- readline( @@ -45,33 +45,33 @@ #' # indicate to proceed with function #' question1 <- "y" #' } -#' +#' #' if (regexpr(question1, "y", ignore.case = TRUE) == 1) { #' # create new folder for the html and qmd files #' doc_path <- fs::path(figures_tables_dir, "all_tables_figures") #' dir.create(doc_path) -#' +#' #' asar::create_tables_doc( #' subdir = figures_tables_dir, #' tables_dir = figures_tables_dir #' ) -#' +#' #' asar::create_figures_doc( #' subdir = figures_tables_dir, #' figures_dir = figures_tables_dir #' ) -#' +#' #' tab_doc_name <- list.files(figures_tables_dir)[grepl("_tables.qmd", list.files(figures_tables_dir))] #' tab_doc <- readLines(fs::path(figures_tables_dir, tab_doc_name)) -#' +#' #' fig_doc_name <- list.files(figures_tables_dir)[grepl("_figures.qmd", list.files(figures_tables_dir))] #' fig_doc <- readLines(fs::path(figures_tables_dir, fig_doc_name)) -#' +#' #' tabs_figs_text <- c( #' tab_doc, #' fig_doc #' ) -#' +#' #' yaml_text <- #' "--- #' title: 'All Tables & Figures' @@ -81,14 +81,14 @@ #' embed-resources: true #' --- #' " -#' +#' #' new_html_qmd <- c(yaml_text, tabs_figs_text) -#' +#' #' writeLines( #' new_html_qmd, #' fs::path(figures_tables_dir, "all_tables_figures.qmd") #' ) -#' +#' #' withr::with_dir( #' figures_tables_dir, #' quarto::quarto_render( @@ -96,7 +96,7 @@ #' output_file = fs::path("all_tables_figures.html") #' ) #' ) -#' +#' #' file.rename( #' from = fs::path(figures_tables_dir, "all_tables_figures.html"), #' to = fs::path( @@ -104,7 +104,7 @@ #' "all_tables_figures.html" #' ) #' ) -#' +#' #' file.rename( #' from = fs::path(figures_tables_dir, "all_tables_figures.qmd"), #' to = fs::path( @@ -112,7 +112,7 @@ #' "all_tables_figures.qmd" #' ) #' ) -#' +#' #' cli::cli_alert_success("Generated html and qmd with all tables and figures", wrap = TRUE) #' cli::cli_alert_info("Overwrote previous html and qmd") #' } else { diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 45be6d46..a985a51c 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -203,26 +203,38 @@ plot_biomass <- function( 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, - module = selected_module) - B.terminal.est <- calc_kqs(returned_kq = "B.terminal.est", - dat = dat, - relative = relative, - module = selected_module) - B.terminal.min <- calc_kqs(returned_kq = "B.terminal.min", - dat = dat, - relative = relative, - module = selected_module) - B.terminal.max <- calc_kqs(returned_kq = "B.terminal.max", - dat = dat, - relative = relative, - module = selected_module) + 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, + module = selected_module + ) + B.terminal.est <- calc_kqs( + returned_kq = "B.terminal.est", + dat = dat, + relative = relative, + module = selected_module + ) + B.terminal.min <- calc_kqs( + returned_kq = "B.terminal.min", + dat = dat, + relative = relative, + module = selected_module + ) + B.terminal.max <- calc_kqs( + returned_kq = "B.terminal.max", + dat = dat, + relative = relative, + module = selected_module + ) # SS3, FIMS if ("spawning_biomass_msy" %in% dat$label) { @@ -242,13 +254,19 @@ plot_biomass <- function( B.msy.max <- NA # BAM } else if ("bmsy" %in% dat$label) { - B.msy <- calc_kqs(returned_kq = "B.msy", - 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 <- 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 diff --git a/R/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index 6e9385ab..0ed6e39c 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -116,31 +116,48 @@ plot_fishing_mortality <- function( 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, module = selected_module ) - - F.target <- calc_kqs(returned_kq = "F.target", - dat = dat) - F.limit <- calc_kqs(returned_kq = "F.limit", - dat = dat) - F.terminal.est <- calc_kqs(returned_kq = "F.terminal.est", - dat = dat, - module = selected_module) - F.terminal.min <- calc_kqs(returned_kq = "F.terminal.min", - dat = dat, - module = selected_module) - F.terminal.max <- calc_kqs(returned_kq = "F.terminal.max", - dat = dat, - module = selected_module) - F.MSY.terminal <- calc_kqs(returned_kq = "F.MSY.terminal", - dat = dat) - F.MSY.terminal.max <- calc_kqs(returned_kq = "F.MSY.terminal.max", - dat = dat) - F.MSY.terminal.min <- calc_kqs(returned_kq = "F.MSY.terminal.min", - dat = dat) - + + F.target <- calc_kqs( + returned_kq = "F.target", + dat = dat + ) + F.limit <- calc_kqs( + returned_kq = "F.limit", + dat = dat + ) + F.terminal.est <- calc_kqs( + returned_kq = "F.terminal.est", + dat = dat, + module = selected_module + ) + F.terminal.min <- calc_kqs( + returned_kq = "F.terminal.min", + dat = dat, + module = selected_module + ) + F.terminal.max <- calc_kqs( + returned_kq = "F.terminal.max", + dat = dat, + module = selected_module + ) + F.MSY.terminal <- calc_kqs( + returned_kq = "F.MSY.terminal", + dat = dat + ) + F.MSY.terminal.max <- calc_kqs( + returned_kq = "F.MSY.terminal.max", + dat = dat + ) + F.MSY.terminal.min <- calc_kqs( + returned_kq = "F.MSY.terminal.min", + dat = dat + ) + export_kqs( F.ref.pt, F.start.year, diff --git a/R/plot_selectivity.R b/R/plot_selectivity.R index e663906c..3ed3b4ed 100644 --- a/R/plot_selectivity.R +++ b/R/plot_selectivity.R @@ -2,9 +2,9 @@ #' #' @inheritParams plot_spawning_biomass #' @param unit_label units for length-based selectivity. -#' +#' #' Default: "cm" -#' +#' #' @returns A plot showing selectivity by age or length. #' #' @details The input is from an assessment model output file @@ -40,8 +40,6 @@ plot_selectivity <- function( figures_dir = getwd(), ... ) { - - # Extract selectivity selectivity <- filter_data( dat = dat, @@ -53,14 +51,14 @@ plot_selectivity <- function( interactive = interactive, module = module ) - + # process data processed_data <- process_data( dat = selectivity, group = "year", facet = c(group, facet) ) - + if ("length_bins" %in% colnames(processed_data[[1]])) { prepared_data <- processed_data[[1]] |> dplyr::mutate(length_bins = as.numeric(length_bins)) @@ -70,44 +68,46 @@ plot_selectivity <- function( dplyr::mutate(age = as.numeric(age)) type <- "age" } - + # |> dplyr::mutate(group_var = NA) groups <- processed_data[[2]] facets <- processed_data[[3]] - - if (any(facets == "age" | facets == "length_bins" | facets == "year")){ + + if (any(facets == "age" | facets == "length_bins" | facets == "year")) { facets <- facets[!grepl("length_bins|age|year", facets)] } - - if (groups == "age" | groups == "length_bins" | groups == "year"){ + + if (groups == "age" | groups == "length_bins" | groups == "year") { groups <- facets[1] facets <- facets[-1] prepared_data <- prepared_data |> dplyr::mutate(group_var = .data[[groups]]) - if (groups == ""){ + if (groups == "") { groups <- NULL } } - + # Check if there is >1 label if (length(unique(prepared_data$label)) > 1) { prepared_data <- prepared_data |> # always select the first label if TRUE dplyr::filter(label == unique(label)[1]) } - + # Plot # TODO: left off here. Need to show fleets, models, other groupings final <- plot_timeseries( dat = prepared_data |> dplyr::filter(year == max(prepared_data$year, na.rm = TRUE)), x = ifelse(type == "length", - "length_bins", - "age"), + "length_bins", + "age" + ), y = "estimate", geom = "line", xlab = ifelse(type == "length", - glue::glue("Length ({unit_label})"), - "Age"), + glue::glue("Length ({unit_label})"), + "Age" + ), ylab = "Selectivity", group = groups, facet = facets, @@ -121,28 +121,32 @@ plot_selectivity <- function( if (make_rda) { # Obtain relevant key quantities for captions/alt text selectivity.type.cap <- ifelse(type == "length", - "Length", - "Age") - + "Length", + "Age" + ) + selectivity.type.low <- tolower(selectivity.type.cap) - + selectivity.x <- ifelse(type == "age", - "years", - unit_label) - + "years", + unit_label + ) + selectivity.start.year <- min(prepared_data$year) - + selectivity.end.year <- max(prepared_data$year) - + selectivity.x.min <- ifelse(type == "age", - min(prepared_data$age), - min(prepared_data$length_bins)) |> + min(prepared_data$age), + min(prepared_data$length_bins) + ) |> as.numeric() |> round(digits = 3) - + selectivity.x.max <- ifelse(type == "age", - max(prepared_data$age), - max(prepared_data$length_bins)) |> + max(prepared_data$age), + max(prepared_data$length_bins) + ) |> as.numeric() |> round(digits = 3) diff --git a/R/process_data.R b/R/process_data.R index 39aa3ecd..f873b82d 100644 --- a/R/process_data.R +++ b/R/process_data.R @@ -82,7 +82,7 @@ process_data <- function( # check for additional indexed variables index_variables <- check_grouping(dat) - + # Set group_var to identified grouping if (!is.null(group) && group != "none") { data <- dplyr::mutate( @@ -324,7 +324,7 @@ process_data <- function( group <- facet[1] facet <- facet[-1] } - + # Export list of objects list( # variable, @@ -382,23 +382,23 @@ process_table <- function( index_variables <- c(index_variables, mod_index) } - if (!is.null(group)){ + if (!is.null(group)) { id_group <- group } else { id_group <- index_variables[-grep("year|age|length_bin", index_variables)] } cols <- index_variables[grep("year|age|length_bin", index_variables)] - - if (!is.null(group)){ - if (any(is.na(dat[[group]]))){ + + if (!is.null(group)) { + if (any(is.na(dat[[group]]))) { dat <- dat |> dplyr::filter(!is.na(.data[[group]])) } - } else if (!is.null(id_group)){ - if (length(id_group) > 1){ + } else if (!is.null(id_group)) { + if (length(id_group) > 1) { cli::cli_alert_warning("Data contains >1 indexing variable. Selecting {id_group[1]}.") } - if (length(id_group) > 0 && any(is.na(dat[[id_group]]))){ + if (length(id_group) > 0 && any(is.na(dat[[id_group]]))) { dat <- dat |> dplyr::filter(!is.na(.data[[id_group]])) } @@ -504,12 +504,12 @@ process_table <- function( mod_dat <- dplyr::filter(dat, model == mod) mod_index_variables <- check_grouping(mod_dat) mod_id_group <- mod_index_variables[-grep("year|age|length_bin", mod_index_variables)] - + # overriding mod_id_group when user inputs `group` argument - if (!is.null(group) && (group %in% mod_id_group)){ + if (!is.null(group) && (group %in% mod_id_group)) { mod_id_group <- group } - + mod_cols <- mod_index_variables[grep("year|age|length_bin", mod_index_variables)] mod_uncert_lab <- unique(mod_dat$uncertainty_label) if (length(mod_uncert_lab) == 1 && is.na(mod_uncert_lab)) { diff --git a/R/save_all_plots.R b/R/save_all_plots.R index 75c19584..94a12303 100644 --- a/R/save_all_plots.R +++ b/R/save_all_plots.R @@ -100,15 +100,15 @@ #' @param landings_unit_label String. Landings units #' #' Default: "mt" -#' +#' #' @param projections_unit_label Units for projections quantities #' -#' Default: c("catch" = "mt", "spawning_biomass" = "mt", +#' Default: c("catch" = "mt", "spawning_biomass" = "mt", #' "fishing_mortality" = "") -#' +#' #' @param projections_uncert_label Abbreviated uncertainty label for #' each quantity -#' +#' #' Default: c("catch" = "stddev", "spawning_biomass" = "stddev", #' "fishing_mortality" = "stddev") #' @@ -450,16 +450,16 @@ save_all_plots <- function( print(e) } ) - - + + tryCatch( { cli::cli_h2("plot_selectivity") plot_selectivity(dat, - # unit_label = selectivity_unit_label, # commenting this out because if interactive = FALSE, unit_label is not used as age is selected (over length), which doesn't require a unit label - make_rda = TRUE, - interactive = FALSE, - figures_dir = figures_tables_dir + # unit_label = selectivity_unit_label, # commenting this out because if interactive = FALSE, unit_label is not used as age is selected (over length), which doesn't require a unit label + make_rda = TRUE, + interactive = FALSE, + figures_dir = figures_tables_dir ) # |> suppressWarnings() |> invisible() }, error = function(e) { @@ -531,16 +531,16 @@ save_all_plots <- function( print(e) } ) - + tryCatch( { cli::cli_h2("table_projections") table_projections(dat, - unit_label = projections_unit_label, - # uncertainty_label = projections_uncert_label, - interactive = FALSE, - make_rda = TRUE, - tables_dir = figures_tables_dir + unit_label = projections_unit_label, + # uncertainty_label = projections_uncert_label, + interactive = FALSE, + make_rda = TRUE, + tables_dir = figures_tables_dir ) # |> # suppressWarnings() |> # invisible() diff --git a/R/table_projections.R b/R/table_projections.R index 56b8cbc9..a3816d8c 100644 --- a/R/table_projections.R +++ b/R/table_projections.R @@ -3,31 +3,32 @@ #' Generates a table showing assumed and projected years of Catch, SB, and F. #' #' @inheritParams table_landings -#' +#' #' @param unit_label Character vector. Abbreviated unit label for each quantity -#' -#' Default: c("catch" = "mt", "spawning_biomass" = "mt", +#' +#' Default: c("catch" = "mt", "spawning_biomass" = "mt", #' "fishing_mortality" = "") -#' +#' #' @return A formatted gt table object. #' @details We would like to thank Dan Hennen for sharing his projections table #' function code, which served as the foundation for this function. -#' +#' #' @export #' @examples #' table_projections( -#' dat = stockplotr::example_data, -#' interactive = FALSE, -#' module = "DERIVED_QUANTITIES") +#' dat = stockplotr::example_data, +#' interactive = FALSE, +#' module = "DERIVED_QUANTITIES" +#' ) #' table_projections <- function( - dat, - unit_label = c("catch" = "mt", "spawning_biomass" = "mt", "fishing_mortality" = ""), - interactive = TRUE, - module = NULL, - make_rda = FALSE, - tables_dir = getwd() - ) { + dat, + unit_label = c("catch" = "mt", "spawning_biomass" = "mt", "fishing_mortality" = ""), + interactive = TRUE, + module = NULL, + make_rda = FALSE, + tables_dir = getwd() +) { # check if catch in data else landings catch_lab <- ifelse( any(grepl("catch$", dat$label)), @@ -83,12 +84,12 @@ table_projections <- function( )[[1]] } ) - + combine_data <- purrr::reduce(purrr::compact(lab_list), dplyr::full_join, by = c("Year")) |> gt::gt() - + final_table <- theme_table(combine_data) - + # export table to rda if argument = T if (make_rda == TRUE) { # No key quantities for captions/alt text since only values @@ -104,22 +105,22 @@ table_projections <- function( x = caps_alttext, file = fs::path(getwd(), "captions_alt_text.csv"), row.names = FALSE - ) - } - - create_rda( - object = final_table, - # get name of function and remove "table_" from it - topic_label = "projections", - fig_or_table = "table", - dat = dat, - dir = tables_dir, - scale_amount = 1, - unit_label = unit_label, - table_df = final_table$`_data` ) } + create_rda( + object = final_table, + # get name of function and remove "table_" from it + topic_label = "projections", + fig_or_table = "table", + dat = dat, + dir = tables_dir, + scale_amount = 1, + unit_label = unit_label, + table_df = final_table$`_data` + ) + } + # Send table(s) to viewer return(final_table) -} \ No newline at end of file +} diff --git a/R/utils_plot.R b/R/utils_plot.R index 6bd9fa36..a7ddac99 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -792,7 +792,6 @@ filter_data <- function( unique(plot_data$module_name), question1 ) - } else { # use <<- to export module to environment for use with key quantity calc selected_module <<- unique(plot_data$module_name)[1] diff --git a/R/utils_rda.R b/R/utils_rda.R index c0fda232..14199f5c 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -9,7 +9,7 @@ #' @param dat Data frame. The original data object. #' @param final ggplot2 object. The final figure. #' @param relative Logical. TRUE/FALSE; specify whether to set y-axis values relative to the ref_line value. -#' @param module Character vector. Module name found in `dat$module_name`. +#' @param module Character vector. Module name found in `dat$module_name`. #' @param ... Additional arguments to be passed to the function. #' #' @return The value of the specified key quantity. @@ -26,8 +26,7 @@ calc_kqs <- function(returned_kq, final = NULL, relative = NULL, module = NULL, - ...){ - + ...) { # plot_stock_recruitment()----- if (returned_kq == "sr.age.min") { return( @@ -42,11 +41,11 @@ calc_kqs <- function(returned_kq, # plot_spawning_biomass()----- if (returned_kq %in% c("rel.ssb.min", "rel.ssb.max")) { rel.ssb <- final[["layers"]][[2]][["data"]] |> - as.data.frame() |> - dplyr::pull(estimate) + as.data.frame() |> + dplyr::pull(estimate) rel.ssb.min <- rel.ssb |> - min() |> - round(digits = 3) + min() |> + round(digits = 3) rel.ssb.max <- rel.ssb |> max() |> round(digits = 3) @@ -55,12 +54,12 @@ calc_kqs <- function(returned_kq, # plot_biomass()----- if (returned_kq %in% c("rel.B.min", "rel.B.max")) { - rel.B <- final[["layers"]][[2]][["data"]] |> - as.data.frame() |> - dplyr::pull(estimate) + rel.B <- final[["layers"]][[2]][["data"]] |> + as.data.frame() |> + dplyr::pull(estimate) rel.B.min <- rel.B |> - min() |> - round(digits = 3) + min() |> + round(digits = 3) rel.B.max <- rel.B |> max() |> round(digits = 3) @@ -82,7 +81,7 @@ calc_kqs <- function(returned_kq, if (returned_kq == "B.end.year") { return(max(prepared_data$year) |> round(digits = 3)) } - + if (returned_kq %in% c("B.terminal.min", "B.terminal.max", "B.terminal.year", "B.terminal.est")) { B.terminal.df <- filter_data( dat = dat, @@ -94,44 +93,44 @@ calc_kqs <- function(returned_kq, module = selected_module, scale_amount = 1, interactive = FALSE - ) |> + ) |> dplyr::filter(year == max(year)) - + B.terminal.year <- B.terminal.df |> dplyr::pull(year) |> - unique() - + unique() + B.terminal.est <- B.terminal.df |> dplyr::pull(estimate) |> unique() |> round(digits = 3) - - if (length(B.terminal.est) > 1){ + + if (length(B.terminal.est) > 1) { B.terminal.est <- B.terminal.df |> dplyr::filter(is.na(age)) |> dplyr::pull(estimate) |> unique() |> round(digits = 3) - - if (length(B.terminal.est) > 0){ + + if (length(B.terminal.est) > 0) { B.terminal.est <- NA B.terminal.uncert <- NA } else { - B.terminal.uncert <- B.terminal.df |> - dplyr::filter(is.na(age)) |> - dplyr::select(uncertainty) |> - as.numeric() + B.terminal.uncert <- B.terminal.df |> + dplyr::filter(is.na(age)) |> + dplyr::select(uncertainty) |> + as.numeric() } - if (length(B.terminal.est) == 0){ + if (length(B.terminal.est) == 0) { B.terminal.est <- NA } } else { B.terminal.uncert <- B.terminal.df |> - dplyr::select(uncertainty) |> - as.numeric() - } - - if (is.na(B.terminal.uncert)){ + dplyr::select(uncertainty) |> + as.numeric() + } + + if (is.na(B.terminal.uncert)) { B.terminal.uncert <- NA B.terminal.min <- NA B.terminal.max <- NA @@ -139,17 +138,18 @@ calc_kqs <- function(returned_kq, B.terminal.min <- round((B.terminal.est - B.terminal.uncert), digits = 3) B.terminal.max <- round((B.terminal.est + B.terminal.uncert), digits = 3) } - + if (returned_kq == "B.terminal.min") { return(B.terminal.min) - } else if (returned_kq == "B.terminal.max"){ - return(B.terminal.max) } else if (returned_kq == "B.terminal.est"){ - return (B.terminal.est) - } else if (returned_kq == "B.terminal.year") { - return(B.terminal.year) - } + } else if (returned_kq == "B.terminal.max") { + return(B.terminal.max) + } else if (returned_kq == "B.terminal.est") { + return(B.terminal.est) + } else if (returned_kq == "B.terminal.year") { + return(B.terminal.year) + } } - + if (returned_kq == "sb_msy") { return( dat |> @@ -192,15 +192,15 @@ calc_kqs <- function(returned_kq, ) } } - + if (returned_kq %in% c("B.msy.min", "B.msy.max")) { - B.msy <- dat |> - dplyr::filter(label == "bmsy") |> - dplyr::select(estimate) |> + B.msy <- dat |> + dplyr::filter(label == "bmsy") |> + dplyr::select(estimate) |> as.numeric() - B.msy_uncert <- dat |> - dplyr::filter(label == "bmsy") |> - dplyr::select(uncertainty) |> + B.msy_uncert <- dat |> + dplyr::filter(label == "bmsy") |> + dplyr::select(uncertainty) |> as.numeric() B.msy.min <- round((B.msy - B.msy_uncert), digits = 3) B.msy.max <- round((B.msy + B.msy_uncert), digits = 3) @@ -210,23 +210,22 @@ calc_kqs <- function(returned_kq, # plot_fishing_mortality()----- if (returned_kq %in% c("F.max", "F.min")) { F.df <- prepared_data$estimate |> - na.omit() - F.max <- F.df |> - max() |> - round(digits = 3) - F.min <- F.df |> - min() |> - round(digits = 3) + na.omit() + F.max <- F.df |> + max() |> + round(digits = 3) + F.min <- F.df |> + min() |> + round(digits = 3) return(ifelse(returned_kq == "F.max", F.max, F.min)) } if (returned_kq == "F.target") { - F.target <- dat |> - dplyr::filter(grepl('f_target', label) | grepl('f_msy', label) | (grepl('fishing_mortality_msy', label) & is.na(year))) |> - dplyr::pull(estimate) |> - round(digits = 3) - if (length(F.target > 1)){ + dplyr::filter(grepl("f_target", label) | grepl("f_msy", label) | (grepl("fishing_mortality_msy", label) & is.na(year))) |> + dplyr::pull(estimate) |> + round(digits = 3) + if (length(F.target > 1)) { F.target <- NA } return(F.target) @@ -245,7 +244,7 @@ calc_kqs <- function(returned_kq, return(NA) } } - + if (returned_kq %in% c("F.terminal.min", "F.terminal.max", "F.terminal.year", "F.terminal.est")) { F.terminal.df <- filter_data( dat = dat, @@ -260,11 +259,11 @@ calc_kqs <- function(returned_kq, ) |> dplyr::filter(year == max(year)) |> unique() - + F.terminal.year <- F.terminal.df |> dplyr::pull(year) |> unique() - + if (length(unique(F.terminal.df$fleet)) > 1) { F.terminal.est <- NA F.terminal.uncert <- NA @@ -272,55 +271,56 @@ calc_kqs <- function(returned_kq, F.terminal.max <- NA } else { F.terminal.est <- F.terminal.df |> - dplyr::pull(estimate) |> - unique() |> + dplyr::pull(estimate) |> + unique() |> as.numeric() |> round(digits = 3) F.terminal.uncert <- F.terminal.df |> - dplyr::select(uncertainty) |> + dplyr::select(uncertainty) |> as.numeric() - + F.terminal.min <- round((F.terminal.est - F.terminal.uncert), digits = 3) F.terminal.max <- round((F.terminal.est + F.terminal.uncert), digits = 3) F.terminal.est <- round(F.terminal.est, digits = 3) } - + if (returned_kq == "F.terminal.min") { return(F.terminal.min) - } else if (returned_kq == "F.terminal.max"){ - return(F.terminal.max) } else if (returned_kq == "F.terminal.est"){ - return (F.terminal.est) - } else if (returned_kq == "F.terminal.year") { - return(F.terminal.year) - } + } else if (returned_kq == "F.terminal.max") { + return(F.terminal.max) + } else if (returned_kq == "F.terminal.est") { + return(F.terminal.est) + } else if (returned_kq == "F.terminal.year") { + return(F.terminal.year) + } } - + if (returned_kq %in% c("F.MSY.terminal", "F.MSY.terminal.min", "F.MSY.terminal.max")) { if (!"f_msy" %in% unique(dat$label)) { F.msy.est <- NA F.msy.terminal.min <- NA F.msy.terminal.max <- NA } else { - F.msy.terminal.df <- dat |> - dplyr::filter(label == "f_msy") - - F.msy.est <- F.msy.terminal.df |> - dplyr::pull(estimate) |> - as.numeric() - - F.msy.uncert <- F.msy.terminal.df |> - dplyr::pull(uncertainty) |> - as.numeric() - - F.msy.terminal.min <- round((F.msy.est - F.msy.uncert), digits = 3) - F.msy.terminal.max <- round((F.msy.est + F.msy.uncert), digits = 3) - F.msy.est <- round(F.msy.est, digits = 3) + F.msy.terminal.df <- dat |> + dplyr::filter(label == "f_msy") + + F.msy.est <- F.msy.terminal.df |> + dplyr::pull(estimate) |> + as.numeric() + + F.msy.uncert <- F.msy.terminal.df |> + dplyr::pull(uncertainty) |> + as.numeric() + + F.msy.terminal.min <- round((F.msy.est - F.msy.uncert), digits = 3) + F.msy.terminal.max <- round((F.msy.est + F.msy.uncert), digits = 3) + F.msy.est <- round(F.msy.est, digits = 3) } if (returned_kq == "F.MSY.terminal") { return(F.msy.est) - } else if (returned_kq == "F.MSY.terminal.min"){ + } else if (returned_kq == "F.MSY.terminal.min") { return(F.msy.terminal.min) - } else if (returned_kq == "F.MSY.terminal.max"){ + } else if (returned_kq == "F.MSY.terminal.max") { return(F.msy.terminal.max) } } diff --git a/R/utils_table.R b/R/utils_table.R index f255203c..af5855cf 100644 --- a/R/utils_table.R +++ b/R/utils_table.R @@ -214,7 +214,7 @@ merge_error <- function( # stringr::str_escape(unlist(id_col_vals, use.names = FALSE)), # collapse = "|") # ) - + # Identify which uncert col aligns with l_col uncert_col <- uncert_cols[grep(l_col, uncert_cols)] @@ -225,7 +225,7 @@ merge_error <- function( !is.na(.data[[uncert_col]]), paste0(.data[[l_col]], " (", .data[[uncert_col]], ")"), .data[[l_col]] - ) + ) ) |> # Remove uncertainty colummn id'd in this step of the loop dplyr::select(-dplyr::all_of(uncert_col)) @@ -235,13 +235,13 @@ merge_error <- function( header_labs <- stringr::str_replace_all(colnames(tab_dat), "_", " ") |> stringr::str_to_title() header_labs2 <- glue::glue("{header_labs[-1]}{ifelse(unit_label!='', paste0(' (', unit_label,') '), ' ')}{ifelse(uncert_lab!='Uncertainty', paste0(' (',uncert_lab, ')'),'')}") - header_labs2 <- header_labs2 |> + header_labs2 <- header_labs2 |> # remove double parentheses if present - stringr::str_replace_all("\\(\\(", "(") |> + stringr::str_replace_all("\\(\\(", "(") |> stringr::str_replace_all("\\)\\)", ")") |> stringr::str_replace_all("\\( \\(", "(") |> stringr::str_replace_all("\\) \\)", ")") - + colnames(tab_dat) <- c(header_labs[1], header_labs2) return(tab_dat) diff --git a/man/convert_output.Rd b/man/convert_output.Rd index 68e66c85..613447fe 100644 --- a/man/convert_output.Rd +++ b/man/convert_output.Rd @@ -7,7 +7,7 @@ convert_output(file, model = NULL, fleet_names = NULL, save_dir = NULL) } \arguments{ -\item{file}{Path. Assessment model output file path. Currently accepts SS3 +\item{file}{Path. Assessment model output file path. Currently accepts SS3 Report.sso file, BAM .rda file, Rceattle model object, and FIMS model object.} \item{model}{String. Assessment model used in evaluation @@ -17,14 +17,14 @@ Default: NULL Options: "ss3", "bam", "fims", "rceattle"} \item{fleet_names}{Character vector. Names of fleets in the assessment model as - shortened in the output file or names of fleets to replace the native fleet - names in the output. For example, if fleet names in the model are 1, 2, 3 + shortened in the output file or names of fleets to replace the native fleet + names in the output. For example, if fleet names in the model are 1, 2, 3 then fleet_names = c("fleet1" = 1, "fleet2" = 2, "fleet3" = 3). Default: NULL} -\item{save_dir}{Path. Full file path to save the converted output file. If -saved file name is not indicated, the file will be saved as "std_output.rda" +\item{save_dir}{Path. Full file path to save the converted output file. If +saved file name is not indicated, the file will be saved as "std_output.rda" in the file path provided. Default: NULL} diff --git a/man/save_all_plots.Rd b/man/save_all_plots.Rd index b8532563..dcc708cb 100644 --- a/man/save_all_plots.Rd +++ b/man/save_all_plots.Rd @@ -142,7 +142,7 @@ Default: 1} \item{projections_unit_label}{Units for projections quantities -Default: c("catch" = "mt", "spawning_biomass" = "mt", +Default: c("catch" = "mt", "spawning_biomass" = "mt", "fishing_mortality" = "")} \item{projections_uncert_label}{Abbreviated uncertainty label for diff --git a/man/table_projections.Rd b/man/table_projections.Rd index 0e0e8d15..6ec1930f 100644 --- a/man/table_projections.Rd +++ b/man/table_projections.Rd @@ -22,7 +22,7 @@ in `ref_line` is used to plot a reference line or calculate relative spawning bi \item{unit_label}{Character vector. Abbreviated unit label for each quantity -Default: c("catch" = "mt", "spawning_biomass" = "mt", +Default: c("catch" = "mt", "spawning_biomass" = "mt", "fishing_mortality" = "")} \item{interactive}{Logical. TRUE/FALSE; indicate whether the environment is interactive. @@ -60,8 +60,9 @@ function code, which served as the foundation for this function. } \examples{ table_projections( - dat = stockplotr::example_data, - interactive = FALSE, - module = "DERIVED_QUANTITIES") + dat = stockplotr::example_data, + interactive = FALSE, + module = "DERIVED_QUANTITIES" +) } diff --git a/tests/testthat/test-plot_biomass_at_age.R b/tests/testthat/test-plot_biomass_at_age.R index 403bbfa2..8639736c 100644 --- a/tests/testthat/test-plot_biomass_at_age.R +++ b/tests/testthat/test-plot_biomass_at_age.R @@ -2,7 +2,8 @@ test_that("plot_biomass_at_age generates plots without errors", { # expect error-free plot with minimal arguments expect_no_error( plot_biomass_at_age(stockplotr::example_data, - module = "BIOMASS_AT_AGE") + module = "BIOMASS_AT_AGE" + ) ) # expect error-free plot with many arguments diff --git a/tests/testthat/test-plot_catch_comp.R b/tests/testthat/test-plot_catch_comp.R index 11927acf..d5edb287 100644 --- a/tests/testthat/test-plot_catch_comp.R +++ b/tests/testthat/test-plot_catch_comp.R @@ -2,8 +2,9 @@ test_that("plot_catch_comp generates plots without errors", { # expect error-free plot with minimal arguments expect_no_error( - plot_catch_comp(stockplotr::example_data, - module = "CATCH_AT_AGE") + plot_catch_comp(stockplotr::example_data, + module = "CATCH_AT_AGE" + ) ) # expect error-free plot with many arguments diff --git a/tests/testthat/test-plot_selectivity.R b/tests/testthat/test-plot_selectivity.R index ca25016d..1221b0c1 100644 --- a/tests/testthat/test-plot_selectivity.R +++ b/tests/testthat/test-plot_selectivity.R @@ -20,7 +20,7 @@ test_that("plot_selectivity generates plots without errors", { figures_dir = getwd() ) ) - + expect_no_error( plot_selectivity( stockplotr::example_data |> diff --git a/tests/testthat/test-table_index.R b/tests/testthat/test-table_index.R index 43869f99..ea30a2ea 100644 --- a/tests/testthat/test-table_index.R +++ b/tests/testthat/test-table_index.R @@ -40,11 +40,11 @@ test_that("rda file made when indicated", { make_rda = TRUE, tables_dir = getwd() ) - + # expect that both tables dir and the index_table.rda file exist expect_true(dir.exists(fs::path(getwd(), "tables"))) expect_true(file.exists(fs::path(getwd(), "tables", "index_table.rda"))) - + # erase temporary testing files file.remove(fs::path(getwd(), "captions_alt_text.csv")) unlink(fs::path(getwd(), "tables"), recursive = T) @@ -58,7 +58,7 @@ test_that("rda file made when indicated", { make_rda = TRUE, tables_dir = getwd() ) - + # load the rda file and check that it contains the expected object load(fs::path(getwd(), "tables", "index_table.rda")) # expect rda contains three objects: table, caption, and latex table @@ -71,9 +71,9 @@ test_that("rda file made when indicated", { expect_false( is.null(rda$latex_table) ) - + expect_true(rda$caption == "Calculated index of abundance and corresponding CVs for the fleets and surveys identified in the column headers. ") - + # erase temporary testing files file.remove(fs::path(getwd(), "captions_alt_text.csv")) unlink(fs::path(getwd(), "tables"), recursive = T) diff --git a/tests/testthat/test-table_landings.R b/tests/testthat/test-table_landings.R index a177a4d6..1d3abe6b 100644 --- a/tests/testthat/test-table_landings.R +++ b/tests/testthat/test-table_landings.R @@ -83,11 +83,11 @@ test_that("rda file made when indicated", { make_rda = TRUE, tables_dir = getwd() ) - + # expect that both tables dir and the landings_table.rda file exist expect_true(dir.exists(fs::path(getwd(), "tables"))) expect_true(file.exists(fs::path(getwd(), "tables", "landings_table.rda"))) - + # erase temporary testing files file.remove(fs::path(getwd(), "captions_alt_text.csv")) file.remove(fs::path(getwd(), "key_quantities.csv")) @@ -102,7 +102,7 @@ test_that("rda file made when indicated", { make_rda = TRUE, tables_dir = getwd() ) - + # load the rda file and check that it contains the expected object load(fs::path(getwd(), "tables", "landings_table.rda")) # expect rda contains three objects: table, caption, and latex table @@ -115,12 +115,11 @@ test_that("rda file made when indicated", { expect_false( is.null(rda$latex_table) ) - + expect_true(rda$caption == "Landed catch by fleet and year in (mt).") - + # erase temporary testing files file.remove(fs::path(getwd(), "captions_alt_text.csv")) file.remove(fs::path(getwd(), "key_quantities.csv")) unlink(fs::path(getwd(), "tables"), recursive = T) }) - diff --git a/tests/testthat/test-table_projections.R b/tests/testthat/test-table_projections.R index 4cd1f139..77d631d2 100644 --- a/tests/testthat/test-table_projections.R +++ b/tests/testthat/test-table_projections.R @@ -16,7 +16,6 @@ test_that("table_projections generates plots without errors", { tables_dir = getwd() ) ) - }) test_that("rda file made when indicated", {