Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在下拉菜单中仅显示一个选项(多个选项中的一个)_R_User Interface_Shiny - Fatal编程技术网

R 在下拉菜单中仅显示一个选项(多个选项中的一个)

R 在下拉菜单中仅显示一个选项(多个选项中的一个),r,user-interface,shiny,R,User Interface,Shiny,我对Shiny是个新手,对类似问题的回答对我很有帮助 我想在下拉菜单上显示动态选项,具体取决于用户在单选按钮上的选择。但下面的代码片段只能显示亚种的一种选择,一旦用户选择,首先#1动物类型,然后#2整个与亚种的选择 我有三个变量:#1。动物类型(狮子对老虎)#2.整体与亚种(单选按钮)#3.亚种(如果用户在#2中选择“整体”,则亚种应等于“不适用”) 供参考: 老虎亚种={孟加拉,西伯利亚} 狮子亚种={Barbary,西南非洲,特兰斯瓦尔} 感谢您的帮助。谢谢 library(sh

我对Shiny是个新手,对类似问题的回答对我很有帮助

我想在下拉菜单上显示动态选项,具体取决于用户在单选按钮上的选择。但下面的代码片段只能显示亚种的一种选择,一旦用户选择,首先#1动物类型,然后#2整个与亚种的选择

我有三个变量:#1。动物类型(狮子对老虎)#2.整体与亚种(单选按钮)#3.亚种(如果用户在#2中选择“整体”,则亚种应等于“不适用”)

供参考:

  • 老虎亚种={孟加拉,西伯利亚}

  • 狮子亚种={Barbary,西南非洲,特兰斯瓦尔}

感谢您的帮助。谢谢

    library(shiny)
    if (interactive()) {

    ui <- fluidPage(
     selectizeInput("VarAnimal",
               label = "Animal",
               choices = c("Tiger", "Lion"),
               selected = "Tiger"),

     radioButtons("VarWholeOrSub", 
             "Whole or Sub",
              choices = c("Whole species", "Subspecies"),
              selected = "Whole species"), 

     selectizeInput("VarSubspecies",
               label = "Subspecies",
               choices = c("Not Applicable", "Bengal", "Siberian", "Barbary", "Southwest African", "Transvaal"),
               selected = "")
    )

    server <- function(input, output, session) {
    observe({
       x <- input$VarWholeOrSub

       if (input$VarWholeOrSub == "Whole species"){
         x <- c("Not Applicable")} else{
         x <- ifelse(input$VarAnimal == "Tiger", c("Bengal", "Siberian"), c("Barbary", "Southwest African", "Transvaal"))  
    }

      updateSelectizeInput(session,
                       "VarSubspecies",
                       choices = x)

    })
    }


    shinyApp(ui, server)
    }
