1919# ' @param show_total (`logical(1)`)
2020# ' Indicates whether to show total column.
2121# ' Defaults to `TRUE`.
22+ # ' @param remove_zero_columns (`logical(1)`)
23+ # ' Indicates whether to remove columns that contain only zeros from the output table.
24+ # ' Defaults to `FALSE`.
2225# '
2326# ' @note For more examples, please see the vignette "Using cross table" via
2427# ' `vignette("using-cross-table", package = "teal.modules.general")`.
2528# '
2629# ' @inherit shared_params return
2730# '
31+ # ' @section Table Settings:
32+ # ' The module provides several table settings that can be adjusted:
33+ # ' \itemize{
34+ # ' \item \code{Show column percentage}: Shows column percentages when enabled
35+ # ' \item \code{Show total column}: Shows a total column when enabled
36+ # ' \item \code{Remove zero-only columns}: Removes columns that contain only zeros from the output table
37+ # ' }
38+ # '
2839# ' @section Decorating Module:
2940# '
3041# ' This module generates the following objects, which can be modified in place using decorators:
@@ -157,6 +168,7 @@ tm_t_crosstable <- function(label = "Cross Table",
157168 y ,
158169 show_percentage = TRUE ,
159170 show_total = TRUE ,
171+ remove_zero_columns = FALSE ,
160172 pre_output = NULL ,
161173 post_output = NULL ,
162174 basic_table_args = teal.widgets :: basic_table_args(),
@@ -177,6 +189,7 @@ tm_t_crosstable <- function(label = "Cross Table",
177189
178190 checkmate :: assert_flag(show_percentage )
179191 checkmate :: assert_flag(show_total )
192+ checkmate :: assert_flag(remove_zero_columns )
180193 checkmate :: assert_multi_class(pre_output , c(" shiny.tag" , " shiny.tag.list" , " html" ), null.ok = TRUE )
181194 checkmate :: assert_multi_class(post_output , c(" shiny.tag" , " shiny.tag.list" , " html" ), null.ok = TRUE )
182195 checkmate :: assert_class(basic_table_args , classes = " basic_table_args" )
@@ -191,6 +204,7 @@ tm_t_crosstable <- function(label = "Cross Table",
191204 label = label ,
192205 x = x ,
193206 y = y ,
207+ remove_zero_columns = remove_zero_columns ,
194208 basic_table_args = basic_table_args ,
195209 decorators = decorators
196210 )
@@ -209,7 +223,7 @@ tm_t_crosstable <- function(label = "Cross Table",
209223}
210224
211225# UI function for the cross-table module
212- ui_t_crosstable <- function (id , x , y , show_percentage , show_total , pre_output , post_output , ... ) {
226+ ui_t_crosstable <- function (id , x , y , show_percentage , show_total , remove_zero_columns , pre_output , post_output , ... ) {
213227 args <- list (... )
214228 ns <- NS(id )
215229 is_single_dataset <- teal.transform :: is_single_dataset(x , y )
@@ -244,7 +258,8 @@ ui_t_crosstable <- function(id, x, y, show_percentage, show_total, pre_output, p
244258 bslib :: accordion_panel(
245259 title = " Table settings" ,
246260 checkboxInput(ns(" show_percentage" ), " Show column percentage" , value = show_percentage ),
247- checkboxInput(ns(" show_total" ), " Show total column" , value = show_total )
261+ checkboxInput(ns(" show_total" ), " Show total column" , value = show_total ),
262+ checkboxInput(ns(" remove_zero_columns" ), " Remove zero-only columns" , value = remove_zero_columns )
248263 )
249264 ),
250265 ui_decorate_teal_data(ns(" decorator" ), decorators = select_decorators(args $ decorators , " table" ))
@@ -258,7 +273,7 @@ ui_t_crosstable <- function(id, x, y, show_percentage, show_total, pre_output, p
258273}
259274
260275# Server function for the cross-table module
261- srv_t_crosstable <- function (id , data , label , x , y , basic_table_args , decorators ) {
276+ srv_t_crosstable <- function (id , data , label , x , y , remove_zero_columns , basic_table_args , decorators ) {
262277 checkmate :: assert_class(data , " reactive" )
263278 checkmate :: assert_class(isolate(data()), " teal_data" )
264279 moduleServer(id , function (input , output , session ) {
@@ -352,6 +367,7 @@ srv_t_crosstable <- function(id, data, label, x, y, basic_table_args, decorators
352367
353368 show_percentage <- input $ show_percentage
354369 show_total <- input $ show_total
370+ remove_zero_columns <- input $ remove_zero_columns
355371
356372 plot_title <- paste(
357373 " Cross-Table of" ,
@@ -415,14 +431,32 @@ srv_t_crosstable <- function(id, data, label, x, y, basic_table_args, decorators
415431 )
416432 ) %> %
417433 teal.code :: eval_code(
418- substitute(
419- expr = {
420- ANL <- tern :: df_explicit_na(ANL )
421- table <- rtables :: build_table(lyt = table , df = ANL [order(ANL [[y_name ]]), ])
422- },
423- env = list (y_name = y_name )
424- )
434+ expression(ANL <- tern :: df_explicit_na(ANL ))
425435 )
436+
437+ if (remove_zero_columns ) {
438+ obj <- obj %> %
439+ teal.code :: eval_code(
440+ substitute(
441+ expr = {
442+ ANL [[y_name ]] <- droplevels(ANL [[y_name ]])
443+ table <- rtables :: build_table(lyt = table , df = ANL [order(ANL [[y_name ]]), ])
444+ },
445+ env = list (y_name = y_name )
446+ )
447+ )
448+ } else {
449+ obj <- obj %> %
450+ teal.code :: eval_code(
451+ substitute(
452+ expr = {
453+ table <- rtables :: build_table(lyt = table , df = ANL [order(ANL [[y_name ]]), ])
454+ },
455+ env = list (y_name = y_name )
456+ )
457+ )
458+ }
459+ obj
426460 })
427461
428462 decorated_output_q <- srv_decorate_teal_data(
0 commit comments