条件面板基于用户选择的输入数量

时间:2017-06-07 16:31:29

标签: r user-interface shiny

我正在尝试根据selectInput中选择的条目数来填充不同数量的字段,其中multiple = TRUE。因此,如果选择“numfields”输入中的1个条目,则会出现第一个conditionalPanel,依此类推。我现在所拥有的输入显示我希望在没有任何用户输入的条件下输入。

$response = getAmazonData("de", $parent);
Print_r($response);

此外,在相关说明中,Shiny自动默认为selectInput字段选择没有条目,其中multiple = TRUE。有没有办法让它选择第一个条目,就像多次= FALSE时一样?

感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:2)

这是通过服务器的另一种解决方案。 显而易见的解决方案是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)

答案 1 :(得分:1)

您可以使用selectInput()参数在selected =中设置默认选择:

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

condition =中的conditionalPanel()参数将文字解释为JS而不是R

检查输入&#39;计数&#39;你应该使用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 = "")
    )
  )
)