From 653cf038d7397aa44d55c8cfb71a9214958113c3 Mon Sep 17 00:00:00 2001 From: vedhav Date: Wed, 29 Jan 2025 16:19:14 +0530 Subject: [PATCH 1/2] chore: move the reporter module creation to `module_teal` --- R/init.R | 8 ------- R/module_teal.R | 18 ++++++++++++++++ tests/testthat/test-module_teal.R | 36 +++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) 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..1f440ce2c 100644 --- a/R/module_teal.R +++ b/R/module_teal.R @@ -45,6 +45,20 @@ #' @return `NULL` invisibly NULL +#' @rdname module_teal +#' @keywords internal +#' @noRd +append_reporter_module <- function(modules) { + 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"))) + ) + } + modules +} + #' @rdname module_teal #' @export ui_teal <- function(id, modules) { @@ -52,6 +66,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 +130,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.") 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", { From eed27bdb9cf4905fa937a483cbfd8839757f0d2b Mon Sep 17 00:00:00 2001 From: Dawid Kaledkowski Date: Wed, 29 Jan 2025 12:32:57 +0100 Subject: [PATCH 2/2] initialize reporter object append_reporter_module to modules.R --- R/module_teal.R | 18 +++--------------- R/modules.R | 13 +++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/R/module_teal.R b/R/module_teal.R index 1f440ce2c..bbf6d7cc3 100644 --- a/R/module_teal.R +++ b/R/module_teal.R @@ -45,20 +45,6 @@ #' @return `NULL` invisibly NULL -#' @rdname module_teal -#' @keywords internal -#' @noRd -append_reporter_module <- function(modules) { - 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"))) - ) - } - modules -} - #' @rdname module_teal #' @export ui_teal <- function(id, modules) { @@ -236,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( @@ -244,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`.