diff --git a/DESCRIPTION b/DESCRIPTION index 241ddf308..d66519916 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,15 +31,29 @@ Depends: teal.transform (>= 0.5.0) Imports: checkmate (>= 2.1.0), + colourpicker, dplyr (>= 1.0.5), DT (>= 0.13), forcats (>= 1.0.0), + generics, + ggExtra, + ggpmisc (>= 0.4.3), + ggpp, + ggrepel, + goftest, grid, + gridExtra, + htmlwidgets, + jsonlite, + lattice (>= 0.18-4), + MASS, + rtables (>= 0.6.8), scales, shinyjs, shinyTree (>= 0.2.8), shinyvalidate, shinyWidgets (>= 0.5.1), + sparkline, stats, stringr (>= 1.4.1), teal.code (>= 0.5.0), @@ -53,28 +67,14 @@ Imports: tools, utils Suggests: - broom (>= 0.7.10), - colourpicker, - ggExtra, - ggpmisc (>= 0.4.3), - ggpp, - ggrepel, - goftest, - gridExtra, - htmlwidgets, - jsonlite, knitr (>= 1.42), - lattice (>= 0.18-4), logger (>= 0.2.0), - MASS, nestcolor (>= 0.1.0), pkgload, rlang (>= 1.0.0), rmarkdown (>= 2.23), - rtables (>= 0.6.8), rvest, shinytest2, - sparkline, testthat (>= 3.1.9), withr (>= 2.0.0) VignetteBuilder: @@ -88,10 +88,9 @@ Config/Needs/verdepcheck: haleyjeppson/ggmosaic, tidyverse/ggplot2, insightsengineering/teal.data, insightsengineering/teal.logger, insightsengineering/teal.reporter, insightsengineering/teal.widgets, insightsengineering/tern, tidyverse/tibble, tidyverse/tidyr, - tidymodels/broom, daattali/colourpicker, daattali/ggExtra, - aphalo/ggpmisc, aphalo/ggpp, slowkow/ggrepel, baddstats/goftest, - gridExtra, ramnathv/htmlwidgets, jeroen/jsonlite, yihui/knitr, - daroczig/logger, deepayan/lattice, MASS, + daattali/colourpicker, daattali/ggExtra, aphalo/ggpmisc, aphalo/ggpp, + slowkow/ggrepel, baddstats/goftest, gridExtra, ramnathv/htmlwidgets, + jeroen/jsonlite, yihui/knitr, daroczig/logger, deepayan/lattice, MASS, insightsengineering/nestcolor, r-lib/rlang, rstudio/rmarkdown, insightsengineering/rtables, tidyverse/rvest, sparkline, rstudio/shinytest2, r-lib/testthat, r-lib/withr diff --git a/R/tm_g_distribution.R b/R/tm_g_distribution.R index 390ab4c88..17e04e479 100644 --- a/R/tm_g_distribution.R +++ b/R/tm_g_distribution.R @@ -121,16 +121,6 @@ tm_g_distribution <- function(label = "Distribution Module", post_output = NULL) { message("Initializing tm_g_distribution") - # Requires Suggested packages - extra_packages <- c("ggpmisc", "ggpp", "goftest", "MASS", "broom") - missing_packages <- Filter(function(x) !requireNamespace(x, quietly = TRUE), extra_packages) - if (length(missing_packages) > 0L) { - stop(sprintf( - "Cannot load package(s): %s.\nInstall or restart your session.", - toString(missing_packages) - )) - } - # Normalize the parameters if (inherits(dist_var, "data_extract_spec")) dist_var <- list(dist_var) if (inherits(strata_var, "data_extract_spec")) strata_var <- list(strata_var) @@ -1176,7 +1166,7 @@ srv_distribution <- function(id, expr = { test_stats <- ANL %>% dplyr::select(dist_var) %>% - with(., broom::glance(do.call(test, args))) %>% + with(., generics::glance(do.call(test, args))) %>% dplyr::mutate_if(is.numeric, round, 3) }, env = env @@ -1190,7 +1180,7 @@ srv_distribution <- function(id, test_stats <- ANL %>% dplyr::select(dist_var, s_var, g_var) %>% dplyr::group_by_at(dplyr::vars(dplyr::any_of(groups))) %>% - dplyr::do(tests = broom::glance(do.call(test, args))) %>% + dplyr::do(tests = generics::glance(do.call(test, args))) %>% tidyr::unnest(tests) %>% dplyr::mutate_if(is.numeric, round, 3) }, diff --git a/R/tm_g_scatterplot.R b/R/tm_g_scatterplot.R index 066ee5506..4c804f073 100644 --- a/R/tm_g_scatterplot.R +++ b/R/tm_g_scatterplot.R @@ -229,16 +229,6 @@ tm_g_scatterplot <- function(label = "Scatterplot", ggplot2_args = teal.widgets::ggplot2_args()) { message("Initializing tm_g_scatterplot") - # Requires Suggested packages - extra_packages <- c("ggpmisc", "ggExtra", "colourpicker") - missing_packages <- Filter(function(x) !requireNamespace(x, quietly = TRUE), extra_packages) - if (length(missing_packages) > 0L) { - stop(sprintf( - "Cannot load package(s): %s.\nInstall or restart your session.", - toString(missing_packages) - )) - } - # Normalize the parameters if (inherits(x, "data_extract_spec")) x <- list(x) if (inherits(y, "data_extract_spec")) y <- list(y) diff --git a/R/tm_g_scatterplotmatrix.R b/R/tm_g_scatterplotmatrix.R index e8c7ce3b9..2f857afcc 100644 --- a/R/tm_g_scatterplotmatrix.R +++ b/R/tm_g_scatterplotmatrix.R @@ -164,11 +164,6 @@ tm_g_scatterplotmatrix <- function(label = "Scatterplot Matrix", post_output = NULL) { message("Initializing tm_g_scatterplotmatrix") - # Requires Suggested packages - if (!requireNamespace("lattice", quietly = TRUE)) { - stop("Cannot load lattice - please install the package or restart your session.") - } - # Normalize the parameters if (inherits(variables, "data_extract_spec")) variables <- list(variables) diff --git a/R/tm_missing_data.R b/R/tm_missing_data.R index 7af029e74..0f6c7b578 100644 --- a/R/tm_missing_data.R +++ b/R/tm_missing_data.R @@ -91,14 +91,6 @@ tm_missing_data <- function(label = "Missing data", post_output = NULL) { message("Initializing tm_missing_data") - # Requires Suggested packages - if (!requireNamespace("gridExtra", quietly = TRUE)) { - stop("Cannot load gridExtra - please install the package or restart your session.") - } - if (!requireNamespace("rlang", quietly = TRUE)) { - stop("Cannot load rlang - please install the package or restart your session.") - } - # Normalize the parameters if (inherits(ggplot2_args, "ggplot2_args")) ggplot2_args <- list(default = ggplot2_args) @@ -1135,6 +1127,13 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par ggtheme = input$ggtheme ) + # Unlikely that `rlang` is not available, new hashing may be expensive + hashing_function <- if (requireNamespace("rlang", quietly = TRUE)) { + quote(rlang::hash) + } else { + function(x) paste(as.integer(x), collapse = "") + } + teal.code::eval_code( common_code_q(), substitute( @@ -1149,41 +1148,44 @@ srv_missing_data <- function(id, data, reporter, filter_panel_api, dataname, par ) ) %>% teal.code::eval_code( - quote({ - summary_plot_patients <- ANL[, c(parent_keys, analysis_vars)] %>% - dplyr::group_by_at(parent_keys) %>% - dplyr::mutate(id = dplyr::cur_group_id()) %>% - dplyr::ungroup() %>% - dplyr::group_by_at(c(parent_keys, "id")) %>% - dplyr::summarise_all(anyNA) %>% - dplyr::ungroup() - - # order subjects by decreasing number of missing and then by - # missingness pattern (defined using sha1) - order_subjects <- summary_plot_patients %>% - dplyr::select(-"id", -dplyr::all_of(parent_keys)) %>% - dplyr::transmute( - id = dplyr::row_number(), - number_NA = apply(., 1, sum), - sha = apply(., 1, rlang::hash) - ) %>% - dplyr::arrange(dplyr::desc(number_NA), sha) %>% - getElement(name = "id") - - # order columns by decreasing percent of missing values - ordered_columns <- summary_plot_patients %>% - dplyr::select(-"id", -dplyr::all_of(parent_keys)) %>% - dplyr::summarise( - column = create_cols_labels(colnames(.)), - na_count = apply(., MARGIN = 2, FUN = sum), - na_percent = na_count / nrow(.) * 100 - ) %>% - dplyr::arrange(na_percent, dplyr::desc(column)) - - summary_plot_patients <- summary_plot_patients %>% - tidyr::gather("col", "isna", -"id", -dplyr::all_of(parent_keys)) %>% - dplyr::mutate(col = create_cols_labels(col)) - }) + substitute( + expr = { + summary_plot_patients <- ANL[, c(parent_keys, analysis_vars)] %>% + dplyr::group_by_at(parent_keys) %>% + dplyr::mutate(id = dplyr::cur_group_id()) %>% + dplyr::ungroup() %>% + dplyr::group_by_at(c(parent_keys, "id")) %>% + dplyr::summarise_all(anyNA) %>% + dplyr::ungroup() + + # order subjects by decreasing number of missing and then by + # missingness pattern (defined using sha1) + order_subjects <- summary_plot_patients %>% + dplyr::select(-"id", -dplyr::all_of(parent_keys)) %>% + dplyr::transmute( + id = dplyr::row_number(), + number_NA = apply(., 1, sum), + sha = apply(., 1, hashing_function) + ) %>% + dplyr::arrange(dplyr::desc(number_NA), sha) %>% + getElement(name = "id") + + # order columns by decreasing percent of missing values + ordered_columns <- summary_plot_patients %>% + dplyr::select(-"id", -dplyr::all_of(parent_keys)) %>% + dplyr::summarise( + column = create_cols_labels(colnames(.)), + na_count = apply(., MARGIN = 2, FUN = sum), + na_percent = na_count / nrow(.) * 100 + ) %>% + dplyr::arrange(na_percent, dplyr::desc(column)) + + summary_plot_patients <- summary_plot_patients %>% + tidyr::gather("col", "isna", -"id", -dplyr::all_of(parent_keys)) %>% + dplyr::mutate(col = create_cols_labels(col)) + }, + env = list(hashing_function = hashing_function) + ) ) %>% teal.code::eval_code( substitute( diff --git a/R/tm_t_crosstable.R b/R/tm_t_crosstable.R index d4e3b4700..728fbca2e 100644 --- a/R/tm_t_crosstable.R +++ b/R/tm_t_crosstable.R @@ -140,11 +140,6 @@ tm_t_crosstable <- function(label = "Cross Table", basic_table_args = teal.widgets::basic_table_args()) { message("Initializing tm_t_crosstable") - # Requires Suggested packages - if (!requireNamespace("rtables", quietly = TRUE)) { - stop("Cannot load rtables - please install the package or restart your session.") - } - # Normalize the parameters if (inherits(x, "data_extract_spec")) x <- list(x) if (inherits(y, "data_extract_spec")) y <- list(y) diff --git a/R/tm_variable_browser.R b/R/tm_variable_browser.R index 626822d3d..4dec3ab26 100644 --- a/R/tm_variable_browser.R +++ b/R/tm_variable_browser.R @@ -78,17 +78,6 @@ tm_variable_browser <- function(label = "Variable Browser", ggplot2_args = teal.widgets::ggplot2_args()) { message("Initializing tm_variable_browser") - # Requires Suggested packages - if (!requireNamespace("sparkline", quietly = TRUE)) { - stop("Cannot load sparkline - please install the package or restart your session.") - } - if (!requireNamespace("htmlwidgets", quietly = TRUE)) { - stop("Cannot load htmlwidgets - please install the package or restart your session.") - } - if (!requireNamespace("jsonlite", quietly = TRUE)) { - stop("Cannot load jsonlite - please install the package or restart your session.") - } - # Start of assertions checkmate::assert_string(label) checkmate::assert_character(datasets_selected) @@ -1280,7 +1269,6 @@ create_sparklines.default <- function(arr, width = 150, ...) { as.character(tags$code("unsupported variable type", class = "text-blue")) } - custom_sparkline_formatter <- function(labels, counts) { htmlwidgets::JS( sprintf( diff --git a/tests/testthat/setup-logger.R b/tests/testthat/setup-logger.R index f6401f548..69c1394c7 100644 --- a/tests/testthat/setup-logger.R +++ b/tests/testthat/setup-logger.R @@ -1 +1,3 @@ -logger::log_appender(function(...) NULL, namespace = "teal.modules.general") +if (requireNamespace("logger", quietly = TRUE)) { + logger::log_appender(function(...) NULL, namespace = "teal.modules.general") +} diff --git a/tests/testthat/test-examples.R b/tests/testthat/test-examples.R index 7acbd0a6d..a82ee89dc 100644 --- a/tests/testthat/test-examples.R +++ b/tests/testthat/test-examples.R @@ -145,6 +145,7 @@ for (i in rd_files()) { paste0("example-", basename(i)), { skip_if_too_deep(5) + testthat::skip_if_not_installed("pkgload") if (basename(i) %in% strict_exceptions) { op <- options() withr::local_options(opts_partial_match_old) diff --git a/tests/testthat/test-shinytest2-tm_data_table.R b/tests/testthat/test-shinytest2-tm_data_table.R index c6330d43d..7920c0c90 100644 --- a/tests/testthat/test-shinytest2-tm_data_table.R +++ b/tests/testthat/test-shinytest2-tm_data_table.R @@ -44,6 +44,7 @@ test_that("e2e - tm_data_table: Verify checkbox displayed over data table", { }) test_that("e2e - tm_data_table: Verify module displays data table", { + testthat::skip_if_not_installed("rvest") skip_if_too_deep(5) app_driver <- app_driver_tm_data_table() diff --git a/tests/testthat/test-shinytest2-tm_file_viewer.R b/tests/testthat/test-shinytest2-tm_file_viewer.R index 7a6eb12c6..a55685aad 100644 --- a/tests/testthat/test-shinytest2-tm_file_viewer.R +++ b/tests/testthat/test-shinytest2-tm_file_viewer.R @@ -17,6 +17,7 @@ app_driver_tm_file_viewer <- function() { } test_that("e2e - tm_file_viewer: Initializes without errors and shows files tree specified in input_path argument", { + testthat::skip_if_not_installed("rvest") skip_if_too_deep(5) app_driver <- app_driver_tm_file_viewer() @@ -42,6 +43,7 @@ test_that("e2e - tm_file_viewer: Initializes without errors and shows files tree }) test_that("e2e - tm_file_viewer: Shows selected image file", { + testthat::skip_if_not_installed("rvest") skip_if_too_deep(5) app_driver <- app_driver_tm_file_viewer() @@ -58,6 +60,7 @@ test_that("e2e - tm_file_viewer: Shows selected image file", { }) test_that("e2e - tm_file_viewer: Shows selected text file", { + testthat::skip_if_not_installed("rvest") skip_if_too_deep(5) app_driver <- app_driver_tm_file_viewer() diff --git a/tests/testthat/test-shinytest2-tm_misssing_data.R b/tests/testthat/test-shinytest2-tm_misssing_data.R index af35629ab..b1ea91429 100644 --- a/tests/testthat/test-shinytest2-tm_misssing_data.R +++ b/tests/testthat/test-shinytest2-tm_misssing_data.R @@ -147,6 +147,7 @@ test_that("e2e - tm_missing_data: Validate functionality and UI response for 'By }) test_that("e2e - tm_missing_data: Validate 'By Variable Levels' table values", { + testthat::skip_if_not_installed("rvest") skip_if_too_deep(5) app_driver <- app_driver_tm_missing_data()