Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the reporter module creation to module_teal #1468

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 7 additions & 1 deletion R/module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.")

Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions R/modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
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
Loading