Change Shiny navbarPage tabPanel programatically

311 views
Skip to first unread message

Josh Gage

unread,
May 15, 2017, 1:17:03 PM5/15/17
to Shiny - Web Framework for R

I have a Shiny app with several tabPanels within a navbarPage.


ui<-shinyUI(
  navbarPage(
  theme = "cyborgBootstrap.css",
  "Shiny panel append",
    tabPanel(
      "First Panel",
      strong("panel 1")
      ),
    tabPanel(
      "Second Panel",
      strong("panel 1")
      ),
    tabPanel(
      "Third Panel",
      strong("panel 1")
      )
    )
  )

When the app loads, "First Panel" will be active. I would like to be able to add some server functions that toggle the panels when certain tasks complete. How could I toggle between panels programatically?

Bárbara Borges

unread,
May 16, 2017, 11:02:36 AM5/16/17
to Shiny - Web Framework for R
Use updateNavbarPage().

Minimal example:

library(shiny)

ui <- navbarPage(title = "updateNavbarPage", id = "tabs",
  tabPanel(title = "First Panel", value = "panel1",
    strong("Panel 1"),
    hr(),
    actionButton("go1to2", "Go to 2"),
    actionButton("go1to3", "Go to 3")
  ),
  tabPanel(title = "Second Panel", value = "panel2",
    strong("Panel 2"),
    hr(),
    actionButton("go2to1", "Go to 1"),
    actionButton("go2to3", "Go to 3")
  ),
  tabPanel(title = "Third Panel", value = "panel3",
    strong("Panel 3"),
    hr(),
    actionButton("go3to1", "Go to 1"),
    actionButton("go3to2", "Go to 2")
  )
)

server <- function(input, output, session) {
  
  observeEvent(c(input$go2to1, input$go3to1), {
    updateNavbarPage(session, "tabs", selected = "panel1")
  }, ignoreInit = TRUE)
  
  observeEvent(c(input$go1to2, input$go3to2), {
    updateNavbarPage(session, "tabs", selected = "panel2")
  }, ignoreInit = TRUE)
  
  observeEvent(c(input$go1to3, input$go2to3), {
    updateNavbarPage(session, "tabs", selected = "panel3")
  }, ignoreInit = TRUE)
}

shinyApp(ui, server)
Reply all
Reply to author
Forward
0 new messages