库(闪亮)
if(interactive()){

ui当是或否具有不同的测试长度时,使用
ifelse(测试,是,否)
会出现问题:

> ifelse("a"=="a", c("a","b"), c("b","a"))
[1] "a"
> ifelse(rep("a"=="a",2), c("a","b"), c("b","a"))
[1] "a" "b"
在您的情况下(使用3种不同的长度),您应该使用
if

  if (input$VarWholeOrSub == "Whole species"){
    x <- c("Not Applicable")
    } else {
      if(input$VarAnimal == "Tiger")
        x <- c("Bengal", "Siberian")
      else
        x <- c("Barbary", "Southwest African", "Transvaal")
    }
if(输入$VarWholeOrSub==“整个物种”){

x当“是”或“否”具有不同的测试长度时,问题来自于使用
ifelse(测试,是,否)

> ifelse("a"=="a", c("a","b"), c("b","a"))
[1] "a"
> ifelse(rep("a"=="a",2), c("a","b"), c("b","a"))
[1] "a" "b"
在您的情况下(使用3种不同的长度),您应该使用
if

  if (input$VarWholeOrSub == "Whole species"){
    x <- c("Not Applicable")
    } else {
      if(input$VarAnimal == "Tiger")
        x <- c("Bengal", "Siberian")
      else
        x <- c("Barbary", "Southwest African", "Transvaal")
    }
if(输入$VarWholeOrSub==“整个物种”){

ifelse的文档说明:

ifelse返回一个与test形状相同的值,该值由从yes或no中选择的元素填充,具体取决于test的元素是TRUE还是FALSE

library(shiny)
if (interactive()) {

  ui <- fluidPage(
    selectizeInput("VarAnimal",
                   label = "Animal",
                   choices = c("Tiger", "Lion"),
                   selected = "Tiger"),

    radioButtons("VarWholeOrSub", 
                 "Whole or Sub",
                 choices = c("Whole species", "Subspecies"),
                 selected = "Whole species"), 

    selectizeInput("VarSubspecies",
                   label = "Subspecies",
                   choices = c("Not Applicable", "Bengal", "Siberian", "Barbary", "Southwest African", "Transvaal"),
                   selected = "")
  )

  server <- function(input, output, session) {
    observe({

      if (input$VarWholeOrSub == "Whole species"){
        updateSelectizeInput(session, 'VarSubspecies', choices = c("Not Applicable"))
        } else{
          if(input$VarAnimal == "Tiger"){
            updateSelectizeInput(session, 'VarSubspecies', choices = c("Bengal", "Siberian"))
          } else if (input$VarAnimal == "Lion"){
            updateSelectizeInput(session, 'VarSubspecies', choices = c("Barbary", "Southwest African", "Transvaal"))
          }
        }
    })
  }


  shinyApp(ui, server)
} 
库(闪亮)
if(interactive()){

ifelse的文档说明:

ifelse返回一个与test形状相同的值,该值由从yes或no中选择的元素填充,具体取决于test的元素是TRUE还是FALSE

library(shiny)
if (interactive()) {

  ui <- fluidPage(
    selectizeInput("VarAnimal",
                   label = "Animal",
                   choices = c("Tiger", "Lion"),
                   selected = "Tiger"),

    radioButtons("VarWholeOrSub", 
                 "Whole or Sub",
                 choices = c("Whole species", "Subspecies"),
                 selected = "Whole species"), 

    selectizeInput("VarSubspecies",
                   label = "Subspecies",
                   choices = c("Not Applicable", "Bengal", "Siberian", "Barbary", "Southwest African", "Transvaal"),
                   selected = "")
  )

  server <- function(input, output, session) {
    observe({

      if (input$VarWholeOrSub == "Whole species"){
        updateSelectizeInput(session, 'VarSubspecies', choices = c("Not Applicable"))
        } else{
          if(input$VarAnimal == "Tiger"){
            updateSelectizeInput(session, 'VarSubspecies', choices = c("Bengal", "Siberian"))
          } else if (input$VarAnimal == "Lion"){
            updateSelectizeInput(session, 'VarSubspecies', choices = c("Barbary", "Southwest African", "Transvaal"))
          }
        }
    })
  }


  shinyApp(ui, server)
} 
库(闪亮)
if(interactive()){
ui几点:

(1) 如前几篇文章所述,
ifelse
是罪魁祸首。
?如果其他

ifelse(测试,是,否)
ifelse返回一个与test形状相同的值

在您的情况下,您应该按照建议使用if-else块

(2) 您可能希望代码中的硬编码更少,方法是分离数据(可能是全局的,以便以后不需要更改R代码来更改数据)并去掉一些冗余代码:

df <- data.frame(Animals = c("Tiger", "Lion"), 
                 WholeSpecies=rep("Not Applicable", 2),
                 SubSpecies=c("Bengal, Siberian", "Barbary, Southwest African, Transvaal"), 
                 stringsAsFactors = FALSE)
head(df)
#  Animals   WholeSpecies                            SubSpecies
#1   Tiger Not Applicable                      Bengal, Siberian
#2    Lion Not Applicable Barbary, Southwest African, Transvaal

library(shiny)
if (interactive()) {

  ui <- fluidPage(
    selectizeInput("VarAnimal",
                   label = "Animal",
                   choices = df$Animals,
                   selected = df$Animals[1]),

    radioButtons("VarWholeOrSub", 
                 "Whole or Sub",
                 choices = c("Whole species", "Subspecies"),
                 selected = "Whole species"), 

    selectizeInput("VarSubspecies",
                   label = "Subspecies",
                   choices = c(unique(df$WholeSpecies), unlist(strsplit(unique(df$SubSpecies), split=','))),
                   selected = "")
  )

  server <- function(input, output, session) {
    observe({

      x <- df[df$Animals==input$VarAnimal,]$WholeSpecies

      if (input$VarWholeOrSub == "Subspecies"){
        x <- unlist(strsplit(df[df$Animals==input$VarAnimal,]$SubSpecies, split=','))
      }

      updateSelectizeInput(session,
                           "VarSubspecies",
                           choices = x)

    })
  }

  shinyApp(ui, server)
}
df两点:

(1) 如前几篇文章所述,
ifelse
是罪魁祸首。
?如果其他

ifelse(测试,是,否)
ifelse返回一个与test形状相同的值

在您的情况下,您应该按照建议使用if-else块

(2) 您可能希望代码中的硬编码更少,方法是分离数据(可能是全局的,以便以后不需要更改R代码来更改数据)并去掉一些冗余代码:

df <- data.frame(Animals = c("Tiger", "Lion"), 
                 WholeSpecies=rep("Not Applicable", 2),
                 SubSpecies=c("Bengal, Siberian", "Barbary, Southwest African, Transvaal"), 
                 stringsAsFactors = FALSE)
head(df)
#  Animals   WholeSpecies                            SubSpecies
#1   Tiger Not Applicable                      Bengal, Siberian
#2    Lion Not Applicable Barbary, Southwest African, Transvaal

library(shiny)
if (interactive()) {

  ui <- fluidPage(
    selectizeInput("VarAnimal",
                   label = "Animal",
                   choices = df$Animals,
                   selected = df$Animals[1]),

    radioButtons("VarWholeOrSub", 
                 "Whole or Sub",
                 choices = c("Whole species", "Subspecies"),
                 selected = "Whole species"), 

    selectizeInput("VarSubspecies",
                   label = "Subspecies",
                   choices = c(unique(df$WholeSpecies), unlist(strsplit(unique(df$SubSpecies), split=','))),
                   selected = "")
  )

  server <- function(input, output, session) {
    observe({

      x <- df[df$Animals==input$VarAnimal,]$WholeSpecies

      if (input$VarWholeOrSub == "Subspecies"){
        x <- unlist(strsplit(df[df$Animals==input$VarAnimal,]$SubSpecies, split=','))
      }

      updateSelectizeInput(session,
                           "VarSubspecies",
                           choices = x)

    })
  }

  shinyApp(ui, server)
}

df非常感谢!很抱歉,你的答案在HubertL之后出现了一点……否则,我会把你的答案作为投票结果……没问题。更重要的是要知道问题的原因/原因。很高兴你能够继续解决你的问题。非常感谢!很抱歉,你的答案在HubertL之后出现了一点……否则,我会投票给你答案是urs…不是问题。更重要的是要知道问题的原因/原因。很高兴你能够继续解决问题。