Skip to content

Commit

Permalink
chore: move the reporter module creation to module_teal
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhav committed Jan 29, 2025
1 parent d928e40 commit 653cf03
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
8 changes: 0 additions & 8 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
18 changes: 18 additions & 0 deletions R/module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,29 @@
#' @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) {
checkmate::assert_character(id, max.len = 1, any.missing = FALSE)
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(
Expand Down Expand Up @@ -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.")

Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/test-module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 653cf03

Please sign in to comment.