0
votes

I need to show different dropdown/selectinput for different tabs.

Ex. If tab one is selected, then show selectinput with a list of values Tim, Bill, Jeff, ... If tab two is selected, then show selectinput with a list of values Cat, Dog, Squirrel, ...

I found the below script online but it does vice-versa (shows/hides tabpanels based on selectinput selection - But I need 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')
    })
  }
))
1

1 Answers

0
votes

Here you go.

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]])
    })
  }
))