Marta
Marta

Reputation: 3162

Disable one option when second is chosen in selectizeInput

I've got a simple shiny application, like here:

ui.R:

library(shiny)

data(iris)

shinyUI(fluidPage(

  titlePanel("Shiny Text"),

  sidebarLayout(
    sidebarPanel(
      selectizeInput("species", label=NULL, options=list(placeholder="Choose species"), 
                     choices = c(levels(iris$Species), "all"),
                     multiple=TRUE)
    ),

    mainPanel(
      tableOutput("tabela")
    )
  )
))

server.R:

library(shiny)
library(dplyr)

data(iris)

shinyServer(function(input, output) {

  output$tabela <- renderTable({

      if(input$species == "all"){
          iris
      } else{
          iris %>% 
              filter(Species %in% input$species)    
      }
  })
})         

What I'd like to achive is to block option "all" when one of "virginica", "setosa" or "versiciolor" is already chosen. Option all and something doesn't make any sense...

Do you know how to deal with it?

Upvotes: 0

Views: 110

Answers (1)

mlegge
mlegge

Reputation: 6913

This works, redefining the reactive output depending on the selection:

server.R

library(shiny)
library(dplyr)

data(iris)
specChoices <- levels(iris$Species)

shinyServer(function(input, output) {

  output$menu <- renderUI({
    if (!is.null(input$species)){
      if (any(input$species %in% specChoices)) {
        selectizeInput("species", label=NULL, options=list(placeholder="Choose species"), 
                       choices = specChoices, selected = input$species,
                       multiple=TRUE)
      } else {
        selectizeInput("species", label=NULL, options=list(placeholder="Choose species"), 
                       choices = c(specChoices, "all"), selected = "all",
                       multiple=TRUE)  
      }  
    } else {
      selectizeInput("species", label=NULL, options=list(placeholder="Choose species"), 
                     choices = c(specChoices, "all"),
                     multiple=TRUE)  
    }
  })

  output$tabela <- renderTable({
    if (!is.null(input$species)){
      if("all" %in% input$species ){
        iris
      } else{
        iris %>% 
          filter(Species %in% input$species)    
      }  
    } else {
      iris
    }
  })
})         

ui.R

library(shiny)

data(iris)

shinyUI(fluidPage(

  titlePanel("Shiny Text"),

  sidebarLayout(
    sidebarPanel(
     uiOutput("menu")
    ),

    mainPanel(
      tableOutput("tabela")
    )
  )
))   

Upvotes: 1

Related Questions