From 4ea8847f5b6c970885d937034baa50c5f2f6f2e0 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA <181890943+sbreitbart-NOAA@users.noreply.github.com> Date: Tue, 1 Dec 2026 19:38:21 +0000 Subject: [PATCH] style and docs: run devtools::document() and style with styler or air --- DESCRIPTION | 2 +- R/conout_bam.R | 18 +++++++---- R/conout_rceattle.R | 4 +-- R/conout_ss3.R | 7 ++-- R/convert_output.R | 32 +++++++++---------- R/extract_sis_data.R | 6 ++-- R/plot_age_to_length_conversion.R | 25 +++++++-------- R/utils_plot.R | 24 +++++++------- .../fixtures/prepare_rceattle_output_file.R | 2 +- tests/testthat/test-convert_output.R | 2 +- tests/testthat/test-plot-age-to-length.R | 4 +-- tests/testthat/test-plot_spawning_biomass.R | 11 ++++--- tests/testthat/test-plot_stock_recruitment.R | 3 +- tests/testthat/test-utils_plot.R | 12 ++++--- 14 files changed, 82 insertions(+), 70 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index fb76a855..dec12ed3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -68,7 +68,7 @@ VignetteBuilder: Remotes: NOAA-FIMS/FIMS, r4ss/r4ss -Config/roxygen2/version: 8.1.1 +Config/roxygen2/version: 8.1.2 Config/testthat/edition: 3 Config/testthat/parallel: true Config/testthat/start-first: save_all_plots*, convert_output* diff --git a/R/conout_bam.R b/R/conout_bam.R index 8d03dc15..cf5ddaad 100644 --- a/R/conout_bam.R +++ b/R/conout_bam.R @@ -141,19 +141,25 @@ conout_bam <- function( if ("age" %in% colnames(df2)) { if (all(is.na(df2$age))) { df2 <- df2 |> - dplyr::mutate(age = age_a, - age_n = NA) + dplyr::mutate( + age = age_a, + age_n = NA + ) } # check if age column values are equal to age_a if (all(df2$age == df2$age_a, na.rm = TRUE)) { df2 <- df2 |> - dplyr::mutate(age = age_a, - age_n = NA) + dplyr::mutate( + age = age_a, + age_n = NA + ) } } else { df2 <- df2 |> - dplyr::mutate(age = age_a, - age_a = NA) + dplyr::mutate( + age = age_a, + age_a = NA + ) } if (all(is.na(df2$age_a))) df2 <- dplyr::select(df2, -age_a) } diff --git a/R/conout_rceattle.R b/R/conout_rceattle.R index 006b1e8b..a3e1aeb6 100644 --- a/R/conout_rceattle.R +++ b/R/conout_rceattle.R @@ -382,10 +382,10 @@ conout_rceattle <- function( module_name = module_name, len_bins = as.numeric(stringr::str_remove(len_bins, "length_")) ) - + df_alk[setdiff(tolower(names(out_new)), tolower(names(df_alk)))] <- NA data_list_list[[module_name]] <- df_alk - + # final df for data_list module # rbind() rather than bind_rows(): every frame has already been padded # to the standard column set above, so rbind() succeeds and keeps the diff --git a/R/conout_ss3.R b/R/conout_ss3.R index 01bf08a9..b3c76f2f 100644 --- a/R/conout_ss3.R +++ b/R/conout_ss3.R @@ -995,7 +995,7 @@ conout_ss3 <- function( # ID dimensions and assign df1 <- as.data.frame.table(extract, responseName = "value", stringsAsFactors = FALSE) colnames(df1)[1:3] <- c("len_bins", "age", "slice_label") - + # Dynamically extract all "Key: Value" pairs and pivot df2 <- df1 |> # Match every 'Word: Number' pair (supporting decimals and negative numbers) @@ -1021,7 +1021,7 @@ conout_ss3 <- function( # I believe this is the only ones rename_map <- c( season = "seas", - subseason = "sub_seas" , + subseason = "sub_seas", estimate = "value" # gp = "growth_pattern" ) @@ -1029,7 +1029,8 @@ conout_ss3 <- function( dplyr::rename(dplyr::any_of(rename_map)) |> dplyr::mutate( module_name = parm_sel, - label = "age_to_length_conversion") + label = "age_to_length_conversion" + ) df3[setdiff(tolower(names(out_new)), tolower(names(df3)))] <- NA # Add to out list out_list[[parm_sel]] <- df3 diff --git a/R/convert_output.R b/R/convert_output.R index 1b59fab3..c41b7461 100644 --- a/R/convert_output.R +++ b/R/convert_output.R @@ -9,8 +9,8 @@ #' appended together. #' #' `names(files)` will be used as the name of the model in the output. -#' -#' @param file `r lifecycle::badge('deprecated')` +#' +#' @param file `r lifecycle::badge('deprecated')` #' This argument was deprecated in favor of the `files` argument in order #' to clarify that multiple files can be processed simultaneously. #' @@ -21,8 +21,8 @@ #' Options: "ss3", "bam", "fims", "rceattle" #' #' Should match length of `files` if >1 model. -#' -#' @param model `r lifecycle::badge('deprecated')` +#' +#' @param model `r lifecycle::badge('deprecated')` #' This argument was deprecated in favor of the `models` argument in order #' to clarify that multiple models can be processed simultaneously. #' @@ -99,26 +99,26 @@ convert_output <- function( # check for used deprecated arguments if (lifecycle::is_present(file)) { lifecycle::deprecate_warn( - when = "0.16.0", - what = "convert_output(file)", + when = "0.16.0", + what = "convert_output(file)", details = "Please use `files` instead of `file`" ) - + files <- file } if (lifecycle::is_present(model)) { lifecycle::deprecate_warn( - when = "0.16.0", - what = "convert_output(model)", + when = "0.16.0", + what = "convert_output(model)", details = "Please use `models` instead of `model`" ) - + models <- model } # Is user inputs a single model file and doesn't wrap it as a list object # First check handles WHAM and Rceattle models, second check handles single model input, # final check handles FIMS models - if(any(grepl("Data_File|data_list|info|TMBfilename", names(files))) || is.character(files) || is.data.frame(files)){ + if (any(grepl("Data_File|data_list|info|TMBfilename", names(files))) || is.character(files) || is.data.frame(files)) { cli::cli_alert("`files` is a single model. Converting to list object.") files <- list(files) names(files) <- "model_1" @@ -234,8 +234,7 @@ convert_output <- function( } if (is.character(file)) { if (is.null(models) || is.na(models[i])) { - model <- switch( - stringr::str_extract(file, "\\.([^.]+)$"), + model <- switch(stringr::str_extract(file, "\\.([^.]+)$"), ".sso" = { cli::cli_alert_info("Processing Stock Synthesis output file...") "ss3" @@ -257,9 +256,8 @@ convert_output <- function( } else { model <- models[i] } - } else if (is.null(models) || is.na(models[i])){ - model <- switch( - class(file)[1], + } else if (is.null(models) || is.na(models[i])) { + model <- switch(class(file)[1], "FIMSFit" = { cli::cli_alert_info("Processing FIMS output file...") "fims" @@ -286,7 +284,7 @@ convert_output <- function( }, cli::cli_abort("Unknown file type. Please indicate model.") ) - } else{ + } else { model <- models[i] } diff --git a/R/extract_sis_data.R b/R/extract_sis_data.R index df901ebb..0b8d7afa 100644 --- a/R/extract_sis_data.R +++ b/R/extract_sis_data.R @@ -22,10 +22,10 @@ #' #' @details See the "Automating data extraction for SIS" vignette for a #' detailed overview of the workflow and requirements for this function. -#' +#' #' Note that if you have created figures and tables using a subset of data, and your rda and -#' key_quantities.csv files represent that subsetted data, the data extracted with this -#' function will also be subsetted. To extract data from the full dataset, +#' key_quantities.csv files represent that subsetted data, the data extracted with this +#' function will also be subsetted. To extract data from the full dataset, #' create new figures and tables and rerun this function. #' #' @export diff --git a/R/plot_age_to_length_conversion.R b/R/plot_age_to_length_conversion.R index dd943f85..c1ba20c2 100644 --- a/R/plot_age_to_length_conversion.R +++ b/R/plot_age_to_length_conversion.R @@ -2,7 +2,7 @@ #' #' Creates a density plot of age-to-length conversion, either fixed input or #' realized growth-derived output. -#' +#' #' @inheritParams plot_spawning_biomass #' @param ... Arguments called from \link[ggridges]{geom_density_ridges} #' @examples @@ -45,17 +45,17 @@ plot_age_to_length_conversion <- function( dat = prepared_data, facet = facet ) - + plot_data <- processing[[1]] group <- processing[[2]] if (!is.null(processing[[3]])) facet <- processing[[3]] - if(group %in% c("age", "length_bins")){ + if (group %in% c("age", "length_bins")) { group <- NULL } else { facet <- c(facet, group) group <- NULL } - if(any(facet %in% c("age", "length_bins"))) { + if (any(facet %in% c("age", "length_bins"))) { facet <- facet[-grep("age|length_bins", facet)] } # Override grouping variable when there is only NA's @@ -73,20 +73,19 @@ plot_age_to_length_conversion <- function( # add check in case facet is returned as character(0) facet = if (length(facet) > 0) facet else NULL, ... - ) + - theme_noaa() - + ) + + theme_noaa() - ### Make RDA ---- - if (make_rda) { + ### Make RDA ---- + if (make_rda) { plot_data <- plot_data |> dplyr::mutate( age = as.numeric(age), length_bins = as.numeric(length_bins) ) - #add any additional key quantities or processing needed before creating the RDA + # add any additional key quantities or processing needed before creating the RDA age.length.group <- glue::glue_collapse(facet, sep = ", ", last = " and ") length.units <- unit_label age.length.age.min <- min(plot_data[["age"]], na.rm = TRUE) @@ -219,12 +218,12 @@ plot_age_to_length_conversion <- function( dat = plot_data, dir = figures_dir ) - } #end make_rda - + } # end make_rda + if (exists("selected_module")) { rm(selected_module, envir = .GlobalEnv) } - + # Output final plot plt } diff --git a/R/utils_plot.R b/R/utils_plot.R index 2879ebe8..1a37f85f 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -1379,13 +1379,13 @@ plot_density <- function( plot <- ggplot2::ggplot() # Add geom - plot <- plot + + plot <- plot + ggridges::geom_density_ridges( - data = dat, + data = dat, ggplot2::aes( x = as.integer(.data[[x]]), y = as.integer(.data[[y]]), - weight = .data[["estimate"]], + weight = .data[["estimate"]], group = .data[[y]], color = .data[["model"]], fill = .data[["model"]] @@ -1396,16 +1396,16 @@ plot_density <- function( alpha = 0.8, ... ) + - ggplot2::coord_flip() - + ggplot2::coord_flip() + # Add labels to axis and legend - final <- plot + ggplot2::labs( - x = xlab, - y = ylab, - color = "Model", - fill = "Model" - ) - + final <- plot + ggplot2::labs( + x = xlab, + y = ylab, + color = "Model", + fill = "Model" + ) + # Remove legend if no group is selected if (length(unique(dat$model)) == 1) { final <- final + ggplot2::theme(legend.position = "none") diff --git a/tests/testthat/fixtures/prepare_rceattle_output_file.R b/tests/testthat/fixtures/prepare_rceattle_output_file.R index 8800c5b8..eeac88f2 100644 --- a/tests/testthat/fixtures/prepare_rceattle_output_file.R +++ b/tests/testthat/fixtures/prepare_rceattle_output_file.R @@ -42,7 +42,7 @@ stopifnot( # "index_hat", "catch_hat", "age_hat", "log_index_hat", "sel_at_age", # "M_at_age" # ) -# +# # fixture <- structure( # list( # data_list = full$data_list[intersect(data_list_keep, names(full$data_list))], diff --git a/tests/testthat/test-convert_output.R b/tests/testthat/test-convert_output.R index bd8a13eb..a6e3adf1 100644 --- a/tests/testthat/test-convert_output.R +++ b/tests/testthat/test-convert_output.R @@ -73,7 +73,7 @@ test_that("missing arguments trigger warnings or errors", { ), "Output file not compatible." ) - + dir.create(fs::path("fixtures", "ss3_models_converted", "Hake_2018"), recursive = TRUE) expect_error( diff --git a/tests/testthat/test-plot-age-to-length.R b/tests/testthat/test-plot-age-to-length.R index d18f4357..c3a0d0ff 100644 --- a/tests/testthat/test-plot-age-to-length.R +++ b/tests/testthat/test-plot-age-to-length.R @@ -19,10 +19,10 @@ test_that("plot_age_to_length_conversion generates plots without errors", { ) ) - expect_no_error( + expect_no_error( plot_age_to_length_conversion( stockplotr::ss3_simple |> - dplyr::filter(age < 10), + dplyr::filter(age < 10), group = NULL, facet = NULL, interactive = FALSE, diff --git a/tests/testthat/test-plot_spawning_biomass.R b/tests/testthat/test-plot_spawning_biomass.R index 8b6fde48..dba9756e 100644 --- a/tests/testthat/test-plot_spawning_biomass.R +++ b/tests/testthat/test-plot_spawning_biomass.R @@ -48,17 +48,19 @@ test_that("plot_spawning_biomass plots all models by default", { ) dat <- dplyr::bind_rows( dplyr::mutate(stockplotr::example_data, model = "model1"), - dplyr::mutate(stockplotr::example_data, model = "model2", estimate=estimate*1.5) + dplyr::mutate(stockplotr::example_data, model = "model2", estimate = estimate * 1.5) ) plot <- plot_spawning_biomass( - dat, module = "TIME_SERIES", unit_label = "mt", + 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", + 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") @@ -75,7 +77,8 @@ test_that("interactive model selection limits the plot", { dplyr::mutate(stockplotr::example_data, model = "model2") ) plot <- plot_spawning_biomass( - dat, module = "TIME_SERIES", model = NULL, interactive = TRUE, + 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") diff --git a/tests/testthat/test-plot_stock_recruitment.R b/tests/testthat/test-plot_stock_recruitment.R index 96d61af0..12b37a24 100644 --- a/tests/testthat/test-plot_stock_recruitment.R +++ b/tests/testthat/test-plot_stock_recruitment.R @@ -40,7 +40,8 @@ test_that("stock recruitment uses the selected models for both series", { dplyr::mutate(stockplotr::example_data, model = "model2") ) plot <- plot_stock_recruitment( - dat, model = "model2", interactive = FALSE, + dat, + model = "model2", interactive = FALSE, spawning_biomass_label = "mt", recruitment_label = "mt" ) expect_equal(unique(plot$layers[[1]]$data$model), "model2") diff --git a/tests/testthat/test-utils_plot.R b/tests/testthat/test-utils_plot.R index c84fcc3c..81b76572 100644 --- a/tests/testthat/test-utils_plot.R +++ b/tests/testthat/test-utils_plot.R @@ -140,7 +140,8 @@ test_that("model defaults retain all models when the environment is non-interact 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 + 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")) @@ -167,7 +168,8 @@ test_that("all selects every model but remains a literal module name", { 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 + dat, "model", + interactive = FALSE, allow_multiple_selection = FALSE ) expect_equal(unique(selected$plot_data$model), "model_one") expect_equal(selected$selected, "model_one") @@ -178,14 +180,16 @@ test_that("single-model selection and module selection retain their defaults", { .package = "utils" ) selected <- stockplotr:::filter_interactive( - dat, "model", interactive = TRUE, allow_multiple_selection = FALSE + 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 + modules, "module_name", + interactive = FALSE, allow_multiple_selection = TRUE ) expect_equal(unique(selected$plot_data$module_name), "TIME_SERIES") })