【问题标题】:Conditional Panel based on number of user selected inputs基于用户选择的输入数量的条件面板
【发布时间】:2017-11-09 03:17:17
【问题描述】:

我正在尝试根据在多个 = TRUE 的 selectInput 中选择的条目数来填充不同数量的字段。因此,如果选择了“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)

另外,在相关的注释中,Shiny 自动默认没有为 selectInput 字段选择任何条目,其中 multiple = TRUE。有没有办法让它像 multiple = FALSE 一样选择第一个条目?

感谢您的帮助,谢谢。

【问题讨论】:

    标签: r user-interface shiny


    【解决方案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)
    

    【讨论】:

    • 此解决方案因错误Error in .subset2(x, "impl")$outputOptions(name, ...) : check is not in list of output objects 而崩溃,是否可能需要一个软件包才能在您的机器上运行,但不是我的机器?
    • 我做了一个编辑,我只是​​做了一些重构,让它看起来更好。但似乎outputOptions(output, 'check', suspendWhenHidden=FALSE) 必须在反应函数之后,......从来没有注意到这一点。学到了 :)。
    • 好了,现在它按预期工作,我将其标记为正确,因为另一个答案会填充所有字段,直到选择其中一个条目,而您的解决方案确实会显示一个空白页,直到选择了一个选项。
    【解决方案2】:

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

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

    conditionalPanel() 中的 condition = 参数采用解释为 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 = "")
        )
      )
    )
    

    【讨论】:

    • 这按预期工作,但不是设置selected = 'A',有没有办法设置它以便它选择第一个条目,在这种情况下是A,而不指定选择A?我问的原因是,在我的代码中,这个选择输入是基于不同输入的反应性输入,因此,第一个条目将根据不同字段的输入而有所不同。
    • 但是如果它对不同的输入产生反应,它应该被包裹在renderUI() 中,不是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多