Skip to content

Commit

Permalink
tm_crosstable ready
Browse files Browse the repository at this point in the history
  • Loading branch information
m7pr committed Nov 20, 2024
1 parent 97e60ef commit 51f6882
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions R/tm_t_crosstable.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
#'
#' @inherit shared_params return
#'
#' @section Decorating `tm_t_crosstable`:
#'
#' This module generates the following objects, which can be modified in place using decorators:
#' - `table` (`ElementaryTable` - output of `rtables::build_table`)
#'
#' For additional details and examples of decorators, refer to the vignette
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
#'
#' @examplesShinylive
#' library(teal.modules.general)
#' interactive <- function() TRUE
Expand Down Expand Up @@ -134,7 +142,8 @@ tm_t_crosstable <- function(label = "Cross Table",
show_total = TRUE,
pre_output = NULL,
post_output = NULL,
basic_table_args = teal.widgets::basic_table_args()) {
basic_table_args = teal.widgets::basic_table_args(),
decorators = list(default = teal_transform_module())) {
message("Initializing tm_t_crosstable")

# Requires Suggested packages
Expand All @@ -158,6 +167,7 @@ tm_t_crosstable <- function(label = "Cross Table",
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
checkmate::assert_class(basic_table_args, classes = "basic_table_args")
checkmate::assert_list(decorators, "teal_transform_module")
# End of assertions

# Make UI args
Expand All @@ -167,7 +177,8 @@ tm_t_crosstable <- function(label = "Cross Table",
label = label,
x = x,
y = y,
basic_table_args = basic_table_args
basic_table_args = basic_table_args,
decorators = decorators
)

ans <- module(
Expand All @@ -184,6 +195,7 @@ tm_t_crosstable <- function(label = "Cross Table",

# UI function for the cross-table module
ui_t_crosstable <- function(id, x, y, show_percentage, show_total, pre_output, post_output, ...) {
args <- list(...)
ns <- NS(id)
is_single_dataset <- teal.transform::is_single_dataset(x, y)

Expand Down Expand Up @@ -221,7 +233,8 @@ ui_t_crosstable <- function(id, x, y, show_percentage, show_total, pre_output, p
checkboxInput(ns("show_percentage"), "Show column percentage", value = show_percentage),
checkboxInput(ns("show_total"), "Show total column", value = show_total)
)
)
),
ui_teal_transform_data(ns("decorate"), transformators = args$decorators)
),
forms = tagList(
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
Expand All @@ -232,7 +245,7 @@ ui_t_crosstable <- function(id, x, y, show_percentage, show_total, pre_output, p
}

# Server function for the cross-table module
srv_t_crosstable <- function(id, data, reporter, filter_panel_api, label, x, y, basic_table_args) {
srv_t_crosstable <- function(id, data, reporter, filter_panel_api, label, x, y, basic_table_args, decorators) {
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
checkmate::assert_class(data, "reactive")
Expand Down Expand Up @@ -351,7 +364,7 @@ srv_t_crosstable <- function(id, data, reporter, filter_panel_api, label, x, y,
teal.code::eval_code(
substitute(
expr = {
lyt <- basic_tables %>%
table <- basic_tables %>%
split_call %>% # styler: off
rtables::add_colcounts() %>%
tern::analyze_vars(
Expand Down Expand Up @@ -387,19 +400,20 @@ srv_t_crosstable <- function(id, data, reporter, filter_panel_api, label, x, y,
substitute(
expr = {
ANL <- tern::df_explicit_na(ANL)
tbl <- rtables::build_table(lyt = lyt, df = ANL[order(ANL[[y_name]]), ])
tbl
table <- rtables::build_table(lyt = table, df = ANL[order(ANL[[y_name]]), ])
},
env = list(y_name = y_name)
)
)
})

decorated_output_q <- srv_teal_transform_data("decorate", data = output_q, transformators = decorators)

output$title <- renderText(output_q()[["title"]])

table_r <- reactive({
req(iv_r()$is_valid())
output_q()[["tbl"]]
decorated_output_q()[["table"]]
})

teal.widgets::table_with_settings_srv(
Expand All @@ -409,7 +423,7 @@ srv_t_crosstable <- function(id, data, reporter, filter_panel_api, label, x, y,

teal.widgets::verbatim_popup_srv(
id = "rcode",
verbatim_content = reactive(teal.code::get_code(output_q())),
verbatim_content = reactive(teal.code::get_code(req(decorated_output_q()))),
title = "Show R Code for Cross-Table"
)

Expand All @@ -428,7 +442,7 @@ srv_t_crosstable <- function(id, data, reporter, filter_panel_api, label, x, y,
card$append_text("Comment", "header3")
card$append_text(comment)
}
card$append_src(teal.code::get_code(output_q()))
card$append_src(teal.code::get_code(req(decorated_output_q_build())))
card
}
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
Expand Down

0 comments on commit 51f6882

Please sign in to comment.