Create and use an empty tabset panel to insert tabs dynamically

In my Shiny app I try to create tabsets completely dynamically. Using insertTab works when I have statically created a first tab, but how do I insert a tab into an empty tabset? Should I create the tabset only when the first tab name/icon is known?

library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      actionButton("add", "Add 'Dynamic' tab"),
    ),
    mainPanel(
      tabsetPanel(id = "tabs",
                  ## Missing argument
      #     tabPanel("Hello", "This is the hello tab")
      )
    )
  )
)
server <- function(input, output, session) {
  observeEvent(input$add, {
    insertTab(inputId = "tabs",
              tabPanel("Dynamic", "This a dynamically-added tab"),
              target = ""
    )
  })
}

shinyApp(ui, server)
library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      actionButton("add", "Add 'Dynamic' tab"),
    ),
    mainPanel(
      tabsetPanel(id = "tabs")
    )
  )
)
server <- function(input, output, session) {
  observeEvent(input$add, {
    appendTab(inputId = "tabs",
              tabPanel("Dynamic", 
                       "This a dynamically-added tab")
    )
  })
}

shinyApp(ui, server)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.