I try to dynamically create an accordion bast on a dataframe. Despite many try-outs I did not manage to get it working. Below the code of an example dashboard with two examples:
see example below.
Hopefully someone can help?
library(bs4Dash)
library(shiny)
dataset <- data.frame(
title = c("title 1", "title 2")
, tekst = c("text 1", "text 2")
)
# create using lapply (flexible)
lijst1 <- accordion(
id = "test1"
, lapply(seq_along(dataset$title), function(i){
accordionItem(title = dataset$title[i], dataset$tekst[i])
})
)
# create by specify each item
lijst2 <- accordion(
id = "test2"
, accordionItem(title = "title 1 simple", "text 1 simple")
, accordionItem(title = "title 1 simple", "text 2 simple")
)
ui <- dashboardPage(
dashboardHeader()
, dashboardSidebar()
, dashboardBody(
h2("Doesn't work")
, lijst1
, h2("Works")
, lijst2
)
)
server = function(input, output) {}
shinyApp(ui = ui, server = server)
One option to achieve your desired result would be to make use of do.call
which works quite well with lapply
as it allows you to pass function arguments as a list
:
library(bs4Dash)
library(shiny)
dataset <- data.frame(
title = c("title 1", "title 2"),
tekst = c("text 1", "text 2")
)
# create using lapply (flexible)
lijst1 <- do.call("accordion", c(
list(id = "test1"),
lapply(seq_along(dataset$title), function(i) accordionItem(title = dataset$title[i], dataset$tekst[i]))
))
# create by specify each item
lijst2 <- accordion(
id = "test2",
accordionItem(title = "title 1 simple", "text 1 simple"),
accordionItem(title = "title 1 simple", "text 2 simple")
)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
h2("Doesn't work"),
lijst1,
h2("Works"),
lijst2
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)