根据选项卡面板选择在 R shiny 中显示/隐藏 selectinput

Show / Hide selectinput in R shiny based on tabpanel selection

我需要为不同的标签显示不同的 dropdown/selectinput。

例如。如果选项卡 1 被选中,则显示带有 Tim、Bill、Jeff 等值列表的 selectinput ... 如果选择了选项卡二,则显示 selectinput 和值列表 Cat、Dog、Squirrel、...

我在网上找到了下面的脚本,但反之亦然(shows/hides tabpanels based on selectinput selection - 但我需要show/hide selectinput based on tabpanel selection).

runApp(list(
  ui = shinyUI(
    fluidPage(
      
      sidebarLayout(
        sidebarPanel(
          selectInput(
            inputId = 'selected.indicator',
            label = 'Select an option: ',
            choices = c('mean', 'median', 'mode')
          )
        ),
        mainPanel(
          tabsetPanel(
            tabPanel("Prevalence / mean", value = 'meanTab', DT::dataTableOutput("prevtab")),
            tabPanel("Subgroups comparison", value = 'medianTab',  DT::dataTableOutput("comptab")),
            id ="tabselected"
          )
        )
      )
    )
  ),
  
  server = function(input, output, session) {
    
    observe({
      req(input$selected.indicator)
      if (grepl("MEDIAN", toupper(input$selected.indicator), fixed = TRUE)) {
        hideTab(inputId = "tabselected", target = 'medianTab')
      }
      else showTab(inputId = "tabselected", target = 'medianTab')
    })
  }
))

给你。

runApp(list(
  ui = shinyUI(
    fluidPage(
      
      sidebarLayout(
        sidebarPanel(
          selectInput(
            inputId = 'selected.indicator',
            label = 'Select an option: ',
            choices = c('')
          )
        ),
        mainPanel(
          tabsetPanel(
            tabPanel("tab1", value = 'tab1', p("tab1")),
            tabPanel("tab2", value = 'tab2',  p("tab2")),
            id ="tabselected"
          )
        )
      )
    )
  ),
  
  server = function(input, output, session) {
    choices <- reactiveValues(
      tab1 = c('Tim', 'Bill', 'Jeff'),
      tab2 = c('Cat', 'Dog', 'Squirrel')
    )
    observeEvent(input$tabselected, {
      updateSelectInput(session, 'selected.indicator', choices = choices[[input$tabselected]])
    })
  }
))