22

I need the user to click a button and be taken to a different tabPanel of a navbarPage. Here is a simple app where this could be implemented. How can I achieve this?

ui <- shinyUI(
 navbarPage('Test App',
  tabPanel('Page 1',
   p('This is the first tab'), br(),
   actionButton('jumpToP2', 'Jump to Second Tab')
  ),

  tabPanel('Page 2',
   p('This is the second tab'),
   actionButton('jumpToP1', 'Jump to First Tab')
  )
 )
)

server <- shinyServer(function(input, output){
  observeEvent(input$jumpToP2,{
    ## Switch active tab to 'Page 2'
  })

  observeEvent(input$jumpToP1,{
    ## Switch active tab to 'Page 1'
  })
})

shinyApp(ui, server)
tsouchlarakis
  • 1,499
  • 3
  • 23
  • 44
  • this works with some minor changes: https://stackoverflow.com/questions/44309328/generic-button-for-go-to-next-and-previous-tabitem-shiny – Ferroao Nov 25 '18 at 20:57

2 Answers2

35

You can use updateTabsetPanel to switch between different tabPanels. See the documentation and example codes at https://shiny.rstudio.com/reference/shiny/latest/updateTabsetPanel.html. The code below should fulfill your requirements.

ui <- navbarPage('Test App',id = "inTabset",
                 tabPanel(title = "Panel 1", value = "panel1", 
                          actionButton('jumpToP2', 'Jump to Second Tab')),
                 tabPanel(title = "Panel 2", value = "panel2", 
                          actionButton('jumpToP1', 'Jump to First Tab'))
        )

server <- function(input, output, session) {
    observeEvent(input$jumpToP2, {
        updateTabsetPanel(session, "inTabset",
                          selected = "panel2")
    })

    observeEvent(input$jumpToP1, {
        updateTabsetPanel(session, "inTabset",
                          selected = "panel1")
    })

}

shinyApp(ui, server)
Phil
  • 7,287
  • 3
  • 36
  • 66
discipulus
  • 2,665
  • 3
  • 34
  • 51
0

I'm not so sure that this really the best way for you to design your U... Do you know that tabPanel() will act pretty much the same way as an actionButton() will in this case?

ui <- navbarPage('Test App',id = "inTabset",
             tabPanel(title = "Panel 1", uiOutput("panel1")),                       
             tabPanel(title = "Panel 2", uiOutput("panel2")) 
                      )
server <- function(input, output, session) {
    output$panel1 <- renderUI({
    #your UI data
    })
    output$panel2 <- renderUI({
    #your UI data
    })
}
shinyApp(ui, server)
Phi
  • 414
  • 3
  • 7