在 bs4Dash 中使用 lapply 动态创建手风琴

create dynamically an accordion with lapply in bs4Dash

我尝试在数据帧上动态创建手风琴韧皮。尽管进行了许多尝试,但我还是没能成功。下面是带有两个示例的示例仪表板的代码:

  1. lappy:不工作 (lijst1)
  2. 对每个项目进行编程:工作 (lijst2)

请参阅下面的示例。

希望有人能帮忙?

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)

实现所需结果的一个选择是使用 do.call,它与 lapply 配合得很好,因为它允许您将函数参数作为 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)