Hello,
I am working on a Shiny application that allows a user to view and edit tables created from a database. I am using a module to generate a data table, and up to three data tables are generated depending on the number of records (1, 2, or 3) in the database. To streamline the user interface, I would like to call modules conditionally based on the number of database records that exist. So, for example, if there are only two records in the database, then only data tables for those first two records should be generated. I am having some difficulty accomplishing this in my ui. I can get something like the following to work where I can use conditions to call a single, independent record:
ui
column(10,
conditionalPanel(
condition = "output.sampleList.length <= 1",
dataTableUI("one")
),
conditionalPanel(
condition = "output.sampleList.length <= 2",
dataTableUI("two")
),
conditionalPanel(
condition = "output.sampleList.length <= 3",
dataTableUI("three")
)
)
However, what I really need is something more like the following where the condition dictates the
number of modules that are displayed. The approach below does not error but yields six empty modules. It does not seem that it is feasible to refer to modules more than once even if they are isolated in conditional panels. Is there another way to address this type of functionality? I know a loop would be another way to handle this but that has its own complications, especially for styling, and would prefer a conditional approach if it is possible.
column(10,
conditionalPanel(
condition = "output.sampleList.length <= 1",
dataTableUI("one")
),
conditionalPanel(
condition = "output.sampleList.length <= 2",
dataTableUI("one"),
dataTableUI("two")
),
conditionalPanel(
condition = "output.sampleList.length <= 3",
dataTableUI("one"),
dataTableUI("two"),
dataTableUI("three")
)
)
# truncated main app server code
server <- function(input, output) {
samples <- reactive({
id <- call to database....
return(id)
})
callModule(dataTableUI, "one", site, reactive({ samples()[1,'id'] }))
callModule(dataTableUI, "two", site, reactive({ samples()[2,'id'] }))
callModule(dataTableUI, "three", site, reactive({ samples()[3,'id'] }))
output$sampleList <- reactive({
samples()[,'id']
})
outputOptions(output, "sampleList", suspendWhenHidden = FALSE)
}
# truncated module code
dataTableUI <- function(id) {
ns <- NS(id)
...
}
dataTable <- function(input, output, session, site, sampleId) {
...
}
Thank you for any suggestions!