From 98f5c6ceb0dfe94947664d2af43084d2573cff8a Mon Sep 17 00:00:00 2001 From: Patrick Callahan Date: Sun, 17 Nov 2024 15:12:16 +0100 Subject: [PATCH] Run styler on package --- .Rprofile | 4 +- R/mod_distributions_page.R | 3 +- R/mod_plot_distributions.R | 4 +- R/mod_report_generator.R | 103 +++++++++--------- R/mod_sample_probabilities.R | 2 +- R/mod_save_data.R | 51 +++++---- R/mod_simulation_inputs_page.R | 19 ++-- R/mod_start_simulation.R | 2 +- R/run_simulations.R | 69 ++++++------ R/set_ordinalsimr_options.R | 63 +++++------ R/zzz.R | 10 +- man/ordinalsimr-package.Rd | 2 +- man/set_ordinalsimr_options.Rd | 36 +++--- tests/testthat/test-use_ordinalsimr_options.R | 96 ++++++++-------- tests/testthat/test-utils.R | 9 -- tests/testthat/test-zzz.R | 53 +++++---- 16 files changed, 248 insertions(+), 278 deletions(-) diff --git a/.Rprofile b/.Rprofile index a5488e5..5ca0325 100644 --- a/.Rprofile +++ b/.Rprofile @@ -1,11 +1,11 @@ source("renv/activate.R") -options(shiny.autoload.r=FALSE) +options(shiny.autoload.r = FALSE) options( list( ordinalsimr.default_iterations = 20, ordinalsimr.default_size_min = 30, ordinalsimr.default_size_max = 35, ordinalsimr.default_ratio = "50:50", - ordinalsimr.default_distributions = data.frame(c(0.4,0.3,0.3), c(0.8,0.1,0.1)) + ordinalsimr.default_distributions = data.frame(c(0.4, 0.3, 0.3), c(0.8, 0.1, 0.1)) ) ) diff --git a/R/mod_distributions_page.R b/R/mod_distributions_page.R index 66fbdd0..4940ae6 100644 --- a/R/mod_distributions_page.R +++ b/R/mod_distributions_page.R @@ -24,7 +24,8 @@ mod_distributions_page_ui <- function(id) { h6("Confidence Intervals"), !!!mod_plot_distributions_ui("plot_distributions_1")[["ci_inputs"]], h6("Outlier Removal (Plot Only)"), - mod_plot_distributions_ui("plot_distributions_1")[["outlier_input"]]), + mod_plot_distributions_ui("plot_distributions_1")[["outlier_input"]] + ), nav_panel( title = "Power", mod_plot_distributions_ui("plot_distributions_1")[["output_plots"]][["power_plot"]] diff --git a/R/mod_plot_distributions.R b/R/mod_plot_distributions.R index 2b0da1c..020b816 100644 --- a/R/mod_plot_distributions.R +++ b/R/mod_plot_distributions.R @@ -174,7 +174,7 @@ mod_plot_distributions_server <- function(id, p_value_table, n) { alpha = p_val_threshold(), t1_error_confidence_int = input$t1_error_group1_confidence_int ) - } + } }) output$t1_error_group1 <- DT::renderDataTable({ validate( @@ -195,7 +195,6 @@ mod_plot_distributions_server <- function(id, p_value_table, n) { # GROUP 2 TYPE 1 ERROR group2_t1_reactive_table <- reactive({ - if (!is.null(p_value_table$group2_results())) { p_value_table$group2_results() %>% bind_rows() %>% @@ -212,7 +211,6 @@ mod_plot_distributions_server <- function(id, p_value_table, n) { t1_error_confidence_int = input$t1_error_group2_confidence_int ) } - }) output$t1_error_group2 <- DT::renderDataTable({ validate( diff --git a/R/mod_report_generator.R b/R/mod_report_generator.R index a21fa5c..cec4fc4 100644 --- a/R/mod_report_generator.R +++ b/R/mod_report_generator.R @@ -35,7 +35,7 @@ mod_report_generator_server <- function(id, formatted_data, rng_info) { run_info = formatted_data()$comparison_data$run_info, distribution_statistics = formatted_data()$comparison_data$distribution_statistics, distribution_plot = formatted_data()$comparison_data$distribution_plot - ), + ), group1_data = list( run_info = formatted_data()$group1_data$run_info, group1_t1error = formatted_data()$group1_data$group1_t1error @@ -62,60 +62,59 @@ mod_report_generator_server <- function(id, formatted_data, rng_info) { download_counter_zip <- reactiveVal(1) - output$download_report <- downloadHandler( - filename = function() { - paste0("ordinalsimr_session_", strtrim(session$token, 8), - "_download_", download_counter_zip(), - ".zip") - }, - content = function(file) { - - output_folder <- file.path(paste0("ordinalsimr_session_", strtrim(session$token, 8), - "_download_", download_counter_zip())) - dir.create(output_folder, showWarnings = FALSE) - - try({ - write( - custom_report(), - file.path(output_folder, "completed_report.html") - ) - }) - try({ - saveRDS( - formatted_data(), - file.path(output_folder, "ordinalsimr_results.rds") - ) - }) - try({ - write( - readLines(system.file("report_template.Rmd", package = "ordinalsimr")), - file.path(output_folder, "report_template.Rmd") - ) - }) - - zip_files <- c( - file.path(output_folder, "completed_report.html"), - file.path(output_folder, "ordinalsimr_results.rds"), - file.path(output_folder, "report_template.Rmd") - ) - - try({ - utils::zip( - zipfile = file, - files = zip_files - # flags = '-r9Xb' - ) - }) - - unlink(output_folder, recursive = TRUE, force = TRUE) - download_counter_zip(download_counter_zip() + 1) - - }, - contentType = "application/zip" - ) + output$download_report <- downloadHandler( + filename = function() { + paste0( + "ordinalsimr_session_", strtrim(session$token, 8), + "_download_", download_counter_zip(), + ".zip" + ) + }, + content = function(file) { + output_folder <- file.path(paste0( + "ordinalsimr_session_", strtrim(session$token, 8), + "_download_", download_counter_zip() + )) + dir.create(output_folder, showWarnings = FALSE) + + try({ + write( + custom_report(), + file.path(output_folder, "completed_report.html") + ) + }) + try({ + saveRDS( + formatted_data(), + file.path(output_folder, "ordinalsimr_results.rds") + ) + }) + try({ + write( + readLines(system.file("report_template.Rmd", package = "ordinalsimr")), + file.path(output_folder, "report_template.Rmd") + ) + }) + zip_files <- c( + file.path(output_folder, "completed_report.html"), + file.path(output_folder, "ordinalsimr_results.rds"), + file.path(output_folder, "report_template.Rmd") + ) + try({ + utils::zip( + zipfile = file, + files = zip_files + # flags = '-r9Xb' + ) + }) + unlink(output_folder, recursive = TRUE, force = TRUE) + download_counter_zip(download_counter_zip() + 1) + }, + contentType = "application/zip" + ) }) } diff --git a/R/mod_sample_probabilities.R b/R/mod_sample_probabilities.R index 6559ae7..f2da62f 100644 --- a/R/mod_sample_probabilities.R +++ b/R/mod_sample_probabilities.R @@ -11,7 +11,7 @@ mod_sample_probabilities_ui <- function(id) { ns <- NS(id) default_ratio <- tryCatch( - getOption("ordinalsimr.default_ratio", default = "50:50") , + getOption("ordinalsimr.default_ratio", default = "50:50"), error = function(e) { "50:50" } diff --git a/R/mod_save_data.R b/R/mod_save_data.R index 991d8cf..99d14fe 100644 --- a/R/mod_save_data.R +++ b/R/mod_save_data.R @@ -65,34 +65,39 @@ mod_save_data_server <- function(id, input_data, processed_data, rng_info, input h5("Excel Download:"), p("Please install the {writexl} package.") ) - } - }) + } + }) download_counter_excel <- reactiveVal(1) output$save_xlsx <- downloadHandler( - filename = function() { - paste0("data-", Sys.Date(), "-", strtrim(session$token, 6), "-", download_counter_excel(), ".xlsx") - }, - content = function(file) { - writexl::write_xlsx( - list( - distribution_statistics = data_to_save()$comparison_data$distribution_statistics, - comparison_run_info = data_to_save()$comparison_data$run_info, - group1_type1_error = data_to_save()$group1_data$group1_t1error, - group1_run_info = data_to_save()$group1_data$run_info, - group2_type1_error = data_to_save()$group2_data$group2_t1error, - group2_run_info = data_to_save()$group2_data$run_info - ) %>% - lapply(function(x) {if (is.null(x)) {data.frame()} else {x}}), - path = file - ) - # increment download number - download_counter_excel(download_counter_excel() + 1) - } - ) + filename = function() { + paste0("data-", Sys.Date(), "-", strtrim(session$token, 6), "-", download_counter_excel(), ".xlsx") + }, + content = function(file) { + writexl::write_xlsx( + list( + distribution_statistics = data_to_save()$comparison_data$distribution_statistics, + comparison_run_info = data_to_save()$comparison_data$run_info, + group1_type1_error = data_to_save()$group1_data$group1_t1error, + group1_run_info = data_to_save()$group1_data$run_info, + group2_type1_error = data_to_save()$group2_data$group2_t1error, + group2_run_info = data_to_save()$group2_data$run_info + ) %>% + lapply(function(x) { + if (is.null(x)) { + data.frame() + } else { + x + } + }), + path = file + ) + # increment download number + download_counter_excel(download_counter_excel() + 1) + } + ) return(data_to_save) - }) } diff --git a/R/mod_simulation_inputs_page.R b/R/mod_simulation_inputs_page.R index 158d575..91f8286 100644 --- a/R/mod_simulation_inputs_page.R +++ b/R/mod_simulation_inputs_page.R @@ -15,9 +15,8 @@ mod_simulation_inputs_page_ui <- function(id) { layout_columns( fill = TRUE, fillable = TRUE, - col_widths = c(-1,10,-1,-1,10,-1), + col_widths = c(-1, 10, -1, -1, 10, -1), row_heights = c(9, 7), - card( card_header("Data Entry", class = "bg-dark"), layout_sidebar( @@ -37,29 +36,27 @@ mod_simulation_inputs_page_ui <- function(id) { "Core Inputs", card_title("Iterations, Samples, and Tests"), layout_column_wrap( - width = 1/3, - mod_iterations_ui("iterations_1"), - mod_sample_size_ui("sample_size_1"), - mod_sample_probabilities_ui("sample_probabilities_1") + width = 1 / 3, + mod_iterations_ui("iterations_1"), + mod_sample_size_ui("sample_size_1"), + mod_sample_probabilities_ui("sample_probabilities_1") ), layout_column_wrap( width = 1, - mod_select_tests_ui("select_tests_1") - ) + mod_select_tests_ui("select_tests_1") + ) ), - nav_panel( "Type I Erorr", card_title("TI Error by Group"), mod_type_one_error_ui("type_one_error_1") ), - nav_panel( "RNG Options", card_title("Advanced: Random Number Generator Adjustments"), markdown("These Random Number Generators are advanced options, and they use the default values employed by R as of version 4.4. Run `?RNGkind` in an R session to see the associated help file."), layout_column_wrap( - width = 1/3, + width = 1 / 3, !!!mod_rng_option_ui("rng_option_1") ) ) diff --git a/R/mod_start_simulation.R b/R/mod_start_simulation.R index 64db7de..09f4d84 100644 --- a/R/mod_start_simulation.R +++ b/R/mod_start_simulation.R @@ -11,7 +11,7 @@ mod_start_simulation_ui <- function(id) { ns <- NS(id) tagList( actionButton(ns("run_button"), "Run Tests") %>% - tagAppendAttributes(class="btn btn-success") + tagAppendAttributes(class = "btn btn-success") ) } diff --git a/R/run_simulations.R b/R/run_simulations.R index e62f07c..e477b12 100644 --- a/R/run_simulations.R +++ b/R/run_simulations.R @@ -50,43 +50,42 @@ run_simulations <- function(sample_size, sample_prob, prob0, prob1, niter, inclu ) - lapply(sample_size, function(x) { - sample_size_nested <- x - initial_groups <- lapply(1:niter, function(x) { - assign_groups( - sample_size = sample_size_nested, - sample_prob = sample_prob, - prob0 = prob0, prob1 = prob1, - seed = x, - .rng_kind = .rng_kind, - .rng_normal_kind = .rng_normal_kind, - .rng_sample_kind = .rng_sample_kind - ) - }) + lapply(sample_size, function(x) { + sample_size_nested <- x + initial_groups <- lapply(1:niter, function(x) { + assign_groups( + sample_size = sample_size_nested, + sample_prob = sample_prob, + prob0 = prob0, prob1 = prob1, + seed = x, + .rng_kind = .rng_kind, + .rng_normal_kind = .rng_normal_kind, + .rng_sample_kind = .rng_sample_kind + ) + }) - p_values <- initial_groups %>% - sapply(., function(x) ordinal_tests(x[["x"]], x[["y"]], included = included)) %>% - t() + p_values <- initial_groups %>% + sapply(., function(x) ordinal_tests(x[["x"]], x[["y"]], included = included)) %>% + t() - initial_groups_formatted <- lapply(initial_groups, function(groups) { - tibble( - y = list(groups[["y"]]), x = list(groups[["x"]]), - n_null = groups[["n_null"]], n_intervene = groups[["n_intervene"]], - sample_size = groups[["sample_size"]], K = groups[["K"]] - ) - }) %>% - bind_rows() %>% - mutate(run = row_number(), .before = .data$y) - - if (shiny::isRunning()) { - incProgress( - 1/(max(sample_size)-min(sample_size)), - detail = paste("Sample size", sample_size_nested, "completed.") - ) - } - - return(sim_results_table = bind_cols(p_values, initial_groups_formatted)) + initial_groups_formatted <- lapply(initial_groups, function(groups) { + tibble( + y = list(groups[["y"]]), x = list(groups[["x"]]), + n_null = groups[["n_null"]], n_intervene = groups[["n_intervene"]], + sample_size = groups[["sample_size"]], K = groups[["K"]] + ) }) %>% - magrittr::set_names(paste0("sample_size_", sample_size)) + bind_rows() %>% + mutate(run = row_number(), .before = .data$y) + + if (shiny::isRunning()) { + incProgress( + 1 / (max(sample_size) - min(sample_size)), + detail = paste("Sample size", sample_size_nested, "completed.") + ) + } + return(sim_results_table = bind_cols(p_values, initial_groups_formatted)) + }) %>% + magrittr::set_names(paste0("sample_size_", sample_size)) } diff --git a/R/set_ordinalsimr_options.R b/R/set_ordinalsimr_options.R index 504718f..e40d79e 100644 --- a/R/set_ordinalsimr_options.R +++ b/R/set_ordinalsimr_options.R @@ -1,4 +1,3 @@ - #' Set Option Helper #' #' @param option_name name of the ordinalsimr option to set @@ -9,23 +8,23 @@ #' @keywords internal #' .set_options_helper <- function(option_name, value, additional_msg = NULL) { - tryCatch( expr = { - options( rlang::list2({{option_name}} := value)) + options(rlang::list2({{ option_name }} := value)) if (is.null(value)) { NULL } message("Setting ", option_name, " to ", as.character(value)) - if (!is.null(additional_msg)) { message(additional_msg) } + if (!is.null(additional_msg)) { + message(additional_msg) + } }, error = function(e) { invisible() - } + } ) - } @@ -48,24 +47,24 @@ #' # Set the default values for the ordinalsimr Shiny app #' #' set_ordinalsimr_options( -#' default_iterations = 1000, -#' default_size_min = 10, -#' default_size_max = 100, -#' default_ratio = "50:50", -#' default_distributions = data.frame(c(0.4, 0.3, 0.3), c(0.8, 0.1, 0.1)) -#' ) +#' default_iterations = 1000, +#' default_size_min = 10, +#' default_size_max = 100, +#' default_ratio = "50:50", +#' default_distributions = data.frame(c(0.4, 0.3, 0.3), c(0.8, 0.1, 0.1)) +#' ) #' -#' # Values can be either overwritten or unset by setting them to NULL. The Shiny -#' # app still has backup values if these options are not set. Not all arguments -#' # need to be provided +#' # Values can be either overwritten or unset by setting them to NULL. The Shiny +#' # app still has backup values if these options are not set. Not all arguments +#' # need to be provided #' -#' set_ordinalsimr_options( -#' default_iterations = 500, # Ex: update argument -#' default_size_min = NULL, # Ex: unset argument -#' default_size_max = NULL, # Ex: unset argument -#' # default_ratio = NULL, # Ex: arg not provided (by commenting out) -#' default_distributions = NULL -#' ) +#' set_ordinalsimr_options( +#' default_iterations = 500, # Ex: update argument +#' default_size_min = NULL, # Ex: unset argument +#' default_size_max = NULL, # Ex: unset argument +#' # default_ratio = NULL, # Ex: arg not provided (by commenting out) +#' default_distributions = NULL +#' ) #' set_ordinalsimr_options <- function( default_iterations, @@ -73,17 +72,13 @@ set_ordinalsimr_options <- function( default_size_max, default_ratio, default_distributions, - default_entry_rows - ) { - - + default_entry_rows) { .set_options_helper("ordinalsimr.default_iterations", default_iterations) .set_options_helper("ordinalsimr.default_size_min", default_size_min) .set_options_helper("ordinalsimr.default_size_max", default_size_max) .set_options_helper("ordinalsimr.default_ratio", default_ratio) .set_options_helper("ordinalsimr.default_distributions", default_distributions) .set_options_helper("ordinalsimr.default_entry_rows", default_entry_rows, additional_msg = "The ordinalsimr.default_distributions option takes precedence over the ordinalsimr.default_entry_rows option if it has been set.") - } @@ -98,16 +93,14 @@ set_ordinalsimr_options <- function( #' @examples #' get_ordinalsimr_options() get_ordinalsimr_options <- function() { - - c("ordinalsimr.default_iterations", + c( + "ordinalsimr.default_iterations", "ordinalsimr.default_size_min", "ordinalsimr.default_size_max", "ordinalsimr.default_ratio", "ordinalsimr.default_distributions", - "ordinalsimr.default_entry_rows") %>% - stats::setNames(., .) %>% - lapply(function(x) getOption(x)) - + "ordinalsimr.default_entry_rows" + ) %>% + stats::setNames(., .) %>% + lapply(function(x) getOption(x)) } - - diff --git a/R/zzz.R b/R/zzz.R index 924767c..6fd4724 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -11,18 +11,16 @@ #' @keywords internal #' .onLoad <- function(libname, pkgname) { - op <- options() op.ordinalsimr <- list( - ordinalsimr.default_iterations = 1000, - ordinalsimr.default_size_min = 30, - ordinalsimr.default_size_max = 200, - ordinalsimr.default_ratio = "50:50" + ordinalsimr.default_iterations = 1000, + ordinalsimr.default_size_min = 30, + ordinalsimr.default_size_max = 200, + ordinalsimr.default_ratio = "50:50" ) toset <- !(names(op.ordinalsimr) %in% names(op)) if (any(toset)) options(op.ordinalsimr[toset]) # nocov invisible() - } diff --git a/man/ordinalsimr-package.Rd b/man/ordinalsimr-package.Rd index 543f220..dd6b42f 100644 --- a/man/ordinalsimr-package.Rd +++ b/man/ordinalsimr-package.Rd @@ -4,7 +4,7 @@ \name{ordinalsimr-package} \alias{ordinalsimr} \alias{ordinalsimr-package} -\title{ordinalsimr: Compare Ordinal Endpoints Esing Simulations} +\title{ordinalsimr: Compare Ordinal Endpoints Using Simulations} \description{ Simultaneously evaluate multiple ordinal outcome measures. Applied data analysts in particular are faced with uncertainty in choosing appropriate statistical tests for ordinal data. The included Shiny applications allows users to simulate outcomes given different ordinal data distributions. } diff --git a/man/set_ordinalsimr_options.Rd b/man/set_ordinalsimr_options.Rd index 3007cde..1c77d9c 100644 --- a/man/set_ordinalsimr_options.Rd +++ b/man/set_ordinalsimr_options.Rd @@ -37,23 +37,23 @@ Set ordinalsimr Shiny App Default Values # Set the default values for the ordinalsimr Shiny app set_ordinalsimr_options( - default_iterations = 1000, - default_size_min = 10, - default_size_max = 100, - default_ratio = "50:50", - default_distributions = data.frame(c(0.4, 0.3, 0.3), c(0.8, 0.1, 0.1)) - ) - - # Values can be either overwritten or unset by setting them to NULL. The Shiny - # app still has backup values if these options are not set. Not all arguments - # need to be provided - - set_ordinalsimr_options( - default_iterations = 500, # Ex: update argument - default_size_min = NULL, # Ex: unset argument - default_size_max = NULL, # Ex: unset argument - # default_ratio = NULL, # Ex: arg not provided (by commenting out) - default_distributions = NULL - ) + default_iterations = 1000, + default_size_min = 10, + default_size_max = 100, + default_ratio = "50:50", + default_distributions = data.frame(c(0.4, 0.3, 0.3), c(0.8, 0.1, 0.1)) +) + +# Values can be either overwritten or unset by setting them to NULL. The Shiny +# app still has backup values if these options are not set. Not all arguments +# need to be provided + +set_ordinalsimr_options( + default_iterations = 500, # Ex: update argument + default_size_min = NULL, # Ex: unset argument + default_size_max = NULL, # Ex: unset argument + # default_ratio = NULL, # Ex: arg not provided (by commenting out) + default_distributions = NULL +) } diff --git a/tests/testthat/test-use_ordinalsimr_options.R b/tests/testthat/test-use_ordinalsimr_options.R index ef53936..0d0ae6e 100644 --- a/tests/testthat/test-use_ordinalsimr_options.R +++ b/tests/testthat/test-use_ordinalsimr_options.R @@ -1,57 +1,52 @@ - - # write tests for get_ordinalsimr_options() function test_that("get_ordinalsimr_options() returns a list of ordinalsimr options", { - # set options in a withr with_options environment - withr::with_options(list(ordinalsimr.default_iterations = 500, - ordinalsimr.default_size_min = 10, - ordinalsimr.default_size_max = 100, - ordinalsimr.default_ratio = "50:50", - ordinalsimr.default_distributions = data.frame(c(0.4, 0.3, 0.3), c(0.8, 0.1, 0.1)), - ordinalsimr.default_entry_rows = 5), { - - + withr::with_options(list( + ordinalsimr.default_iterations = 500, + ordinalsimr.default_size_min = 10, + ordinalsimr.default_size_max = 100, + ordinalsimr.default_ratio = "50:50", + ordinalsimr.default_distributions = data.frame(c(0.4, 0.3, 0.3), c(0.8, 0.1, 0.1)), + ordinalsimr.default_entry_rows = 5 + ), { options <- get_ordinalsimr_options() expect_true(is.list(options)) expect_equal(length(options), 6) - expect_equal(names(options), c("ordinalsimr.default_iterations", - "ordinalsimr.default_size_min", - "ordinalsimr.default_size_max", - "ordinalsimr.default_ratio", - "ordinalsimr.default_distributions", - "ordinalsimr.default_entry_rows")) + expect_equal(names(options), c( + "ordinalsimr.default_iterations", + "ordinalsimr.default_size_min", + "ordinalsimr.default_size_max", + "ordinalsimr.default_ratio", + "ordinalsimr.default_distributions", + "ordinalsimr.default_entry_rows" + )) expect_equal(options$ordinalsimr.default_iterations, 500) expect_equal(options$ordinalsimr.default_size_min, 10) expect_equal(options$ordinalsimr.default_size_max, 100) expect_equal(options$ordinalsimr.default_ratio, "50:50") expect_equal(options$ordinalsimr.default_distributions, data.frame(c(0.4, 0.3, 0.3), c(0.8, 0.1, 0.1))) expect_equal(options$ordinalsimr.default_entry_rows, 5) - }) - - - }) # write tests for setting the ordinalsimr options with the set_ordinalsimr_options() function test_that("check that set_ordinalsimr_options() works", { - opts <- options() suppressMessages({ - # set options - set_ordinalsimr_options(default_iterations = 1000, - default_size_min = NULL, - default_size_max = NULL, - default_ratio = NULL, - default_distributions = NULL, - default_entry_rows = 10) + set_ordinalsimr_options( + default_iterations = 1000, + default_size_min = NULL, + default_size_max = NULL, + default_ratio = NULL, + default_distributions = NULL, + default_entry_rows = 10 + ) # get options options <- get_ordinalsimr_options() @@ -60,12 +55,14 @@ test_that("check that set_ordinalsimr_options() works", { # check options expect_true(is.list(options)) expect_equal(length(options), 6) - expect_equal(names(options), c("ordinalsimr.default_iterations", - "ordinalsimr.default_size_min", - "ordinalsimr.default_size_max", - "ordinalsimr.default_ratio", - "ordinalsimr.default_distributions", - "ordinalsimr.default_entry_rows")) + expect_equal(names(options), c( + "ordinalsimr.default_iterations", + "ordinalsimr.default_size_min", + "ordinalsimr.default_size_max", + "ordinalsimr.default_ratio", + "ordinalsimr.default_distributions", + "ordinalsimr.default_entry_rows" + )) expect_equal(options$ordinalsimr.default_iterations, 1000) expect_equal(options$ordinalsimr.default_size_min, NULL) expect_equal(options$ordinalsimr.default_size_max, NULL) @@ -76,12 +73,14 @@ test_that("check that set_ordinalsimr_options() works", { # change the options to other values - set_ordinalsimr_options(default_iterations = 500, - default_size_min = 20, - default_size_max = 200, - default_ratio = "60:40", - default_distributions = data.frame(c(0.5, 0.3, 0.2), c(0.7, 0.2, 0.1)), - default_entry_rows = 15) + set_ordinalsimr_options( + default_iterations = 500, + default_size_min = 20, + default_size_max = 200, + default_ratio = "60:40", + default_distributions = data.frame(c(0.5, 0.3, 0.2), c(0.7, 0.2, 0.1)), + default_entry_rows = 15 + ) # check the options again @@ -96,21 +95,16 @@ test_that("check that set_ordinalsimr_options() works", { expect_equal(options$ordinalsimr.default_ratio, "60:40") expect_equal(options$ordinalsimr.default_distributions, data.frame(c(0.5, 0.3, 0.2), c(0.7, 0.2, 0.1))) expect_equal(options$ordinalsimr.default_entry_rows, 15) - }) options(opts) - }) # write tests for the helper function .set_options_helper() test_that("check that .set_options_helper() works", { - - suppressMessages({ - opts <- options() @@ -121,17 +115,15 @@ test_that("check that .set_options_helper() works", { expect_equal(getOption("ordinalsimr.default_iterations"), NULL) expect_message( - .set_options_helper("ordinalsimr.default_size_min", 10, - "The ordinalsimr.default_size_min option has been set to 10.") + .set_options_helper( + "ordinalsimr.default_size_min", 10, + "The ordinalsimr.default_size_min option has been set to 10." + ) ) options(opts) - }) expect_invisible(.set_options_helper(rvafve, 1000)) - - }) - diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index bef0a0e..588361e 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -57,7 +57,6 @@ test_that("data object names are consistent", { test_that("test the plot_power() function", { - plot_obj <- simulation_data_two_groups %>% select(Wilcoxon:`Coin Indep. Test`, sample_size) %>% calculate_power_t2error() %>% @@ -70,16 +69,12 @@ test_that("test the plot_power() function", { expect_match(plot_obj$labels$colour, "Statistical Test") expect_match(plot_obj$labels$ymin, "lower_power_bound") expect_match(plot_obj$labels$ymax, "upper_power_bound") - - - }) test_that("test the plot_distribution_results() function labels", { - plot_obj <- simulation_data_two_groups %>% select(Wilcoxon:`Coin Indep. Test`, sample_size) %>% plot_distribution_results() @@ -88,8 +83,4 @@ test_that("test the plot_distribution_results() function labels", { expect_match(plot_obj$labels$y, "p-value") expect_match(plot_obj$labels$colour, "Statistical Test") expect_match(plot_obj$labels$title, "Mean p-value") - - }) - - diff --git a/tests/testthat/test-zzz.R b/tests/testthat/test-zzz.R index 17a0c16..e8ca76e 100644 --- a/tests/testthat/test-zzz.R +++ b/tests/testthat/test-zzz.R @@ -1,17 +1,11 @@ - - skip_on_cran() test_that("test .onLoad", { - - - if (testthat:::in_rcmd_check() || testthat:::in_covr()) { - indep_session <- callr::r(function() { ordinalsimr_opts_preload <- grep("ordinalsimr.", names(options()), value = TRUE) |> purrr::set_names() |> - purrr::map(~getOption(.x)) + purrr::map(~ getOption(.x)) library(ordinalsimr) @@ -19,11 +13,14 @@ test_that("test .onLoad", { ordinalsimr_opts_postload <- grep("ordinalsimr.", names(options()), value = TRUE) |> purrr::set_names() |> - purrr::map(~getOption(.x)) + purrr::map(~ getOption(.x)) return( - list(ordinalsimr_opts_preload = ordinalsimr_opts_preload, - ordinalsimr_opts_postload = ordinalsimr_opts_postload)) + list( + ordinalsimr_opts_preload = ordinalsimr_opts_preload, + ordinalsimr_opts_postload = ordinalsimr_opts_postload + ) + ) }) @@ -32,10 +29,11 @@ test_that("test .onLoad", { expect_length(indep_session$ordinalsimr_opts_preload, 0) # check that options exist after loading the package - expect_named(indep_session$ordinalsimr_opts_postload, c('ordinalsimr.default_iterations', - 'ordinalsimr.default_ratio', - 'ordinalsimr.default_size_max', - 'ordinalsimr.default_size_min' + expect_named(indep_session$ordinalsimr_opts_postload, c( + "ordinalsimr.default_iterations", + "ordinalsimr.default_ratio", + "ordinalsimr.default_size_max", + "ordinalsimr.default_size_min" )) expect_length(indep_session$ordinalsimr_opts_postload, 4) @@ -43,13 +41,11 @@ test_that("test .onLoad", { expect_equal(indep_session$ordinalsimr_opts_postload$ordinalsimr.default_size_min, 30) expect_equal(indep_session$ordinalsimr_opts_postload$ordinalsimr.default_size_max, 200) expect_equal(indep_session$ordinalsimr_opts_postload$ordinalsimr.default_ratio, "50:50") - } else if(!testthat:::in_rcmd_check()) { - - + } else if (!testthat:::in_rcmd_check()) { indep_session <- callr::r(function() { ordinalsimr_opts_preload <- grep("ordinalsimr.", names(options()), value = TRUE) |> purrr::set_names() |> - purrr::map(~getOption(.x)) + purrr::map(~ getOption(.x)) pkgload::load_all() @@ -57,11 +53,14 @@ test_that("test .onLoad", { ordinalsimr_opts_postload <- grep("ordinalsimr.", names(options()), value = TRUE) |> purrr::set_names() |> - purrr::map(~getOption(.x)) + purrr::map(~ getOption(.x)) return( - list(ordinalsimr_opts_preload = ordinalsimr_opts_preload, - ordinalsimr_opts_postload = ordinalsimr_opts_postload)) + list( + ordinalsimr_opts_preload = ordinalsimr_opts_preload, + ordinalsimr_opts_postload = ordinalsimr_opts_postload + ) + ) }) @@ -70,10 +69,11 @@ test_that("test .onLoad", { expect_length(indep_session$ordinalsimr_opts_preload, 0) # check that options exist after loading the package - expect_named(indep_session$ordinalsimr_opts_postload, c('ordinalsimr.default_iterations', - 'ordinalsimr.default_ratio', - 'ordinalsimr.default_size_max', - 'ordinalsimr.default_size_min' + expect_named(indep_session$ordinalsimr_opts_postload, c( + "ordinalsimr.default_iterations", + "ordinalsimr.default_ratio", + "ordinalsimr.default_size_max", + "ordinalsimr.default_size_min" )) expect_length(indep_session$ordinalsimr_opts_postload, 4) @@ -81,8 +81,5 @@ test_that("test .onLoad", { expect_equal(indep_session$ordinalsimr_opts_postload$ordinalsimr.default_size_min, 30) expect_equal(indep_session$ordinalsimr_opts_postload$ordinalsimr.default_size_max, 200) expect_equal(indep_session$ordinalsimr_opts_postload$ordinalsimr.default_ratio, "50:50") - } - - })