0

I would like to display the current tab that I am on in an R Shiny App. I have viewed the questions related to this on SO, but I just can't get it to work. I assume I'm doing something silly. The issue might be with the nested tabs? This is my first SO post, so I tried my best to great a reprex.

Previous questions:

library(shiny)   



ui = navbarPage("My Shiny App", id = "Top Page",
                tabPanel(title = "My Tabs", 
                         tabsetPanel(type = "tabs", id = "tabset",
                                     tabPanel(title = "My First Tab", value = "tab_1",
                                              textOutput("mytab")),
                                     tabPanel(title = "My Second Tab", value = "tab_2",
                                              textOutput("mytab"))
                                     
                         )
                )
)

server = function(input, output) {
  
  output$mytab <- renderText({ 
    
    paste("You are viewing tab: ", input$tabs)

  })
}
  

shinyApp(ui = ui, server = server)

  • 3
    A couple of things: (1) you refer to `input$tabs` in your `renderText`, but the `id` I think you're referring to is `tabset` in your `ui`; so you may want `input$tabset` instead; (2) you have `textOutput("mytab")` twice in your `ui`, this should be unique. – Ben Aug 31 '20 at 18:17
  • Great thank you. When I get rid of the second instance of `textOutput("mytab")` in the `ui` and change `input$tabs` to `input$tabset`, it works. I see now that the input that I need to use is the id of the `tabsetPanel`. I am still slightly confused though how I might scale this same `textOutput` across many tabs. I guess I would need to change the name of the `output` each time? – trabendo_daze Aug 31 '20 at 18:28
  • 1
    Glad to hear it worked. There are multiple ways to deal with the multiple `textOutput` in your tabs...you could have `mytab1` and `mytab2`, with each of them referencing a common `reactive` expression on what to show in the tab window depending on `input$tabset`...or you can have `output$mytab1 <- output$mytab2 <- renderText({})`...etc... – Ben Aug 31 '20 at 18:31
  • Cool makes a lot of sense. I think this has been sufficiently answered, thanks! – trabendo_daze Aug 31 '20 at 18:35

0 Answers0