2016-12-28 46 views
0

我是Shiny的新手,對SO等類似問題的回答正在幫助我。在下拉菜單中閃亮的顯示只有一個選擇(在幾個之間)

我想在下拉菜單上顯示動態選項,具體取決於用戶在單選按鈕上選擇的內容。但下面的代碼片段只能顯示一個亞種的選擇,一旦用戶選擇,首先#1動物類型,然後#2全部與亞種的選擇。

我有三個變量:#1。動物類型(獅子vs老虎); #2。整體與亞種(單選按鈕); #3。亞種(如果用戶在#2中選擇「全部」,那麼亞種應該等於「不適用」)。

供參考:

  • 虎亞種= {紅,西伯利亞}

  • 獅子亞種= {北非,西南非洲人,德蘭士瓦}

任何幫助理解。謝謝。

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

回答

2

的問題來自於使用ifelse(test, yes, no)當是或否有不同長度的測試:

> 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") 
    } 
+0

謝謝,它的工作! – David

1

ifelse狀態的文檔:

ifelse返回一個具有與測試相同形狀的值,該測試填充了從yes或no中選擇的元素,具體取決於測試元素是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) 
} 
+0

非常感謝!對不起,你的回答在HubertL之後有點......否則,我會投你的答案...... – David

+0

沒問題。更重要的是要知道爲什麼/出了什麼問題。很高興你能夠繼續與你的問題。 – krish

1

幾點:

(1)由於在先前的文章中提到,ifelse是這裏的罪魁禍首。 ?ifelse

ifelse(試驗,是,否)
ifelse與相同的形狀作爲測試

這是一個在你的情況,你應該使用ifelse返回一個值按建議阻止。 (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) 
} 

enter image description here

相關問題