2016-12-28 28 views
0

私はシャイニーを新しくしていますので、私と同様の質問への回答が私を助けています。プルダウンメニューに1つの選択肢(いくつかの中から1つだけ)が表示されています

ラジオボタンでユーザーが選択した内容に応じて、プルダウンメニューに動的な選択肢を表示します。しかし、下のスニペットは、亜種の選択肢を1つだけ表示することができます。ユーザーが選択すると、最初は#1、動物種は#2、亜種全体と亜種の選択が表示されます。

私は3つの変数を持っています:#1。動物型(ライオンvsタイガー); #2。全体亜種(ラジオボタン); #3。 (そしてユーザーが#2で "全体"を選択した場合、亜種は "Not Applicable"に等しくなるはずです)。

FYI:

  • トラの亜種は、= {ベンガル、シベリア}

  • ライオン亜種= {バーバリー、サウスアフリカン、トランスバール}

は、任意のヘルプは理解されます。ありがとう。

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

問題がテストの異なる長さは持っていないyesまたはno 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は、testと同じ形の値を返します。これは、testの要素がTRUEかFALSEかによってyesまたはnoから選択された要素で満たされます。点の

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は、あなたの場合は1であるテスト

として同じ形状で値を返しますifelse

ifelse(テスト、YES、NO)
を言う、あなたは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

関連する問題