R中conditionPanel中的多个条件

时间:2017-02-10 13:57:57

标签: r shiny

我正在努力创建仅在选择特定选项卡时显示的selectInput菜单,并且会显示另一个selectInput菜单,该菜单特定于另一个菜单中可以选择的内容。 以下是一个简短的可重复示例

ui.R

    library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title="Essential Fish Habitat Application"),
  dashboardSidebar(
  sidebarMenu(id="tab",
      menuItem ("Welcome", tabName = "welcome"), 
      menuItem("EFH Maps",tabName="map"),
      selectInput("selectSpecies", h3("Select species"),
                  c("Almaco jack" = "ALMACOJACK",
                    "Banded rudderfish" = "BANDEDRUDDERFISH",
                    "Black grouper" = "BLACKGROUPER",
                    "Blackfin snapper" = "BLACKFINSNAPPER",
                    "Blueline tilefish" = "BLUELINETILEFISH",
                    "Brown shrimp" = "BROWNSHRIMP",
                    "Cobia" = "COBIA",
                    "Cubera snapper" = "CUBERASNAPPER"),
                  selected = "ALMACOJACK"),
      conditionalPanel(condition="input.tab=='map' && input.selectSpecies != 'BANDEDRUDDERFISH' ||
                        input.selectSpecies != 'BLACKGROUPER'",
                        selectInput("fishLifestage", h3("Select lifestage:"),
                                    c("Eggs"= "eggs",
                                      "Larvae" = "larvae",
                                      "Postlarvae" = "postLarvae",
                                      "Early juveniles" = "earlyJuvenile",
                                      "Late juveniles" = "lateJuvenile",
                                      "Adults" = "adult",
                                      "Spawning adults" = "spawningAdult" 
                                    ),
                                    selected = 'adult')),
     conditionalPanel(condition="input.tab=='map' && input.selectSpecies == 'BANDEDRUDDERFISH' ||
                        input.selectSpecies == 'BLACKGROUPER'",
                      selectInput("shrimpLifestage", h3("Select lifestage:"),
                                  c("Fertilized eggs"= "fertilizedEgg",
                                    "Larvae, pre-settlement postlarvae" = "larvae",
                                    "Late postlarvae, juveniles" = "latePostlarvaeJuvenile",

                                    "Sub-adults" = "subAdult",
                                    "Non-spawning adults" = "adult",
                                    "Spawning adults" = "spawningAdult"
                                  ),
                                  selected = 'adult')))),
  dashboardBody()
)

server.R

server <- function(input, output) {  

mapSpecies <- reactive({
  switch(input$selectSpecies,
         "ALMACOJACK" = "ALMACOJACK",
         "BANDEDRUDDERFISH" = "BANDEDRUDDERFISH",
         "BLACKGROUPER" = "BLACKGROUPER",
         "BLACKFINSNAPPER" = "BLACKFINSNAPPER",
         "BLUELINETILEFISH" = "BLUELINETILEFISH",
         "BROWNSHRIMP" = "BROWNSHRIMP",
         "COBIA" = "COBIA",
         "CUBERASNAPPER" = "CUBERASNAPPER")
  })

mapfishLifestage <- reactive({
  switch (input$fishLifestage,
          "spawningAdult"="spawningAdult",
          "adult"="adult",
          "earlyJuvenile" = "earlyJuvenile",
          "lateJuvenile" = "lateJuvenile",
          'larvae' = 'larvae',
          'postLarvae' = 'postLarvae',
          "eggs" = "eggs"
  )
})

mapshrimpLifestage <- reactive({
  switch (input$shrimpLifestage,
          "fertilizedEgg" = "fertilizedEgg",
          "latePostlarvaeJuvenile" = "latePostlarvaeJuvenile",
          "adult" = "adult",
          "spawningAdult"="spawningAdult",
          "subAdult" = "subAdult",
          "larvae" = "larvae"

  )
})

}

我希望selectLifestage菜单仅在选择了EFH maps选项卡时出现,并且根据所选择的种类,我想要fishLifestage与shrimpLifestage selectInput菜单进行更改。当conditionPanel条件=两个或更少的语句时,我已经使这个工作,但我需要它来评估至少三个。到目前为止,我的搜索没有运气解决这个问题。任何帮助,将不胜感激!感谢

1 个答案:

答案 0 :(得分:1)

我添加了括号并更改了||条件和&amp;&amp;为第一个条件小组实现你想要的。

ui <- dashboardPage(
  dashboardHeader(title="Essential Fish Habitat Application"),
  dashboardSidebar(
    sidebarMenu(id="tab",
                menuItem ("Welcome", tabName = "welcome"), 
                menuItem("EFH Maps",tabName="map"),
                selectInput("selectSpecies", h3("Select species"),
                            c("Almaco jack" = "ALMACOJACK",
                              "Banded rudderfish" = "BANDEDRUDDERFISH",
                              "Black grouper" = "BLACKGROUPER",
                              "Blackfin snapper" = "BLACKFINSNAPPER",
                              "Blueline tilefish" = "BLUELINETILEFISH",
                              "Brown shrimp" = "BROWNSHRIMP",
                              "Cobia" = "COBIA",
                              "Cubera snapper" = "CUBERASNAPPER"),
                            selected = "ALMACOJACK"),
                conditionalPanel(condition="input.tab=='map' && (input.selectSpecies != 'BANDEDRUDDERFISH' &&
                        input.selectSpecies != 'BLACKGROUPER')",
                                 selectInput("fishLifestage", h3("Select fish lifestage:"),
                                             c("Eggs"= "eggs",
                                               "Larvae" = "larvae",
                                               "Postlarvae" = "postLarvae",
                                               "Early juveniles" = "earlyJuvenile",
                                               "Late juveniles" = "lateJuvenile",
                                               "Adults" = "adult",
                                               "Spawning adults" = "spawningAdult" 
                                             ),
                                             selected = 'adult')),
                conditionalPanel(condition="input.tab=='map' && (input.selectSpecies == 'BANDEDRUDDERFISH' ||
                        input.selectSpecies == 'BLACKGROUPER')",
                                 selectInput("shrimpLifestage", h3("Select shrimp lifestage:"),
                                             c("Fertilized eggs"= "fertilizedEgg",
                                               "Larvae, pre-settlement postlarvae" = "larvae",
                                               "Late postlarvae, juveniles" = "latePostlarvaeJuvenile",

                                               "Sub-adults" = "subAdult",
                                               "Non-spawning adults" = "adult",
                                               "Spawning adults" = "spawningAdult"
                                             ),
                                             selected = 'adult')))),
  dashboardBody()
)

希望它有所帮助!