@@ -125,8 +125,8 @@ tm_data_table <- function(label = "Data Table",
125125
126126 ans <- module(
127127 label ,
128- server = srv_page_data_table ,
129- ui = ui_page_data_table ,
128+ server = srv_data_table ,
129+ ui = ui_data_table ,
130130 datanames = if (length(datasets_selected ) == 0 ) " all" else datasets_selected ,
131131 server_args = list (
132132 variables_selected = variables_selected ,
@@ -145,7 +145,7 @@ tm_data_table <- function(label = "Data Table",
145145}
146146
147147# UI page module
148- ui_page_data_table <- function (id ,
148+ ui_data_table <- function (id ,
149149 pre_output = NULL ,
150150 post_output = NULL ) {
151151 ns <- NS(id )
@@ -168,7 +168,7 @@ ui_page_data_table <- function(id,
168168 class = " mb-8" ,
169169 column(
170170 width = 12 ,
171- uiOutput(ns(" dataset_table " ))
171+ uiOutput(ns(" data_tables " ))
172172 )
173173 )
174174 ),
@@ -179,7 +179,7 @@ ui_page_data_table <- function(id,
179179}
180180
181181# Server page module
182- srv_page_data_table <- function (id ,
182+ srv_data_table <- function (id ,
183183 data ,
184184 variables_selected = list (),
185185 datasets_selected = character (0 ),
@@ -199,24 +199,38 @@ srv_page_data_table <- function(id,
199199
200200 if_filtered <- reactive(as.logical(input $ if_filtered ))
201201 if_distinct <- reactive(as.logical(input $ if_distinct ))
202-
203- datanames <- isolate(names(data()))
204- datanames <- Filter(function (name ) {
205- is.data.frame(isolate(data())[[name ]])
206- }, datanames )
207-
208- if (! identical(datasets_selected , character (0 ))) {
209- checkmate :: assert_subset(datasets_selected , datanames )
210- datanames <- datasets_selected
211- }
212-
213- output $ dataset_table <- renderUI({
202+
203+ datanames <- reactive({
204+ df_datanames <- Filter(
205+ function (name ) is.data.frame(isolate(data())[[name ]]),
206+ names(data())
207+ )
208+ if (! identical(datasets_selected , character (0 ))) {
209+ missing_datanames <- setdiff(datasets_selected , df_datanames )
210+ if (length(missing_datanames )) {
211+ shiny :: showNotification(
212+ sprintf(
213+ " Some datasets specified `datasets_selected` are missing or are not inheriting from data.frame, those are: %s" ,
214+ toString(missing_datanames )
215+ )
216+ )
217+ }
218+ df_datanames <- intersect(datasets_selected , df_datanames )
219+ }
220+
221+ df_datanames
222+ })
223+
224+
225+
226+ output $ data_tables <- renderUI({
227+ req(datanames())
214228 do.call(
215229 tabsetPanel ,
216230 c(
217231 list (id = session $ ns(" dataname_tab" )),
218232 lapply(
219- datanames ,
233+ datanames() ,
220234 function (x ) {
221235 dataset <- isolate(data()[[x ]])
222236 choices <- names(dataset )
@@ -241,7 +255,7 @@ srv_page_data_table <- function(id,
241255 width = 12 ,
242256 div(
243257 class = " mt-4" ,
244- ui_data_table (
258+ ui_dataset_table (
245259 id = session $ ns(x ),
246260 choices = choices ,
247261 selected = variables_selected
@@ -254,28 +268,34 @@ srv_page_data_table <- function(id,
254268 )
255269 )
256270 })
257-
258- lapply(
259- datanames ,
260- function (x ) {
261- srv_data_table(
262- id = x ,
263- data = data ,
264- dataname = x ,
265- if_filtered = if_filtered ,
266- if_distinct = if_distinct ,
267- dt_args = dt_args ,
268- dt_options = dt_options ,
269- server_rendering = server_rendering ,
270- filter_panel_api = filter_panel_api
271- )
272- }
273- )
271+
272+ # server should be run only once
273+ modules_run <- reactiveVal()
274+ modules_to_run <- reactive(setdiff(datanames(), modules_run()))
275+ observeEvent(modules_to_run(), {
276+ lapply(
277+ modules_to_run(),
278+ function (dataname ) {
279+ srv_dataset_table(
280+ id = dataname ,
281+ data = data ,
282+ dataname = dataname ,
283+ if_filtered = if_filtered ,
284+ if_distinct = if_distinct ,
285+ dt_args = dt_args ,
286+ dt_options = dt_options ,
287+ server_rendering = server_rendering ,
288+ filter_panel_api = filter_panel_api
289+ )
290+ }
291+ )
292+ modules_run(union(modules_run(), modules_to_run()))
293+ })
274294 })
275295}
276296
277297# UI function for the data_table module
278- ui_data_table <- function (id ,
298+ ui_dataset_table <- function (id ,
279299 choices ,
280300 selected ) {
281301 ns <- NS(id )
@@ -306,7 +326,7 @@ ui_data_table <- function(id,
306326}
307327
308328# Server function for the data_table module
309- srv_data_table <- function (id ,
329+ srv_dataset_table <- function (id ,
310330 data ,
311331 dataname ,
312332 if_filtered ,
@@ -358,9 +378,6 @@ srv_data_table <- function(id,
358378 if (is.null(input $ data_table_rows_selected )) {
359379 return (NULL )
360380 }
361- # isolate({
362- # foo1(brush, selector_list)
363- # })
364381 dataset <- data()[[dataname ]][input $ data_table_rows_selected , ]
365382 # todo: when added another time then it is duplicated
366383 slice <- teal_slices(teal_slice(
0 commit comments