Search code examples
rshinytabpanel

Active tab to select radio button in Shiny


I have the following Shiny app code where the user can select radio buttons and then the corresponding tab panel is activated.

The question I have is how to do the reverse i.e. if the user selects a tab panel, how do you activate the corresponding radio button.

A reproducible example is below.

If you select Tab 2 radio button for example, Tab 2 is activated

If you then select Tab 3, then Tab 2 radio button remains selected and I would like it to update to Tab 3 radio button

Thanks

library(shiny)

radio_button_choices = list("Tab 1" = 1, "Tab 2" = 2, "Tab 3" = 3)

ui <- fluidPage(

sidebarLayout(
    sidebarPanel(
      radioButtons(inputId = "radio_button", label = h5("Select tab"), choices = radio_button_choices)),

    mainPanel(
      tabsetPanel(id = "tab",

                  tabPanel("Tab1", value = "panel1", htmlOutput("text1")),
                  tabPanel("Tab2", value = "panel2", htmlOutput("text2")),
                  tabPanel("Tab3", value = "panel3", htmlOutput("text3"))
      )
    ) 
  )
)

server <- function(input, output, session) {

  observeEvent(input$radio_button, {
    updateTabsetPanel(session, "tab",
                      selected = paste0("panel", input$radio_button)
    )
  })

  output$text1 = renderUI({
    str1 = "This is tab 1"
    HTML(paste(str1)) 
  })

  output$text2 = renderUI({
    str1 = "This is tab 2"
    HTML(paste(str1)) 
  })

  output$text3 = renderUI({
    str1 = "This is tab 3"
    HTML(paste(str1)) 
  })

}

# Run the application 
shinyApp(ui = ui, server = server)


Solution

  • You can do something similar with updateRadioButtons.

    You also might like a similar vector for your tabPanel choices.

    library(shiny)
    
    radio_button_choices = list("Tab 1" = 1, "Tab 2" = 2, "Tab 3" = 3)
    panel_choices = list("Panel 1" = 1, "Panel 2" = 2, "Panel 3" = 3)
    
    ui <- fluidPage(
    
      sidebarLayout(
        sidebarPanel(
          radioButtons(inputId = "radio_button", label = h5("Select tab"), choices = radio_button_choices)),
    
        mainPanel(
          tabsetPanel(id = "tab",
    
                      tabPanel(names(panel_choices)[1], value = panel_choices[[1]], htmlOutput("text1")),
                      tabPanel(names(panel_choices)[2], value = panel_choices[[2]], htmlOutput("text2")),
                      tabPanel(names(panel_choices)[3], value = panel_choices[[3]], htmlOutput("text3"))
          )
        ) 
      )
    )
    
    server <- function(input, output, session) {
    
      observeEvent(input$radio_button, {
        updateTabsetPanel(session, "tab", selected = input$radio_button)
      })
    
      output$text1 = renderUI({
        str1 = "This is tab 1"
        HTML(paste(str1)) 
      })
    
      output$text2 = renderUI({
        str1 = "This is tab 2"
        HTML(paste(str1)) 
      })
    
      output$text3 = renderUI({
        str1 = "This is tab 3"
        HTML(paste(str1)) 
      })
    
      observeEvent(input$tab, {
        updateRadioButtons(session, "radio_button", selected = input$tab)
      })
    
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)