在selectizeInput中选择第二个时禁用一个选项

时间:2015-12-02 12:50:36

标签: r select shiny

我有一个简单的闪亮应用程序,就像这里:

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

我想要实现的目标是阻止选项&#34;所有&#34;当其中一个&#34; virginica&#34;,&#34; setosa&#34;或&#34; versiciolor&#34;已被选中。选择所有和某些事情没有任何意义......

你知道怎么处理吗?

1 个答案:

答案 0 :(得分:1)

这样可行,根据选择重新定义无功输出:

<强> 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")
    )
  )
))