diff --git a/R/convert_output.R b/R/convert_output.R index adfc503d..754726f7 100644 --- a/R/convert_output.R +++ b/R/convert_output.R @@ -254,6 +254,8 @@ convert_output <- function( # }, cli::cli_abort("Unknown file type. Please indicate model.") ) + }else { + model <- models[i] } } else if (is.null(models) || is.na(models[i])) { model <- switch(class(file)[1], diff --git a/R/plot_abundance_at_age.R b/R/plot_abundance_at_age.R index 9c77aebb..322681db 100644 --- a/R/plot_abundance_at_age.R +++ b/R/plot_abundance_at_age.R @@ -38,10 +38,7 @@ #' @param model Character vector. (Optional) Model name found in `dat$model`. #' If selecting >1 model, place them in a vector like c("model1", "model2"). #' -#' Default: NULL -#' -#' If the interactive and >1 model is found, user will select the -#' model in the console. @seealso [filter_data()] +#' Default: "All" (all models). Set to NULL for interactive selection when available. #' @param interactive A logical value indicating if the environment is interactive. #' #' Default: `FALSE` @@ -88,7 +85,7 @@ plot_abundance_at_age <- function( scale_amount = 1, proportional = TRUE, module = NULL, - model = NULL, + model = "All", interactive = TRUE, make_rda = FALSE, figures_dir = getwd() diff --git a/R/plot_biomass.R b/R/plot_biomass.R index fdd14630..4488f4c9 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -50,7 +50,7 @@ plot_biomass <- function( era = NULL, unit_label = NULL, module = NULL, - model = NULL, + model = "All", scale_amount = 1, relative = FALSE, make_rda = FALSE, diff --git a/R/plot_biomass_at_age.R b/R/plot_biomass_at_age.R index 9d5d0af7..4a43f1e7 100644 --- a/R/plot_biomass_at_age.R +++ b/R/plot_biomass_at_age.R @@ -46,7 +46,7 @@ plot_biomass_at_age <- function( scale_amount = 1, proportional = TRUE, module = NULL, - model = NULL, + model = "All", interactive = TRUE, make_rda = FALSE, figures_dir = getwd() diff --git a/R/plot_catch_at_age.R b/R/plot_catch_at_age.R index 5d403476..72c10b7d 100644 --- a/R/plot_catch_at_age.R +++ b/R/plot_catch_at_age.R @@ -74,7 +74,7 @@ plot_catch_at_age <- function( proportional = TRUE, interactive = TRUE, module = NULL, - model = NULL, + model = "All", make_rda = FALSE, figures_dir = getwd() ) { diff --git a/R/plot_discard.R b/R/plot_discard.R index 76f49e72..ad7615cc 100644 --- a/R/plot_discard.R +++ b/R/plot_discard.R @@ -87,7 +87,7 @@ plot_discard <- function( scale_amount = 1, interactive = TRUE, module = NULL, - model = NULL, + model = "All", predicted = TRUE, interval = 0.95, tolerance = 0.001, diff --git a/R/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index a33f94cd..c10d8f81 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -36,7 +36,7 @@ plot_fishing_mortality <- function( ref_line = "msy", era = NULL, module = NULL, - model = NULL, + model = "All", make_rda = FALSE, figures_dir = getwd(), interactive = TRUE, diff --git a/R/plot_index.R b/R/plot_index.R index 22007a44..0d70caf6 100644 --- a/R/plot_index.R +++ b/R/plot_index.R @@ -75,7 +75,7 @@ plot_index <- function( facet = NULL, interactive = TRUE, module = NULL, - model = NULL, + model = "All", focus = NULL, interval = 0.95, log = FALSE, diff --git a/R/plot_landings.R b/R/plot_landings.R index 4749e545..ab830d53 100644 --- a/R/plot_landings.R +++ b/R/plot_landings.R @@ -97,7 +97,7 @@ plot_landings <- function( era = NULL, scale_amount = 1, module = NULL, - model = NULL, + model = "All", interactive = TRUE, predicted = TRUE, interval = 0.95, diff --git a/R/plot_natural_mortality.R b/R/plot_natural_mortality.R index 375c144e..0b00506c 100644 --- a/R/plot_natural_mortality.R +++ b/R/plot_natural_mortality.R @@ -26,7 +26,7 @@ plot_natural_mortality <- function( era = NULL, interactive = TRUE, module = NULL, - model = NULL, + model = "All", make_rda = FALSE, figures_dir = getwd(), ... diff --git a/R/plot_recruitment.R b/R/plot_recruitment.R index ced98abd..d68de69a 100644 --- a/R/plot_recruitment.R +++ b/R/plot_recruitment.R @@ -42,7 +42,7 @@ plot_recruitment <- function( # relative = FALSE, interactive = TRUE, module = NULL, - model = NULL, + model = "All", make_rda = FALSE, figures_dir = getwd() ) { diff --git a/R/plot_recruitment_deviations.R b/R/plot_recruitment_deviations.R index 72122986..306a8f46 100644 --- a/R/plot_recruitment_deviations.R +++ b/R/plot_recruitment_deviations.R @@ -33,7 +33,7 @@ plot_recruitment_deviations <- function( dat, module = NULL, - model = NULL, + model = "All", era = NULL, interactive = TRUE, make_rda = FALSE, diff --git a/R/plot_selectivity.R b/R/plot_selectivity.R index 48169f13..4a75d090 100644 --- a/R/plot_selectivity.R +++ b/R/plot_selectivity.R @@ -37,7 +37,7 @@ plot_selectivity <- function( facet = NULL, interactive = TRUE, module = NULL, - model = NULL, + model = "All", make_rda = FALSE, figures_dir = getwd(), ... diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 7dea2b04..4fe58dc8 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -62,10 +62,7 @@ #' @param model Character vector. (Optional) Model name found in `dat$model`. #' If selecting >1 model, place them in a vector like c("model1", "model2"). #' -#' Default: NULL -#' -#' If the interactive and >1 model is found, user will select the -#' model in the console. @seealso [filter_data()] +#' Default: "All" (all models). Set to NULL for interactive selection when available. #' @param scale_amount Number. A number to scale the y-axis values. If > 1, the #' magnitude of the scale amount will be added to the unit label #' (e.g. scale_amount = 1000 with a unit label of "1000s of lbs" will result @@ -139,7 +136,7 @@ plot_spawning_biomass <- function( era = NULL, lbs = lifecycle::deprecated(), module = NULL, - model = NULL, + model = "All", scale_amount = 1, relative = FALSE, make_rda = FALSE, diff --git a/R/plot_spr.R b/R/plot_spr.R index 01d786c0..188af650 100644 --- a/R/plot_spr.R +++ b/R/plot_spr.R @@ -41,7 +41,7 @@ plot_spr <- function( ref_line = "target", era = NULL, module = NULL, - model = NULL, + model = "All", interactive = TRUE, make_rda = FALSE, figures_dir = getwd(), diff --git a/R/plot_stock_recruitment.R b/R/plot_stock_recruitment.R index b5444773..76074179 100644 --- a/R/plot_stock_recruitment.R +++ b/R/plot_stock_recruitment.R @@ -43,7 +43,7 @@ plot_stock_recruitment <- function( interactive = TRUE, era = NULL, module = NULL, - model = NULL, + model = "All", scale_amount = 1, make_rda = FALSE, figures_dir = getwd() @@ -106,7 +106,8 @@ plot_stock_recruitment <- function( era = era, scale_amount = scale_amount, interactive = interactive, - module = module + module = module, + model = selected_model ) process_sb <- process_data( diff --git a/R/process_data.R b/R/process_data.R index ad0c1b98..5fcee3f6 100644 --- a/R/process_data.R +++ b/R/process_data.R @@ -236,6 +236,7 @@ process_data <- function( } # close length index_vars > 0 if (!is.null(group) && group != "none") { + data <- dplyr::mutate(data, group_var = .data[[group]]) # check if value varies in ANY year # pivot data for 1st indexed data and check if all the same if (length(index_variables) > 0) { diff --git a/R/table_biomass_abundance_catch.R b/R/table_biomass_abundance_catch.R index 70997228..2206935a 100644 --- a/R/table_biomass_abundance_catch.R +++ b/R/table_biomass_abundance_catch.R @@ -1,6 +1,8 @@ #' Biomass, abundance, and catch time series table #' #' @inheritParams plot_recruitment +#' @param model Character. Model name found in `dat$model`. If NULL, one model +#' is selected interactively when possible, or the first available model is used. #' #' @param biomass_label String. Units for biomass #' diff --git a/R/table_index.R b/R/table_index.R index 3efdf5f1..5e5b64e8 100644 --- a/R/table_index.R +++ b/R/table_index.R @@ -1,6 +1,8 @@ #' Index of abundance table #' #' @inheritParams plot_recruitment +#' @param model Character. Model name found in `dat$model`. If NULL, one model +#' is selected interactively when possible, or the first available model is used. #' @param group String. Single column that groups the data. #' #' Set group = "none" to summarize data over all indexing values. diff --git a/R/table_landings.R b/R/table_landings.R index 977cda14..3bd1133a 100644 --- a/R/table_landings.R +++ b/R/table_landings.R @@ -1,6 +1,8 @@ #' Landed catch by fleet and year table #' #' @inheritParams plot_recruitment +#' @param model Character. Model name found in `dat$model`. If NULL, one model +#' is selected interactively when possible, or the first available model is used. #' @param unit_label String. Abbreviated landings units #' #' Default: NULL diff --git a/R/table_total_catch.R b/R/table_total_catch.R index 92caaea0..90ff6ea7 100644 --- a/R/table_total_catch.R +++ b/R/table_total_catch.R @@ -1,6 +1,8 @@ #' Catch (table) by fleet, gear type, sector #' #' @inheritParams plot_recruitment +#' @param model Character. Model name found in `dat$model`. If NULL, one model +#' is selected interactively when possible, or the first available model is used. #' @param unit_label String. Abbreviated catch units #' #' Default: NULL diff --git a/R/utils_plot.R b/R/utils_plot.R index 7e26d639..dbb87108 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -658,7 +658,14 @@ cap_first_letter <- function(s) { #' @inheritParams plot_spawning_biomass #' @param label_name String. Name of the label used to filter #' the data. +#' @param model Character vector. Model names to retain, or "All" to retain +#' all models when multiple models are allowed. If NULL, selection is interactive +#' when possible and otherwise defaults to all models when allowed, or the first. +#' +#' Default: NULL #' @param allow_multiple_models Logical. Whether to allow multiple models. +#' When FALSE and `model` is NULL, one model is selected interactively or the +#' first is used in non-interactive mode. #' #' Default: TRUE #' @@ -690,7 +697,7 @@ filter_data <- function( interactive = TRUE, allow_multiple_models = TRUE ) { - # Check if there are multiple module_names present + # Filter by model before selecting a module. model_select <- filter_interactive( dat, column_name = "model", @@ -1210,8 +1217,11 @@ add_reference_line <- function( #' #' @param column_name String. Name of the column to allow interactive filtering for. #' -#' @param column_value String. Value of the column to filter by. If NULL, interactive -#' selection will be enabled. +#' @param column_value Character vector. Values of the column to filter by. For +#' models, "All" retains all models when multiple selection is allowed. If NULL, +#' selection is interactive when possible, defaulting to all models when multiple +#' selection is allowed and the first value otherwise. In the interactive model +#' menu, selecting specific models overrides the preselected "All" option. #' #' Default: NULL #' @@ -1221,7 +1231,7 @@ add_reference_line <- function( #' #' @param allow_multiple_selection Logical. Whether to allow the selection of multiple inputs. #' -#' Default: FALSE +#' Default: TRUE #' #' @return List. filtered data.frame (`plot_data`) and selected option(`selected`). #' @@ -1243,55 +1253,56 @@ filter_interactive <- function( ) { pretty_columns <- c("module_name" = "module", "model" = "model") + default_all <- column_name == "model" && allow_multiple_selection + plot_dat <- dat selected <- unique(dat[[column_name]]) - if (length(unique(dat[[column_name]])) > 1) { + if (length(selected) > 1) { available_names <- unique(dat[[column_name]]) if (!is.null(column_value)) { - if (length(column_value) > 1 && !allow_multiple_selection) { + if ((length(column_value) > 1 || (column_name == "model" && "All" %in% column_value)) && !allow_multiple_selection) { cli::cli_alert_danger("Multiple {pretty_columns[column_name]}s provided, but only 1 allowed. Defaulting to first available {pretty_columns[column_name]} name: {available_names[1]}.") column_value <- available_names[1] } - plot_dat <- dat |> dplyr::filter(.data[[column_name]] %in% column_value) - selected <- column_value + selected <- if (default_all && "All" %in% column_value) available_names else column_value + plot_dat <- dat |> dplyr::filter(.data[[column_name]] %in% selected) } else { cli::cli_alert_warning("Multiple {pretty_columns[column_name]}s found in data. \n") - options <- c() - for (i in seq_along(available_names)) { - # options <- paste0(options, " ", i, ") ", unique(plot_data$module_name)[i], "\n") - options[i] <- paste0(available_names[i]) - } - if (column_name == "model") { - options <- c(options, "All") - } + options <- as.character(available_names) + if (default_all) options <- c("All", options) if (interactive()) { if (interactive) { selection <- utils::select.list( options, + preselect = if (default_all) "All" else NULL, multiple = allow_multiple_selection, title = glue::glue("Select one of the following {pretty_columns[column_name]} names") ) question1 <- if (length(selection) == 0 || identical(selection, "")) { options[1] - } else if ("All" %in% selection) { - options[-length(options)] } else { selection } - selected <- intersect(available_names, question1) + selected <- if (default_all && identical(question1, "All")) { + available_names + } else if (default_all) { + intersect(available_names, setdiff(question1, "All")) + } else { + intersect(available_names, question1) + } if (length(selected) < 1 || (length(selected) > 1 && !allow_multiple_selection)) { selected <- available_names[1] } } else { - selected <- available_names[1] - cli::cli_alert_info("Selection bypassed. Filtering by {selected}.") + selected <- if (default_all) available_names else available_names[1] + cli::cli_alert_info("Selection bypassed. Filtering by {paste(selected, collapse = ', ')}.") } } else { - selected <- available_names[1] - cli::cli_alert_info(glue::glue("Environment not interactive. Selecting {selected}.")) + selected <- if (default_all) available_names else available_names[1] + cli::cli_alert_info(glue::glue("Environment not interactive. Selecting {paste(selected, collapse = ', ')}.")) } if (length(selected) > 0) { plot_dat <- dat |> diff --git a/inst/WORDLIST b/inst/WORDLIST index 4d89ec26..6dee3ae7 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -105,6 +105,7 @@ pkgdown Pre Preformatted preformatted +preselected qq Rceattle rceattle diff --git a/man/filter_data.Rd b/man/filter_data.Rd index 3168a2f4..09ea22cc 100644 --- a/man/filter_data.Rd +++ b/man/filter_data.Rd @@ -36,13 +36,11 @@ Default: NULL If the interactive and >1 module_name is found, user will select the module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} -\item{model}{Character vector. (Optional) Model name found in \code{dat$model}. -If selecting >1 model, place them in a vector like c("model1", "model2"). +\item{model}{Character vector. Model names to retain, or "All" to retain +all models when multiple models are allowed. If NULL, selection is interactive +when possible and otherwise defaults to all models when allowed, or the first. -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: NULL} \item{era}{String. Era of data. @@ -79,6 +77,8 @@ Default: 1} Default: \code{FALSE}} \item{allow_multiple_models}{Logical. Whether to allow multiple models. +When FALSE and \code{model} is NULL, one model is selected interactively or the +first is used in non-interactive mode. Default: TRUE} } diff --git a/man/filter_interactive.Rd b/man/filter_interactive.Rd index 92cf024d..c761128e 100644 --- a/man/filter_interactive.Rd +++ b/man/filter_interactive.Rd @@ -18,8 +18,11 @@ returned from \link[stockplotr]{convert_output}.} \item{column_name}{String. Name of the column to allow interactive filtering for.} -\item{column_value}{String. Value of the column to filter by. If NULL, interactive -selection will be enabled. +\item{column_value}{Character vector. Values of the column to filter by. For +models, "All" retains all models when multiple selection is allowed. If NULL, +selection is interactive when possible, defaulting to all models when multiple +selection is allowed and the first value otherwise. In the interactive model +menu, selecting specific models overrides the preselected "All" option. Default: NULL} @@ -29,7 +32,7 @@ Default: TRUE} \item{allow_multiple_selection}{Logical. Whether to allow the selection of multiple inputs. -Default: FALSE} +Default: TRUE} } \value{ List. filtered data.frame (\code{plot_data}) and selected option(\code{selected}). diff --git a/man/plot_abundance_at_age.Rd b/man/plot_abundance_at_age.Rd index bf6188e2..693d5adc 100644 --- a/man/plot_abundance_at_age.Rd +++ b/man/plot_abundance_at_age.Rd @@ -11,7 +11,7 @@ plot_abundance_at_age( scale_amount = 1, proportional = TRUE, module = NULL, - model = NULL, + model = "All", interactive = TRUE, make_rda = FALSE, figures_dir = getwd() @@ -58,10 +58,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{interactive}{A logical value indicating if the environment is interactive. diff --git a/man/plot_biomass.Rd b/man/plot_biomass.Rd index 416079d8..9231256c 100644 --- a/man/plot_biomass.Rd +++ b/man/plot_biomass.Rd @@ -13,7 +13,7 @@ plot_biomass( era = NULL, unit_label = NULL, module = NULL, - model = NULL, + model = "All", scale_amount = 1, relative = FALSE, make_rda = FALSE, @@ -79,10 +79,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{scale_amount}{Number. A number to scale the y-axis values. If > 1, the magnitude of the scale amount will be added to the unit label diff --git a/man/plot_biomass_at_age.Rd b/man/plot_biomass_at_age.Rd index 99e15c73..1b4ef4d6 100644 --- a/man/plot_biomass_at_age.Rd +++ b/man/plot_biomass_at_age.Rd @@ -11,7 +11,7 @@ plot_biomass_at_age( scale_amount = 1, proportional = TRUE, module = NULL, - model = NULL, + model = "All", interactive = TRUE, make_rda = FALSE, figures_dir = getwd() @@ -56,10 +56,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{interactive}{Logical. TRUE/FALSE; indicate whether the environment in which the function is operating is interactive. This bypasses some options for diff --git a/man/plot_catch_at_age.Rd b/man/plot_catch_at_age.Rd index df5059fd..784e4bc2 100644 --- a/man/plot_catch_at_age.Rd +++ b/man/plot_catch_at_age.Rd @@ -13,7 +13,7 @@ plot_catch_at_age( proportional = TRUE, interactive = TRUE, module = NULL, - model = NULL, + model = "All", make_rda = FALSE, figures_dir = getwd() ) @@ -69,10 +69,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{make_rda}{Logical. TRUE/FALSE; indicate whether to produce an .rda file containing a list with the figure/table, caption, and alternative text (if figure). If TRUE, diff --git a/man/plot_discard.Rd b/man/plot_discard.Rd index caca4458..1b951610 100644 --- a/man/plot_discard.Rd +++ b/man/plot_discard.Rd @@ -15,7 +15,7 @@ plot_discard( scale_amount = 1, interactive = TRUE, module = NULL, - model = NULL, + model = "All", predicted = TRUE, interval = 0.95, tolerance = 0.001, @@ -92,10 +92,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{predicted}{Logical. TRUE (Default) or FALSE; show the discards predicted by the model as lines, with the observed discards as points, to diff --git a/man/plot_fishing_mortality.Rd b/man/plot_fishing_mortality.Rd index ecc4d0a4..89f60a06 100644 --- a/man/plot_fishing_mortality.Rd +++ b/man/plot_fishing_mortality.Rd @@ -12,7 +12,7 @@ plot_fishing_mortality( ref_line = "msy", era = NULL, module = NULL, - model = NULL, + model = "All", make_rda = FALSE, figures_dir = getwd(), interactive = TRUE, @@ -75,10 +75,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{make_rda}{Logical. TRUE/FALSE; indicate whether to save the object and make an automated caption and alternative text in the form of an \code{rda} object. If TRUE, diff --git a/man/plot_index.Rd b/man/plot_index.Rd index a21c24cb..f68e7a8d 100644 --- a/man/plot_index.Rd +++ b/man/plot_index.Rd @@ -11,7 +11,7 @@ plot_index( facet = NULL, interactive = TRUE, module = NULL, - model = NULL, + model = "All", focus = NULL, interval = 0.95, log = FALSE, @@ -56,10 +56,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{focus}{Character vector. Column name or names used to filter data. (i.e. select names of fleets to zoom into the plot). Default = NULL} diff --git a/man/plot_landings.Rd b/man/plot_landings.Rd index 2134758b..a8995661 100644 --- a/man/plot_landings.Rd +++ b/man/plot_landings.Rd @@ -14,7 +14,7 @@ plot_landings( era = NULL, scale_amount = 1, module = NULL, - model = NULL, + model = "All", interactive = TRUE, predicted = TRUE, interval = 0.95, @@ -88,10 +88,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{interactive}{Logical. TRUE/FALSE; indicate whether the environment is interactive. diff --git a/man/plot_natural_mortality.Rd b/man/plot_natural_mortality.Rd index dd719185..6e1795ba 100644 --- a/man/plot_natural_mortality.Rd +++ b/man/plot_natural_mortality.Rd @@ -11,7 +11,7 @@ plot_natural_mortality( era = NULL, interactive = TRUE, module = NULL, - model = NULL, + model = "All", make_rda = FALSE, figures_dir = getwd(), ... @@ -56,10 +56,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{make_rda}{Logical. TRUE/FALSE; indicate whether to save the object and make an automated caption and alternative text in the form of an \code{rda} object. If TRUE, diff --git a/man/plot_recruitment.Rd b/man/plot_recruitment.Rd index f4bb2355..c71cf597 100644 --- a/man/plot_recruitment.Rd +++ b/man/plot_recruitment.Rd @@ -13,7 +13,7 @@ plot_recruitment( facet = NULL, interactive = TRUE, module = NULL, - model = NULL, + model = "All", make_rda = FALSE, figures_dir = getwd() ) @@ -68,10 +68,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{make_rda}{Logical. TRUE/FALSE; indicate whether to save the object and make an automated caption and alternative text in the form of an \code{rda} object. If TRUE, diff --git a/man/plot_recruitment_deviations.Rd b/man/plot_recruitment_deviations.Rd index 10011862..a187217c 100644 --- a/man/plot_recruitment_deviations.Rd +++ b/man/plot_recruitment_deviations.Rd @@ -7,7 +7,7 @@ plot_recruitment_deviations( dat, module = NULL, - model = NULL, + model = "All", era = NULL, interactive = TRUE, make_rda = FALSE, @@ -33,10 +33,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{era}{String. Era of data. diff --git a/man/plot_selectivity.Rd b/man/plot_selectivity.Rd index 4b800bdd..de8af024 100644 --- a/man/plot_selectivity.Rd +++ b/man/plot_selectivity.Rd @@ -11,7 +11,7 @@ plot_selectivity( facet = NULL, interactive = TRUE, module = NULL, - model = NULL, + model = "All", make_rda = FALSE, figures_dir = getwd(), ... @@ -54,10 +54,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{make_rda}{Logical. TRUE/FALSE; indicate whether to save the object and make an automated caption and alternative text in the form of an \code{rda} object. If TRUE, diff --git a/man/plot_spawning_biomass.Rd b/man/plot_spawning_biomass.Rd index 933c4b6e..18ec4610 100644 --- a/man/plot_spawning_biomass.Rd +++ b/man/plot_spawning_biomass.Rd @@ -14,7 +14,7 @@ plot_spawning_biomass( era = NULL, lbs = lifecycle::deprecated(), module = NULL, - model = NULL, + model = "All", scale_amount = 1, relative = FALSE, make_rda = FALSE, @@ -93,10 +93,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{scale_amount}{Number. A number to scale the y-axis values. If > 1, the magnitude of the scale amount will be added to the unit label diff --git a/man/plot_spr.Rd b/man/plot_spr.Rd index d63d670d..a9cde6d3 100644 --- a/man/plot_spr.Rd +++ b/man/plot_spr.Rd @@ -12,7 +12,7 @@ plot_spr( ref_line = "target", era = NULL, module = NULL, - model = NULL, + model = "All", interactive = TRUE, make_rda = FALSE, figures_dir = getwd(), @@ -76,10 +76,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{interactive}{Logical. TRUE/FALSE; indicate whether the environment is interactive. diff --git a/man/plot_stock_recruitment.Rd b/man/plot_stock_recruitment.Rd index 16a52b5c..47ae2d66 100644 --- a/man/plot_stock_recruitment.Rd +++ b/man/plot_stock_recruitment.Rd @@ -11,7 +11,7 @@ plot_stock_recruitment( interactive = TRUE, era = NULL, module = NULL, - model = NULL, + model = "All", scale_amount = 1, make_rda = FALSE, figures_dir = getwd() @@ -56,10 +56,7 @@ module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} \item{model}{Character vector. (Optional) Model name found in \code{dat$model}. If selecting >1 model, place them in a vector like c("model1", "model2"). -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +Default: "All" (all models). Set to NULL for interactive selection when available.} \item{scale_amount}{Number. A number to scale the y-axis values. If > 1, the magnitude of the scale amount will be added to the unit label diff --git a/man/table_biomass_abundance_catch.Rd b/man/table_biomass_abundance_catch.Rd index e0f8df67..fc857486 100644 --- a/man/table_biomass_abundance_catch.Rd +++ b/man/table_biomass_abundance_catch.Rd @@ -78,13 +78,8 @@ in this ordered list: "landings_weight", "landings_numbers", "landings_expected Default: NULL} -\item{model}{Character vector. (Optional) Model name found in \code{dat$model}. -If selecting >1 model, place them in a vector like c("model1", "model2"). - -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +\item{model}{Character. Model name found in \code{dat$model}. If NULL, one model +is selected interactively when possible, or the first available model is used.} \item{make_rda}{Logical. TRUE/FALSE; indicate whether to save the object and make an automated caption and alternative text in the form of an \code{rda} object. If TRUE, diff --git a/man/table_index.Rd b/man/table_index.Rd index f986ba31..7bcbc2e8 100644 --- a/man/table_index.Rd +++ b/man/table_index.Rd @@ -57,13 +57,8 @@ Default: NULL If the interactive and >1 module_name is found, user will select the module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} -\item{model}{Character vector. (Optional) Model name found in \code{dat$model}. -If selecting >1 model, place them in a vector like c("model1", "model2"). - -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +\item{model}{Character. Model name found in \code{dat$model}. If NULL, one model +is selected interactively when possible, or the first available model is used.} \item{label}{String. The label that will be chosen from the input file. If unspecified, the function will search the "label" column and use the first matching label diff --git a/man/table_landings.Rd b/man/table_landings.Rd index 509a16f4..fc7f39b6 100644 --- a/man/table_landings.Rd +++ b/man/table_landings.Rd @@ -63,13 +63,8 @@ Default: NULL If the interactive and >1 module_name is found, user will select the module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} -\item{model}{Character vector. (Optional) Model name found in \code{dat$model}. -If selecting >1 model, place them in a vector like c("model1", "model2"). - -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +\item{model}{Character. Model name found in \code{dat$model}. If NULL, one model +is selected interactively when possible, or the first available model is used.} \item{label}{String or vector of strings. The labels in the "label" column of \code{dat} to tabulate (e.g., "landings_predicted_weight"). If unspecified, diff --git a/man/table_projections.Rd b/man/table_projections.Rd index b122c685..4390a574 100644 --- a/man/table_projections.Rd +++ b/man/table_projections.Rd @@ -42,13 +42,8 @@ Default: NULL If the interactive and >1 module_name is found, user will select the module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} -\item{model}{Character vector. (Optional) Model name found in \code{dat$model}. -If selecting >1 model, place them in a vector like c("model1", "model2"). - -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +\item{model}{Character. Model name found in \code{dat$model}. If NULL, one model +is selected interactively when possible, or the first available model is used.} \item{make_rda}{Logical. TRUE/FALSE; indicate whether to save the object and make an automated caption and alternative text in the form of an \code{rda} object. If TRUE, diff --git a/man/table_total_catch.Rd b/man/table_total_catch.Rd index ea656a1b..9ccb023b 100644 --- a/man/table_total_catch.Rd +++ b/man/table_total_catch.Rd @@ -46,13 +46,8 @@ Default: NULL If the interactive and >1 module_name is found, user will select the module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} -\item{model}{Character vector. (Optional) Model name found in \code{dat$model}. -If selecting >1 model, place them in a vector like c("model1", "model2"). - -Default: NULL - -If the interactive and >1 model is found, user will select the -model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} +\item{model}{Character. Model name found in \code{dat$model}. If NULL, one model +is selected interactively when possible, or the first available model is used.} \item{scale_amount}{Number. A number to scale the y-axis values. If > 1, the magnitude of the scale amount will be added to the unit label diff --git a/tests/testthat/test-convert_output.R b/tests/testthat/test-convert_output.R index 7a17a196..059f6977 100644 --- a/tests/testthat/test-convert_output.R +++ b/tests/testthat/test-convert_output.R @@ -71,7 +71,7 @@ test_that("missing arguments trigger warnings or errors", { models = "fake_model", save_dir = fs::path("fixtures", "ss3_models_converted", "Hake_2018") ), - "`file` not found." + "Output file not compatible." ) dir.create(fs::path("fixtures", "ss3_models_converted", "Hake_2018"), recursive = TRUE) diff --git a/tests/testthat/test-plot_spawning_biomass.R b/tests/testthat/test-plot_spawning_biomass.R index 97303c2f..8f246963 100644 --- a/tests/testthat/test-plot_spawning_biomass.R +++ b/tests/testthat/test-plot_spawning_biomass.R @@ -40,6 +40,47 @@ test_that("plot_spawning_biomass generates plots without errors", { ) }) +test_that("plot_spawning_biomass plots all models by default", { + testthat::local_mocked_bindings(interactive = function() TRUE, .package = "base") + testthat::local_mocked_bindings( + select.list = function(...) stop("Unexpected selection prompt"), + .package = "utils" + ) + dat <- dplyr::bind_rows( + dplyr::mutate(stockplotr::example_data, model = "model1"), + dplyr::mutate(stockplotr::example_data, model = "model2", estimate=estimate*1.5) + ) + plot <- plot_spawning_biomass( + dat, module = "TIME_SERIES", unit_label = "mt", + ref_line = c("msy" = 2000000), interactive = TRUE + ) + expect_setequal(unique(plot$layers[[2]]$data$model), c("model1", "model2")) + expect_length(unique(ggplot2::ggplot_build(plot)$data[[2]]$group), 2) + + plot <- plot_spawning_biomass( + dat, module = "TIME_SERIES", model = "model2", unit_label = "mt", + ref_line = c("msy" = 2000000), interactive = FALSE + ) + expect_equal(unique(plot$layers[[2]]$data$model), "model2") +}) + +test_that("interactive model selection limits the plot", { + testthat::local_mocked_bindings(interactive = function() TRUE, .package = "base") + testthat::local_mocked_bindings( + select.list = function(...) c("All", "model2"), + .package = "utils" + ) + dat <- dplyr::bind_rows( + dplyr::mutate(stockplotr::example_data, model = "model1"), + dplyr::mutate(stockplotr::example_data, model = "model2") + ) + plot <- plot_spawning_biomass( + dat, module = "TIME_SERIES", model = NULL, interactive = TRUE, + unit_label = "mt", ref_line = c("msy" = 2000000) + ) + expect_equal(unique(plot$layers[[2]]$data$model), "model2") +}) + # test_that("rda file made when indicated", { # # export rda # plot_spawning_biomass( diff --git a/tests/testthat/test-plot_stock_recruitment.R b/tests/testthat/test-plot_stock_recruitment.R index b918730f..96d61af0 100644 --- a/tests/testthat/test-plot_stock_recruitment.R +++ b/tests/testthat/test-plot_stock_recruitment.R @@ -34,6 +34,19 @@ test_that("plot_stock_recruitment generates plots without errors", { ) }) +test_that("stock recruitment uses the selected models for both series", { + dat <- dplyr::bind_rows( + dplyr::mutate(stockplotr::example_data, model = "model1"), + dplyr::mutate(stockplotr::example_data, model = "model2") + ) + plot <- plot_stock_recruitment( + dat, model = "model2", interactive = FALSE, + spawning_biomass_label = "mt", recruitment_label = "mt" + ) + expect_equal(unique(plot$layers[[1]]$data$model), "model2") + expect_true(any(!is.na(plot$layers[[1]]$data$predicted_recruitment))) +}) + # test_that("rda file made when indicated", { # # export rda # plot_stock_recruitment( diff --git a/tests/testthat/test-utils_plot.R b/tests/testthat/test-utils_plot.R index f75b25f1..8f5d5ada 100644 --- a/tests/testthat/test-utils_plot.R +++ b/tests/testthat/test-utils_plot.R @@ -69,6 +69,122 @@ test_that("filter_data handles data frames", { expect_setequal(unique(filtered_list$group_var), c("model_one", "model_two")) }) +test_that("model defaults retain all models in interactive and non-interactive modes", { + testthat::local_mocked_bindings( + interactive = function() TRUE, + .package = "base" + ) + choices_seen <- NULL + preselect_seen <- NULL + selection <- character(0) + testthat::local_mocked_bindings(select.list = function(choices, preselect, ...) { + choices_seen <<- choices + preselect_seen <<- preselect + selection + }, .package = "utils") + + dat <- make_utils_plot_multimodel_data() + for (interactive in c(TRUE, FALSE)) { + filtered <- filter_data( + dat = dat, + label_name = "^biomass$", + geom = "line", + interactive = interactive + ) + expect_setequal(unique(filtered$model), c("model_one", "model_two")) + expect_setequal(unique(filtered$group_var), c("model_one", "model_two")) + expect_setequal(get("selected_model", envir = .GlobalEnv), c("model_one", "model_two")) + } + expect_equal(choices_seen, c("All", "model_one", "model_two")) + expect_equal(preselect_seen, "All") + + selection <- "All" + filtered <- filter_data( + dat = dat, label_name = "^biomass$", geom = "line", interactive = TRUE + ) + expect_setequal(unique(filtered$model), c("model_one", "model_two")) + + selection <- "model_two" + filtered <- filter_data( + dat = dat, label_name = "^biomass$", geom = "line", interactive = TRUE + ) + expect_equal(unique(filtered$model), "model_two") + + selection <- c("All", "model_two") + filtered <- filter_data( + dat = dat, label_name = "^biomass$", geom = "line", interactive = TRUE + ) + expect_equal(unique(filtered$model), "model_two") + expect_equal(get("selected_model", envir = .GlobalEnv), "model_two") + + filtered <- filter_data( + dat = dat, label_name = "^biomass$", geom = "line", + model = "model_two", interactive = TRUE + ) + expect_equal(unique(filtered$model), "model_two") +}) + +test_that("specific interactive model selections override All", { + testthat::local_mocked_bindings(interactive = function() TRUE, .package = "base") + testthat::local_mocked_bindings( + select.list = function(...) c("All", "model_two", "model_three"), + .package = "utils" + ) + dat <- tibble::tibble(model = c("model_one", "model_two", "model_three", "model_four")) + selected <- stockplotr:::filter_interactive(dat, "model", interactive = TRUE) + expect_equal(selected$selected, c("model_two", "model_three")) + expect_equal(selected$plot_data$model, c("model_two", "model_three")) +}) + +test_that("model defaults retain all models when the environment is non-interactive", { + testthat::local_mocked_bindings(interactive = function() FALSE, .package = "base") + for (interactive in c(TRUE, FALSE)) { + selected <- stockplotr:::filter_interactive( + make_utils_plot_multimodel_data(), "model", interactive = interactive + ) + expect_setequal(unique(selected$plot_data$model), c("model_one", "model_two")) + expect_setequal(selected$selected, c("model_one", "model_two")) + } +}) + +test_that("All selects every model but remains a literal module name", { + dat <- make_utils_plot_multimodel_data() + selected <- stockplotr:::filter_interactive(dat, "model", column_value = "All") + expect_identical(selected$plot_data, dat) + expect_equal(selected$selected, c("model_one", "model_two")) + + modules <- tibble::tibble(module_name = c("All", "OTHER")) + selected <- stockplotr:::filter_interactive(modules, "module_name", column_value = "All") + expect_equal(selected$plot_data$module_name, "All") + expect_equal(selected$selected, "All") +}) + +test_that("single-model selection and module selection retain their defaults", { + dat <- make_utils_plot_multimodel_data() + selected <- stockplotr:::filter_interactive( + dat, "model", interactive = FALSE, allow_multiple_selection = FALSE + ) + expect_equal(unique(selected$plot_data$model), "model_one") + expect_equal(selected$selected, "model_one") + + testthat::local_mocked_bindings(interactive = function() TRUE, .package = "base") + testthat::local_mocked_bindings( + select.list = function(...) "model_two", + .package = "utils" + ) + selected <- stockplotr:::filter_interactive( + dat, "model", interactive = TRUE, allow_multiple_selection = FALSE + ) + expect_equal(unique(selected$plot_data$model), "model_two") + expect_equal(selected$selected, "model_two") + + modules <- make_utils_plot_multimodule_data() + selected <- stockplotr:::filter_interactive( + modules, "module_name", interactive = FALSE, allow_multiple_selection = TRUE + ) + expect_equal(unique(selected$plot_data$module_name), "TIME_SERIES") +}) + test_that("filter_data uses the first module when selection is empty", { testthat::local_mocked_bindings( interactive = function() TRUE,