rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.36k stars 1.87k forks source link

Unable to display DT in nested shiny modules #3307

Closed jonekeat closed 3 years ago

jonekeat commented 3 years ago

System details

Browser Version: Google Chrome, Version 88.0.4324.182 (Official Build) (64-bit)

Output of sessionInfo():

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] DT_0.16              shinydashboard_0.7.1 shiny_1.5.0         

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5        digest_0.6.27     later_1.1.0.1     mime_0.9          R6_2.5.0          jsonlite_1.7.1   
 [7] xtable_1.8-4      magrittr_2.0.1    rlang_0.4.10      promises_1.1.1    tools_4.0.3       htmlwidgets_1.5.2
[13] crosstalk_1.1.0.1 yaml_2.2.1        rsconnect_0.8.16  httpuv_1.5.4      fastmap_1.0.1     compiler_4.0.3   
[19] htmltools_0.5.1.1

Example application or steps to reproduce the problem

```R library(shiny) library(shinydashboard) library(DT) ui <- function() { dashboardPage( dashboardHeader(title = "abc"), dashboardSidebar(uiOutput("sidebarpanel")), dashboardBody(uiOutput("body"))) } server <- function(input, output, session) { output$sidebarpanel <- renderUI({ tags$div( sidebarMenu(id = "tabs", menuItem("Data", tabName = "data")) ) }) output$body <- renderUI({ tabItems(ui_data1("data1", tabName = "data")) }) input_data1 <- new.env() input_data1$a <- reactive(1) input_data1$b <- reactive(2) input_data2 <- server_data1("data1", input_data1) } ui_data1 <- function(id, tabName){ ns <- NS(id) tabItem(tabName = tabName, uiOutput(ns("body"))) } server_data1 <- function(id, input_data1) { ns <- NS(id) moduleServer(id, function(input, output, session) { output$body <- renderUI({ tabsetPanel( ui_data2(ns("info1"), "Info1") ) }) data2 <- new.env() data2$input_data2 <- server_data2("info1", input_data1) return(data2) }) } ui_data2 <- function(id, title) { ns <- NS(id) tabPanel(title = title, uiOutput(ns("body"))) } server_data2 <- function(id, input_data1) { ns <- NS(id) moduleServer(id, function(input, output, session) { c <- eventReactive(input_data1$a(), { 2 }) sampledata <- reactive(mtcars) output$body <- renderUI({ all_cyl <- unique(sampledata()$cyl) tbl_by_cyl <- lapply(seq_along(all_cyl), function(i) { tabPanel(all_cyl[i], column(12, br(), box(width = "auto", DT::dataTableOutput(ns(paste0("cyl", i)), width = "100%")))) }) do.call(tabsetPanel, tbl_by_cyl) }) observe({ sampledata <- sampledata() all_cyl <- unique(sampledata$cyl) lapply(seq_along(all_cyl), function(i) { output[[paste0("cyl", i)]] <- DT::renderDataTable({ datatable(sampledata[sampledata$cyl == all_cyl[i], ]) }) }) }) return(sampledata) }) } shinyApp(ui, server) ``` ### Describe the problem in detail I and my colleagues have been using shiny for building company dashboard, as we found our apps growing more & more complex, and we decided to go [modularization](https://mastering-shiny.org/scaling-modules.html). I follow the [modularization](https://mastering-shiny.org/scaling-modules.html) chapter in mastering shiny book, but I have issue to dynamically display multiple DT in nested modules, as in the sample code above. The `sampledata` should be uploaded by users in real scenario. Below is the output of the above code: ![image](https://user-images.githubusercontent.com/51594799/109004002-1b251180-76e3-11eb-8f13-23c111acb19d.png) We expect the `DT`s should displayed under each `tabPanel`s PS: I am not sure whether this is a bug or issue in shiny, please forgive me if I had misunderstood something
jonekeat commented 3 years ago

The answer has been posted on StackOverflow, so I will close this issue