diff --git a/R/init.R b/R/init.R index 252e6f6c6..24f4bc74d 100644 --- a/R/init.R +++ b/R/init.R @@ -178,14 +178,6 @@ init <- function(data, } } - reporter <- teal.reporter::Reporter$new()$set_id(attr(filter, "app_id")) - if (is_arg_used(modules, "reporter") && length(extract_module(modules, "teal_module_previewer")) == 0) { - modules <- append_module( - modules, - reporter_previewer_module(server_args = list(previewer_buttons = c("download", "reset"))) - ) - } - # argument transformations ## `modules` - landing module landing <- extract_module(modules, "teal_module_landing") diff --git a/R/module_teal.R b/R/module_teal.R index d78e7eb94..bbf6d7cc3 100644 --- a/R/module_teal.R +++ b/R/module_teal.R @@ -52,6 +52,8 @@ ui_teal <- function(id, modules) { checkmate::assert_class(modules, "teal_modules") ns <- NS(id) + modules <- append_reporter_module(modules) + # show busy icon when `shiny` session is busy computing stuff # based on https://stackoverflow.com/questions/17325521/r-shiny-display-loading-message-while-function-is-running/22475216#22475216 # nolint: line_length. shiny_busy_message_panel <- conditionalPanel( @@ -114,6 +116,8 @@ srv_teal <- function(id, data, modules, filter = teal_slices()) { checkmate::assert_class(modules, "teal_modules") checkmate::assert_class(filter, "teal_slices") + modules <- append_reporter_module(modules) + moduleServer(id, function(input, output, session) { logger::log_debug("srv_teal initializing.") @@ -218,6 +222,7 @@ srv_teal <- function(id, data, modules, filter = teal_slices()) { ) } + reporter <- teal.reporter::Reporter$new()$set_id(attr(filter, "app_id")) module_labels <- unlist(module_labels(modules), use.names = FALSE) slices_global <- methods::new(".slicesGlobal", filter, module_labels) modules_output <- srv_teal_module( @@ -226,7 +231,8 @@ srv_teal <- function(id, data, modules, filter = teal_slices()) { datasets = datasets_rv, modules = modules, slices_global = slices_global, - data_load_status = data_load_status + data_load_status = data_load_status, + reporter = reporter ) mapping_table <- srv_filter_manager_panel("filter_manager_panel", slices_global = slices_global) snapshots <- srv_snapshot_manager_panel("snapshot_manager_panel", slices_global = slices_global) diff --git a/R/modules.R b/R/modules.R index b314e8ccd..d9aac6de0 100644 --- a/R/modules.R +++ b/R/modules.R @@ -641,6 +641,19 @@ append_module <- function(modules, module) { modules } +#' @rdname module_teal +#' @keywords internal +#' @noRd +append_reporter_module <- function(modules) { + if (is_arg_used(modules, "reporter") && length(extract_module(modules, "teal_module_previewer")) == 0) { + modules <- append_module( + modules, + reporter_previewer_module(server_args = list(previewer_buttons = c("download", "reset"))) + ) + } + modules +} + #' Extract/Remove module(s) of specific class #' #' Given a `teal_module` or a `teal_modules`, return the elements of the structure according to `class`. diff --git a/tests/testthat/test-module_teal.R b/tests/testthat/test-module_teal.R index a25eaec54..b09b499c1 100644 --- a/tests/testthat/test-module_teal.R +++ b/tests/testthat/test-module_teal.R @@ -1045,6 +1045,42 @@ testthat::describe("srv_teal teal_modules", { } ) }) + + testthat::it("does not receive report_previewer when none of the modules contain reporter argument", { + shiny::testServer( + app = srv_teal, + args = list( + id = "test", + data = teal.data::teal_data(iris = iris, mtcars = mtcars), + modules = modules( + module("module_1", server = function(id) {}), + module("module_2", server = function(id) {}) + ) + ), + expr = { + session$setInputs(`teal_modules-active_tab` = "report_previewer") + testthat::expect_setequal(names(modules_output), c("module_1", "module_2")) + } + ) + }) + + testthat::it("receives one report_previewer module when any module contains reporter argument", { + shiny::testServer( + app = srv_teal, + args = list( + id = "test", + data = teal.data::teal_data(iris = iris, mtcars = mtcars), + modules = modules( + module("module_1", server = function(id, reporter) {}), + module("module_2", server = function(id) {}) + ) + ), + expr = { + session$setInputs(`teal_modules-active_tab` = "report_previewer") + testthat::expect_setequal(names(modules_output), c("module_1", "module_2", "report_previewer")) + } + ) + }) }) testthat::describe("srv_teal filters", {