rshinyshinydashboard

Display shiny widget only for specific tabPanel in shiny dashboard


I want to display a ashiny widget only when specific tab is selected. Now I cannot display it all.

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyWidgets)
ui <- dashboardPage(
  dashboardHeader(title = "P-T-H2O-An"),
  dashboardSidebar(
    uiOutput("filter1")
  ),
  dashboardBody(
    tabsetPanel(
      id="tabs",
      tabPanel(strong("PLAGIOCLASE SATURATION CHECK")),
      tabPanel(strong("AN CONTENT")),
      tabPanel(strong("THERMOMETRY")),
      tabPanel(strong("HYGROMETRY")),
      tabPanel(strong("BAROMETRY")),
      tabPanel(strong("Filtering & Saving Estimates"))
      
      
    )
  )
)

server <- function(input, output) {
  
  
  output$filter1<-renderUI({
    if(input$tabs=="PLAGIOCLASE SATURATION CHECK"){
      pickerInput(
        inputId = "x1",
        label = "Select all option",
        choices = rownames(mtcars),
        multiple = F,
        options = list(`actions-box` = TRUE)
      )
    }
    else{
      return(NULL)
    }
  })
}

shinyApp(ui, server)

Solution

  • Your input tab name is "strong("PLAGIOCLASE SATURATION CHECK")" and this results in the HTML/Text value passed to the "input" as

    <strong>PLAGIOCLASE SATURATION CHECK</strong>
    

    Therefore "==" will fail as it is not actually the name being read-in.

    Try instead:

      output$filter1<-renderUI({
        ui <- NULL
        if(grepl("PLAGIOCLASE SATURATION CHECK",input$tabs)) {
          ui <- pickerInput(
            inputId = "x1",
            label = "Select all option",
            choices = rownames(mtcars),
            multiple = F,
            options = list(`actions-box` = TRUE)
          )
        }
        return (ui)
      })