Can I use Shiny.setInputValue() to change tab in tabset

I have several tabs, and they use htmlOutput/renderText to create some displays that are not easily achievable with other UI. On one of the tabs, I have an html button that I want to select something and then have the first tab open up again. I am able to get it to update information used on the first tab, but I can't seem to get setInputValue('tabs','tabname') to update the UI to move to the other tab. Below is an example. Note that the value of tabs changes when I hit the button, and the javascript function works as evidenced by the alert().

ui <- fluidPage(
  tags$script(HTML("function setTab(){alert('going to Tab1 fails');Shiny.setInputValue('tabs','Tab1',{priority: 'event'});};")),
  textOutput("status"),
    tabsetPanel(id="tabs",
              tabPanel("Tab1",value="Tab1",
                       titlePanel("Tab1 Panel"),
                       htmlOutput("header1"),
                       textOutput("status2")
              ),
              tabPanel("Tab2",value="Tab2",
                       titlePanel("Tab2 Panel"),
                       htmlOutput("header2")
              )))

 server <- function(input, output, session) {
  output$status <- renderText(paste0("viewing: [",input$tabs ,"]"))

    observe({
     output$header1 <- renderText({"MAIN WINDOW BODY"})   
     output$header2 <- renderText({
     paste('<button type = "button"  onclick=\"setTab()">Go to other tab</button>')
     })
   })        
 }
 
 shinyApp(ui = ui, server = server)

Does anyone know if this is a limitation/bug in Shiny, or am I not doing something like failing to observe() something.

to solve the problem you can add this event in the server section, it should change the selected tab on the client when the value of "tabs" changed.

  observeEvent(input$tabs,{
      updateTabsetPanel(session, "tabs", input$tabs)
  })
1 Like

Thanks--that worked.

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.