2017-06-07 13 views
0

selectInputでmultiple = TRUEで選択されたエントリの数に基づいて、異なる数のフィールドを生成しようとしています。したがって、「numfields」入力から1つの項目が選択された場合、最初の条件付きパネルが表示されます。私が今入力したものは、ユーザー入力なしで条件付きにしたい入力を示しています。ユーザが選択した入力の数に基づく条件付きパネル

a <- c("A","B","C") 

    Choices <- as.character(a) 

    ui <- fluidPage(
    fluidRow(
     selectInput(inputId = "numfields", label = "Select Entries", choices = Choices, multiple = TRUE, selectize = TRUE), 
     conditionalPanel(
      condition = "count(input$numfields) >= 1", 
      textInput(inputId = "field1", label = "First One", value = "") 
     ), 
     conditionalPanel(
     condition = "count(input$numfields) >= 2", 
     textInput(inputId = "field2", label = "Second One", value = "") 
    ), 
     conditionalPanel(
     condition = "count(input$numfields) >= 3", 
     textInput(inputId = "field3", label = "Third One", value = "") 
    ) 
    ) 
    ) 

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

    shinyApp(ui=ui, server=server) 

また、関連するノートでは、自動的にデフォルトでは、multiple = TRUEのselectInputフィールドにはエントリが選択されていません。 multiple = FALSEのときと同じように最初のエントリを選択させる方法はありますか?

ご協力いただきありがとうございます。

答えて

1

サーバーを経由して別のソリューションを。 明白な解決策はrenderUI()だろうが、あなたはcondtionalPanel()を使用する場合:

a <- c("A","B","C") 

Choices <- as.character(a) 

ui <- fluidPage(
    fluidRow(
    selectInput(inputId = "numfields", label = "Select Entries", choices = Choices, multiple = TRUE, selectize = TRUE), 
    conditionalPanel(
     condition = "output.check > 0", 
     textInput(inputId = "field1", label = "First One", value = "") 
    ), 
    conditionalPanel(
     condition = "output.check >= 2", 
     textInput(inputId = "field2", label = "Second One", value = "") 
    ), 
    conditionalPanel(
     condition = "output.check >= 3", 
     textInput(inputId = "field3", label = "Third One", value = "") 
    ) 
) 
) 

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

    output$check <- reactive({ 
    length(input$numfields) 
    }) 

    outputOptions(output, 'check', suspendWhenHidden=FALSE)  
} 

shinyApp(ui=ui, server=server) 
+0

このソリューションは、 '.subset2(x、" impl ")エラーでエラークラッシュします。$ outputOptions(name、...): チェックは出力オブジェクトのリストにありません。あなたのマシンで動作しますが、私のものではありませんか? – User247365

+0

私は編集をしました。私はちょっとリファクタリングを行いました。しかし、 'outputOptions(output、 'check'、suspendWhenHidden = FALSE)はリアクション関数の後に来なければなりません。学んだsmthg :)。 – BigDataScientist

+0

これで、意図したとおりに動作し、正しいものとしてマークしています。これは、項目の1つが選択されるまで、他の回答がすべてのフィールドに入力されるためです。 – User247365

1

あなたはselected =引数を使用してselectInput()でデフォルトの選択を設定することができます。conditionalPanel()

selectInput(inputId = "numfields", 
      label = "Select Entries", 
      choices = Choices, 
      multiple = TRUE, 
      selectize = TRUE, 
      selected = 'A') 

condition =引数がJSではなくRとして解釈だとリテラルを取ります。

が入力をチェックするには 'カウント' あなたは input.numfields.lengthを使用する必要があります。

完全 ui以下
conditionalPanel(
    condition = "input.numfields.length >= 1", 
    textInput(inputId = "field1", label = "First One", value = "") 
) 

:ここ

ui <- fluidPage(
    fluidRow(
    selectInput(inputId = "numfields", 
       label = "Select Entries", 
       choices = Choices, 
       multiple = TRUE, 
       selectize = TRUE, 
       selected = 'A'), 
    conditionalPanel(
     condition = "input.numfields.length >= 1", 
     textInput(inputId = "field1", label = "First One", value = "") 
    ), 
    conditionalPanel(
     condition = "input.numfields.length >= 2", 
     textInput(inputId = "field2", label = "Second One", value = "") 
    ), 
    conditionalPanel(
     condition = "input.numfields.length >= 3", 
     textInput(inputId = "field3", label = "Third One", value = "") 
    ) 
) 
) 
+0

これは意図したとおりに動作しますが、代わりに、 '選択=「A''を設定する、それが選択するように設定することが方法です。 Aを選択することを指定せずに、このインスタンスAである最初のエントリ?私が尋ねる理由は、私のコードでは、この選択入力は異なる入力に基づいて反応するので、最初の入力は異なるフィールドの入力に応じて異なります。 – User247365

+1

しかし、それが異なる入力で反応する場合は、とにかく 'renderUI()'の中にラップする必要がありますか? – BigDataScientist

関連する問題