循环在Shiny的tabsetPanel中创建选项卡

Gau*_*sal 4 r shiny

我试图根据以下示例在Shiny中lapply创建多个标签tabsetPanelhttp : //shiny.rstudio.com/gallery/creating-a-ui-from-a-loop.html。下面是我的app.R代码。当我运行它时,它不会创建5个选项卡,也不会打印每个选项卡的名称。我究竟做错了什么?

library(shiny)

ui <- pageWithSidebar(
  headerPanel("xxx"),
  sidebarPanel(),
  mainPanel(
    tabsetPanel(id='t',
      lapply(1:5, function(i) {
        tabPanel(
          title=paste0('tab', i), 
          textOutput(paste0('a',i))
        )
      }) 
    )
  )
)

server <- function(input, output) {
  observe({
    print(input$t)
  })

  lapply(1:5, function(j) {
    output[[paste0('a',j)]] <- renderPrint({
      input$t
    })
  })
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

Hub*_*rtL 6

这有点棘手,因为tabsetPanel它不接受的列表tabset作为参数。您可以do.call用来“取消列出”参数:

mainPanel(
    do.call(tabsetPanel, c(id='t',lapply(1:5, function(i) {
                  tabPanel(
                    title=paste0('tab', i), 
                    textOutput(paste0('a',i))
                  )
                })))
    )
Run Code Online (Sandbox Code Playgroud)