diff --git a/DESCRIPTION b/DESCRIPTION index fe447551..56d73a6e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: stockplotr Title: Tables and Figures for Stock Assessments -Version: 0.7.0 +Version: 0.9.0 Authors@R: c( person("Samantha", "Schiano", , "samantha.schiano@noaa.gov", role = c("aut", "cre"), comment = c(ORCID = "0009-0003-3744-6428")), @@ -48,6 +48,7 @@ Imports: prodlim, quarto, scales, + stats, stringr, tibble, tidyr, diff --git a/R/convert_output.R b/R/convert_output.R index 62ff6652..06304483 100644 --- a/R/convert_output.R +++ b/R/convert_output.R @@ -4,7 +4,7 @@ #' #' @param file Assessment model output file path #' @param model Assessment model used in evaluation ("ss3", "bam", -#' "asap", "fims", "amak", "ms-java", "wham", "mas"). +#' "fims"). #' @param fleet_names Names of fleets in the assessment model as #' shortened in the output file. If fleet names are not properly read, then #' indicate the fleets names as an acronym in a vector @@ -1745,8 +1745,9 @@ convert_output <- function( #### JABBA #### } else if (tolower(model) == "jabba") { cli::cli_abort("JABBA output not currently compatible.") + #### FIMS #### } else if (model == "fims") { - if (grepl(".RDS", file)) { + if (is.character(file) && length(file) == 1) { fims_output <- readRDS(file) } else { fims_output <- file @@ -1761,7 +1762,7 @@ convert_output <- function( cli::cli_abort(c( message = "Output file not compatible.", "i" = "`model` entered as {model}.", - "i" = "Accepted `model` options: SS3, BAM, WHAM, AMAK, JABBA." + "i" = "Accepted `model` options: ss3, bam, or fims." )) } diff --git a/R/plot_natural_mortality.R b/R/plot_natural_mortality.R index 303f665e..639f88e9 100644 --- a/R/plot_natural_mortality.R +++ b/R/plot_natural_mortality.R @@ -64,7 +64,12 @@ plot_natural_mortality <- function( facet = c(group, facet) ) - processed_data <- processing[[1]] + processed_data <- processing[[1]] + if (is.null(group)) { + processed_data <- processed_data |> + dplyr::mutate(group_var = "1") # set group var without anything since we are using group as age + } + if (!is.null(processing[[3]])) facet <- processing[[3]] # make sure group is the one input by user if (!is.null(group)) { diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 878610e8..4fd29179 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -4,7 +4,7 @@ #' also be made relative to this reference line rather than in absolute units. #' #' @param dat A data frame or names list of data frames (input as `list()`) -#' returned from \link[asar]{convert_output}. The first data frame in the list +#' returned from \link[stockplotr]{convert_output}. The first data frame in the list #' is used in calculation of a reference line if one is present #' @param geom A string stating the geom used for the plot. Default is "line". #' Options include "line", "point", or "area" @@ -117,7 +117,7 @@ plot_spawning_biomass <- function( # Filter data for spawning biomass prepared_data <- filter_data( dat = dat, - label_name = "spawning_biomass$", + label_name = "^spawning_biomass$", geom = geom, era = era, group = group, @@ -166,7 +166,8 @@ plot_spawning_biomass <- function( geom = geom, ylab = spawning_biomass_label, group = group, - facet = facet, + # add check in case facet is returned as character(0) + facet = if (length(facet) > 0) facet else NULL, ... ) # Add reference line diff --git a/R/process_data.R b/R/process_data.R index 77193415..a4083702 100644 --- a/R/process_data.R +++ b/R/process_data.R @@ -241,21 +241,27 @@ process_data <- function( # check if value varies in ANY year # pivot data for 1st indexed data and check if all the same if (length(index_variables) > 0) { - pivot_data <- data |> - dplyr::select(tidyselect::any_of(c("year", "age", "estimate", index_variables))) |> - tidyr::pivot_wider( - id_cols = c(year), - names_from = tidyselect::any_of(index_variables), - values_from = estimate - ) |> - suppressWarnings() - column_data <- pivot_data[-1] + # if (index_variables == "age" | index_variables == "year") { + # # if age or year is the only other index variable, then get data so there is a single column as expected in column_data + # pivot_data <- data + # column_data <- pivot_data[["estimate"]] + # } else { + pivot_data <- data |> + dplyr::select(tidyselect::any_of(c("year", "age", "estimate", index_variables))) |> + tidyr::pivot_wider( + id_cols = c(year), + names_from = tidyselect::any_of(index_variables), + values_from = estimate + ) |> + suppressWarnings() + column_data <- pivot_data[-1] + # } } else { # Look at group_var and identify if there is a needed grouping or can just plot one year pivot_data <- data |> dplyr::select(tidyselect::any_of(c("year", "age", "estimate", "group_var"))) |> tidyr::pivot_wider( - id_cols = c(year), + id_cols = if ("year" %in% colnames(data)) "year" else NULL, names_from = group_var, values_from = estimate ) |> @@ -352,7 +358,7 @@ process_table <- function( mod_data <- dplyr::filter(dat, model == mod) mod_index <- check_grouping(mod_data) mod_names <- rep(mod, length(mod_index)) - mod_index <- setNames(mod_index, mod_names) + mod_index <- stats::setNames(mod_index, mod_names) index_variables <- c(index_variables, mod_index) } @@ -409,7 +415,7 @@ process_table <- function( mod_data <- dplyr::filter(dat, model == mod) mod_index <- check_grouping(mod_data) mod_names <- rep(mod, length(mod_index)) - mod_index <- setNames(mod_index, mod_names) + mod_index <- stats::setNames(mod_index, mod_names) index_variables <- c(index_variables, mod_index) } @@ -495,7 +501,7 @@ process_table <- function( table_list[[mod]] <- table_data # This feels like backward progress - id_group_list[[mod]] <- lapply(setNames(mod_id_group, mod_id_group), function(x) { + id_group_list[[mod]] <- lapply(stats::setNames(mod_id_group, mod_id_group), function(x) { unique(mod_dat[[x]]) }) } # close loop diff --git a/R/stockplotr-package.R b/R/stockplotr-package.R index 05d7a77a..228139aa 100644 --- a/R/stockplotr-package.R +++ b/R/stockplotr-package.R @@ -14,6 +14,9 @@ globvar <- c( "type", "uncertainty", "uncertainty_label", "val", "where", "year", ":=", "Year", "avg", "estimate_orig", "interm", "season", "tables_dir", "time", "total_fish", "years_per_year", ".data", "cohort", "era", "expected_recruitment", "group_var", "model", - "plot_data", "quantile", "rda_dir", "reorder", "total_estimate", "zvar", "filter_data" + "plot_data", "quantile", "rda_dir", "reorder", "total_estimate", "zvar", "filter_data", + "Component", "age_bins", "alt_label", "final_df", "init", "keyword", "len_bins", + "like", "match_key", "morph", "nsim", "output", "output_order", "parm_stdev", "seas", + "sexes", "subseas", "unique_count", "value", "yr" ) if (getRversion() >= "2.15.1") utils::globalVariables(globvar) diff --git a/R/utils_plot.R b/R/utils_plot.R index 921df943..cd630351 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -183,7 +183,7 @@ plot_timeseries <- function( } # Check if facet(s) are desired - if (!is.null(facet)) { + if (!is.null(facet) & length(facet) > 0) { facet <- paste("~", paste(facet, collapse = " + ")) facet_formula <- stats::reformulate(facet) diff --git a/R/utils_table.R b/R/utils_table.R index 761fe158..bf210572 100644 --- a/R/utils_table.R +++ b/R/utils_table.R @@ -305,7 +305,7 @@ merge_error <- function(table_data, uncert_lab, fleets, label, unit_label) { # Rename final df with cleaned names tab_dat <- tab_dat |> - dplyr::rename(any_of(rename_map_final)) |> + dplyr::rename(dplyr::any_of(rename_map_final)) |> dplyr::rename_with(~ gsub("_", " - ", .)) # |> # not sure if we want to keep this or not # dplyr::select(where(~!all(is.na(.)) | !all(. == "-"))) # remove columns that are all NA or all "-")) diff --git a/data-raw/DATASET.R b/data-raw/DATASET.R index b6dd8396..dcdc7dbd 100644 --- a/data-raw/DATASET.R +++ b/data-raw/DATASET.R @@ -2,8 +2,8 @@ # Path to SS3 output file EX_REPORT_PATH <- file.path("inst", "extdata", "Report.sso") # Install asar -devtools::install_github("nmfs-ost/asar") +devtools::install_github("nmfs-ost/stockplotr") # Convert output -example_data <- asar::convert_output(EX_REPORT_PATH) +example_data <- stockplotr::convert_output(EX_REPORT_PATH) # Save object as rda into raw_data usethis::use_data(example_data, overwrite = TRUE) diff --git a/man/convert_output.Rd b/man/convert_output.Rd index 670189a4..1636cd3e 100644 --- a/man/convert_output.Rd +++ b/man/convert_output.Rd @@ -10,7 +10,7 @@ convert_output(file, model = NULL, fleet_names = NULL, save_dir = NULL) \item{file}{Assessment model output file path} \item{model}{Assessment model used in evaluation ("ss3", "bam", -"asap", "fims", "amak", "ms-java", "wham", "mas").} +"fims").} \item{fleet_names}{Names of fleets in the assessment model as shortened in the output file. If fleet names are not properly read, then diff --git a/man/plot_biomass.Rd b/man/plot_biomass.Rd index 77d16e7c..7a4033c6 100644 --- a/man/plot_biomass.Rd +++ b/man/plot_biomass.Rd @@ -22,7 +22,7 @@ plot_biomass( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{geom}{A string stating the geom used for the plot. Default is "line". diff --git a/man/plot_fishing_mortality.Rd b/man/plot_fishing_mortality.Rd index 43596c20..8a181be7 100644 --- a/man/plot_fishing_mortality.Rd +++ b/man/plot_fishing_mortality.Rd @@ -21,7 +21,7 @@ plot_fishing_mortality( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{geom}{A string stating the geom used for the plot. Default is "line". diff --git a/man/plot_indices.Rd b/man/plot_indices.Rd index 8500b188..1ef91088 100644 --- a/man/plot_indices.Rd +++ b/man/plot_indices.Rd @@ -19,7 +19,7 @@ plot_indices( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{unit_label}{units for index of abundance/CPUE} diff --git a/man/plot_landings.Rd b/man/plot_landings.Rd index 0a10f011..e2581993 100644 --- a/man/plot_landings.Rd +++ b/man/plot_landings.Rd @@ -21,7 +21,7 @@ plot_landings( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{unit_label}{units for spawning_biomass} diff --git a/man/plot_natural_mortality.Rd b/man/plot_natural_mortality.Rd index 48e3fef6..951dac58 100644 --- a/man/plot_natural_mortality.Rd +++ b/man/plot_natural_mortality.Rd @@ -18,7 +18,7 @@ plot_natural_mortality( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{group}{a string of a single column that groups the data (e.g. "fleet", diff --git a/man/plot_recruitment.Rd b/man/plot_recruitment.Rd index 0de81b6d..162a8308 100644 --- a/man/plot_recruitment.Rd +++ b/man/plot_recruitment.Rd @@ -19,7 +19,7 @@ plot_recruitment( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{unit_label}{units for recruitment} diff --git a/man/plot_recruitment_deviations.Rd b/man/plot_recruitment_deviations.Rd index 5b7f4a79..63b05753 100644 --- a/man/plot_recruitment_deviations.Rd +++ b/man/plot_recruitment_deviations.Rd @@ -16,7 +16,7 @@ plot_recruitment_deviations( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{module}{(Optional) A string indicating the linked module_name associated diff --git a/man/plot_spawning_biomass.Rd b/man/plot_spawning_biomass.Rd index 09e3b721..bf76af43 100644 --- a/man/plot_spawning_biomass.Rd +++ b/man/plot_spawning_biomass.Rd @@ -23,7 +23,7 @@ plot_spawning_biomass( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{geom}{A string stating the geom used for the plot. Default is "line". diff --git a/man/plot_stock_recruitment.Rd b/man/plot_stock_recruitment.Rd index 86a7e4ad..4bf4955b 100644 --- a/man/plot_stock_recruitment.Rd +++ b/man/plot_stock_recruitment.Rd @@ -17,7 +17,7 @@ plot_stock_recruitment( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{spawning_biomass_label}{Units for spawning biomass} diff --git a/man/save_all_plots.Rd b/man/save_all_plots.Rd index 1ae75ddb..93e05181 100644 --- a/man/save_all_plots.Rd +++ b/man/save_all_plots.Rd @@ -30,7 +30,7 @@ save_all_plots( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{recruitment_unit_label}{Units for recruitment} diff --git a/man/table_landings.Rd b/man/table_landings.Rd index 8d80179a..7ec21c99 100644 --- a/man/table_landings.Rd +++ b/man/table_landings.Rd @@ -19,7 +19,7 @@ table_landings( } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{unit_label}{Abbreviated units of landings} diff --git a/man/write_captions.Rd b/man/write_captions.Rd index eb44c76c..5dcb7fc3 100644 --- a/man/write_captions.Rd +++ b/man/write_captions.Rd @@ -10,7 +10,7 @@ write_captions(dat, dir = getwd(), year = format(Sys.Date(), "\%Y")) } \arguments{ \item{dat}{A data frame or names list of data frames (input as `list()`) -returned from \link[asar]{convert_output}. The first data frame in the list +returned from \link[stockplotr]{convert_output}. The first data frame in the list is used in calculation of a reference line if one is present} \item{dir}{Directory where the output captions and alt text file should be