show / hide tabPanel based on user selection in a drop down

I want to hide a tab when a user selects a certain option in a dropdown. I've put the code inside:

output$text1 <- function() {
 ........
    # Hide rate ratio tab if a indicator is for medians
    if (grepl("MEDIAN", toupper(input$selected.indicator), fixed=TRUE)){hideTab(inputId="tabselected",target=2)}
    else {showTab(inputId="tabselected",target=2)}
.......
  }

the ui:

tabsetPanel(
        tabPanel("Prevalence / mean", value=1, DT::dataTableOutput("prevtab")),
        tabPanel("Subgroups comparison", value=2,  DT::dataTableOutput("comptab")),
        id ="tabselected"
      ),

What am I doing wrong here? Thanks!

Update: rewritten more logically now but still not hiding tab:

  observeEvent(input$selected.indicator, {if (grepl("MEDIAN", toupper(input$selected.indicator), fixed=TRUE))
    {hideTab(inputId="tabselected",target=2)}
    else {showTab(inputId="tabselected",target=2)}
  })
1 Like

I think the problem is that you are using numeric values for the "value" and "target" parameter. Use strings instead - as least in the hideTab/showTab calls. Shiny will change the "value" values to strings in the HTML. You can verify this in the DOM explorer in the browser.

Cheers
Steen

1 Like

Yes, as mentioned by @stkrog using String type for value parameter of tabPanel works. Please check the below example, the "Subgroups comparison" tab is hidden only when "median" choice is selected and an observer can be used to perform the action at Server.

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 Like

Brilliant thanks guys, problem solved. Cheers

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.