Open nFrechen opened 6 years ago
If you just remove the bsCollapse
in there, this code works! I'm still learning my way around the package and I ran into that issue earlier today and removing the bsCollapse
did the trick. Not totally sure why!
library(shiny)
library(shinyBS)
server <- function(input, output) {}
ui <- fluidPage(
lapply(1:3, function(x) textInput(x, paste("Select", x))),
lapply(1:3, function(x) bsCollapsePanel(paste("Collapse", x)))
)
shinyApp(ui = ui, server = server)
@nFrechen @benjaminrobinson This is probably no longer relevant since it was quite a while ago, but since I've made the same mistake before, I thought I should comment with the solution.
The bsCollapse function expects a variable number of arguments, rather than a single-argument list. To call such a function with a dynamic number of children, you can use do.call
like so:
do.call(bsCollapse, lapply(1:3, function(x) bsCollapsePanel(paste("Collapse", x))))
This will take your list and pass it to bsCollapse as a variable number of arguments.
Hi,
I use shinyBS::bsCollapse*()
(great tool, thanks you). But: this seems to be an unstable behavior. For instance, the attached code follows your recommendation but ends up with the same bug.
library(shiny)
ui <- fluidPage(
shinyjs::useShinyjs(),
uiOutput("edit_files")
)
server <- function(input, output, session) {
output$edit_files <- renderUI({
files <- dir("~/Documents/Test data/EAL samples/", full.names = TRUE)
tables <- setNames(
lapply(files, data.table::fread, data.table = FALSE, stringsAsFactors = FALSE),
basename(files)
)
do.call(
tabsetPanel,
args = c(
id = session$ns("tabset"),
lapply(
basename(files),
tables = tables,
# file UI - tab content
function(table.name, tables) {
table <- tables[[table.name]]
browser()
tabPanel(
title = table.name,
value = table.name,
do.call(
shinyBS::bsCollapse,
args = c(
id = session$ns(table.name),
multiple = FALSE,
lapply(
seq(nrow(table)),
id = session$ns(table.name),
table.name = table.name,
table = table,
# row UI - collapsePanel
function(test, id, table.name, table) {
# Rest of the UI
}
)
)
)
)
}
)
)
)
})
}
shinyApp(ui, server)
I trie to generate a bunch of collapsePanels with an apply function. This works for a lot of shiny elements like inputs, tabPanels etc. The bsCollapse function however shows an error when given a list of child elements.
Tor reproduce the error use the following app:
I get this error:
I use the package version 0.61 on R version 3.3.3. I tried the latest version from github and it produced similar errors.
Please let me know if you need further details.
Thank you very much!