Active tab to select radio button in Shiny

余生长醉 提交于 2021-01-29 13:37:42

问题


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)


回答1:


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)


来源:https://stackoverflow.com/questions/61431489/active-tab-to-select-radio-button-in-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!