2017-06-07 23 views
0

我想根據selectInput中multiple = TRUE中所選條目的數量來填充不同數量的字段。因此,如果選擇「numfields」輸入中的1個條目,則會出現第一個conditionalPanel,依此類推。我現在所顯示的是我想在沒有任何用戶輸入的情況下有條件輸入。基於用戶所選輸入數量的條件面板

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) 

此外,在一個相關的說明,閃亮自動默認爲不選擇用於selectInput字段中的條目,其中多個= TRUE。有沒有辦法讓它選擇第一個條目,就像它在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(輸出,'檢查',suspendWhenHidden = FALSE)'必須在反應函數之後,......從未注意到這一點。學會了smthg :)。 – BigDataScientist

+0

現在我們走了,現在它按預期工作,我將它標記爲正確的,因爲其他答案會填充所有字段,直到選中其中一個條目,而您的解決方案顯示空白頁面直到選中某個選項。 – User247365

1

您可以使用selected =參數設置在selectInput()默認的選擇:

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

condition =參數在conditionalPanel()需要一個文字多數民衆贊成解釋爲JS,而不是R

要檢查輸入 '計數',你應該使用input.numfields.length

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

下面的完整ui

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