Hi, I want to use renderUI to make a dynamic number of shinyBS::bsCollapsePanels, and am getting different results based on how I construct the list of bsCollapsePanel results. I am not sure if this behavior is due to the renderUI, renderDataTable, or bsCollapsePanel function.
If I construct a list manually (see output$Works), it works properly, but this does not allow me to specify the number of bsCollapsePanels dynamically.
If I construct a list with a for loop (see output$does_not_work), the last table in the list gets printed in every panel. This is very surprising. Any idea why this is occuring? Even though the code seems like it should generate the exact same list of bsCollapsePanels outputs?
Here is the reprex (R Shiny app):
library(shiny)
library(shinyBS)
library(tidyverse)
shinyApp(
ui =
fluidPage(
fluidRow(uiOutput("works")),
fluidRow(uiOutput("does_not_work"))
),
server =
function(input, output, session) {
output$works <- renderUI({
list_of_tables <- list(tibble(V1 = "a"), tibble(V1 = "b"), tibble(V2 = "c"))
# works, but # of panels is not dynamic
myCollapse <- list(bsCollapsePanel(1,renderDataTable(list_of_tables[[1]])),
bsCollapsePanel(2,renderDataTable(list_of_tables[[2]])),
bsCollapsePanel(3,renderDataTable(list_of_tables[[3]])))
do.call(bsCollapse,myCollapse) %>% return()
})
output$does_not_work <- renderUI({
list_of_tables <- list(tibble(V1 = "a"), tibble(V1 = "b"), tibble(V2 = "c"))
# does not work (prints the same table in all panels), and is dynamic
myCollapse <- vector("list",length = length(list_of_tables))
for (i in 1:length(list_of_tables)) {
myCollapse[[i]] <- bsCollapsePanel(i,renderDataTable(list_of_tables[[i]]))
}
do.call(bsCollapse,myCollapse) %>% return()
})
}
)