How to get which tab and sub tab is active?

63 Views Asked by At

I have a shiny application built with navbar.
I would like to be able to retrieve which tab and sub-tab are active? That is, which tabPanel and sub-tabPanel are active ?
In the console I can find out which 1st level tab is active. But I don't know how to retrieve the 2nd level tabs.

library("shiny")
ui = navbarPage(title = "Navbar example", 
                id = "navbar",
                tabPanel(title = "tab 1", 
                         tabsetPanel(id = "id1",
                                     tabPanel(title = "A",
                                     ),
                                     tabPanel(title = "B",
                                     )
                )),
                tabPanel(title = "tab 2", 
                         tabsetPanel(id = "id2",
                                     tabPanel(title = "C",
                                     ),
                                     tabPanel(title = "D",
                                     ),
                                     tabPanel(title = "E",
                                     )
                ))
)

server = shinyServer(function(input, output) {
  observe({
    print(paste0("You are viewing tab \"", input$navbar))
  })
})

shinyApp(ui, server)

Ideally, I'd like to have for example :

  • tab 1 selected and
  • sub-tab B selected

I'm not interested in doing input$id1 or input$id2.
I want the information without having to specify which main tab is already selected

1

There are 1 best solutions below

0
smartse On

I can't find any way to avoid using input$id1 but this method at least automates it:

  index <- data.frame('tab' = c('tab 1','tab 2'), 'subtab'= c('id1','id2'))

    observe({
    print(paste0("You are viewing tab \"", input$navbar, input[[index$subtab[index$tab == input$navbar]]]))
  })