2929# '
3030# ' @inherit shared_params return
3131# '
32+ # ' @section Decorating `tm_data_table`:
33+ # '
34+ # ' This module creates below objects that can be modified with decorators:
35+ # ' - `table` (`DT::datatable`)
36+ # '
37+ # ' For additional details and examples of decorators, refer to the vignette
38+ # ' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
39+ # '
3240# ' @examplesShinylive
3341# ' library(teal.modules.general)
3442# ' interactive <- function() TRUE
@@ -96,7 +104,8 @@ tm_data_table <- function(label = "Data Table",
96104 ),
97105 server_rendering = FALSE ,
98106 pre_output = NULL ,
99- post_output = NULL ) {
107+ post_output = NULL ,
108+ decorators = list (default = teal_transform_module())) {
100109 message(" Initializing tm_data_table" )
101110
102111 # Start of assertions
@@ -121,6 +130,8 @@ tm_data_table <- function(label = "Data Table",
121130 checkmate :: assert_flag(server_rendering )
122131 checkmate :: assert_multi_class(pre_output , c(" shiny.tag" , " shiny.tag.list" , " html" ), null.ok = TRUE )
123132 checkmate :: assert_multi_class(post_output , c(" shiny.tag" , " shiny.tag.list" , " html" ), null.ok = TRUE )
133+
134+ checkmate :: assert_list(decorators , " teal_transform_module" )
124135 # End of assertions
125136
126137 ans <- module(
@@ -133,11 +144,13 @@ tm_data_table <- function(label = "Data Table",
133144 datasets_selected = datasets_selected ,
134145 dt_args = dt_args ,
135146 dt_options = dt_options ,
136- server_rendering = server_rendering
147+ server_rendering = server_rendering ,
148+ decorators = decorators
137149 ),
138150 ui_args = list (
139151 pre_output = pre_output ,
140- post_output = post_output
152+ post_output = post_output ,
153+ decorators = decorators
141154 )
142155 )
143156 attr(ans , " teal_bookmarkable" ) <- TRUE
@@ -147,7 +160,8 @@ tm_data_table <- function(label = "Data Table",
147160# UI page module
148161ui_page_data_table <- function (id ,
149162 pre_output = NULL ,
150- post_output = NULL ) {
163+ post_output = NULL ,
164+ decorators = decorators ) {
151165 ns <- NS(id )
152166
153167 tagList(
@@ -161,7 +175,8 @@ ui_page_data_table <- function(id,
161175 ns(" if_distinct" ),
162176 " Show only distinct rows:" ,
163177 value = FALSE
164- )
178+ ),
179+ ui_teal_transform_data(ns(" decorator" ), transformators = decorators )
165180 )
166181 ),
167182 fluidRow(
@@ -185,7 +200,8 @@ srv_page_data_table <- function(id,
185200 variables_selected ,
186201 dt_args ,
187202 dt_options ,
188- server_rendering ) {
203+ server_rendering ,
204+ decorators ) {
189205 checkmate :: assert_class(data , " reactive" )
190206 checkmate :: assert_class(isolate(data()), " teal_data" )
191207 moduleServer(id , function (input , output , session ) {
@@ -260,7 +276,8 @@ srv_page_data_table <- function(id,
260276 if_distinct = if_distinct ,
261277 dt_args = dt_args ,
262278 dt_options = dt_options ,
263- server_rendering = server_rendering
279+ server_rendering = server_rendering ,
280+ decorators = decorators
264281 )
265282 }
266283 )
@@ -305,36 +322,57 @@ srv_data_table <- function(id,
305322 if_distinct ,
306323 dt_args ,
307324 dt_options ,
308- server_rendering ) {
325+ server_rendering ,
326+ decorators ) {
309327 moduleServer(id , function (input , output , session ) {
310328 iv <- shinyvalidate :: InputValidator $ new()
311329 iv $ add_rule(" variables" , shinyvalidate :: sv_required(" Please select valid variable names" ))
312330 iv $ add_rule(" variables" , shinyvalidate :: sv_in_set(
313331 set = names(isolate(data())[[dataname ]]), message_fmt = " Not all selected variables exist in the data"
314332 ))
315333 iv $ enable()
334+ # teal::validate_inputs(iv)
316335
317- output $ data_table <- DT :: renderDataTable(server = server_rendering , {
318- teal :: validate_inputs(iv )
319-
336+ data_table_data <- reactive({
320337 df <- data()[[dataname ]]
321- variables <- input $ variables
322338
323339 teal :: validate_has_data(df , min_nrow = 1L , msg = paste(" data" , dataname , " is empty" ))
324340
325- dataframe_selected <- if (if_distinct()) {
326- dplyr :: count(df , dplyr :: across(dplyr :: all_of(variables )))
327- } else {
328- df [variables ]
329- }
341+ teal.code :: eval_code(
342+ data(),
343+ substitute(
344+ expr = {
345+ dataframe_selected <- if (if_distinct ) {
346+ dplyr :: count(dataname , dplyr :: across(dplyr :: all_of(variables )))
347+ } else {
348+ dataname [variables ]
349+ }
350+ dt_args <- args
351+ dt_args $ options <- dt_options
352+ if (! is.null(dt_rows )) {
353+ dt_args $ options $ pageLength <- dt_rows
354+ }
355+ dt_args $ data <- dataframe_selected
356+ table <- do.call(DT :: datatable , dt_args )
357+ },
358+ env = list (
359+ dataname = as.name(dataname ),
360+ if_distinct = if_distinct(),
361+ variables = input $ variables ,
362+ args = dt_args ,
363+ dt_options = dt_options ,
364+ dt_rows = input $ dt_rows
365+ )
366+ )
367+ )
368+ })
330369
331- dt_args $ options <- dt_options
332- if (! is.null(input $ dt_rows )) {
333- dt_args $ options $ pageLength <- input $ dt_rows
334- }
335- dt_args $ data <- dataframe_selected
370+ decorated_data_table_data <-
371+ srv_teal_transform_data(" decorate" , data = data_table_data , transformators = decorators )
336372
337- do.call(DT :: datatable , dt_args )
373+ output $ data_table <- DT :: renderDataTable(server = server_rendering , {
374+ # no table is displayed
375+ decorated_data_table_data()[[" table" ]]
338376 })
339377 })
340378}
0 commit comments