【问题标题】:Shiny: Make renderUI react to drop-down list not submitButtonShiny:使 renderUI 对下拉列表做出反应,而不是 submitButton
【发布时间】:2016-12-31 01:39:11
【问题描述】:

如何让 renderUI 对用户从下拉列表中选择不同的值做出反应,而无需单击我的提交按钮?

我有一个包含 3 个东西的 wellPanel:
1) 我的下拉列表
2) 一组动态输入(由我的 renderUI 函数创建,取决于 #1 中的选择)
3) 提交按钮

期望的行为:对下拉选择的更改为用户提供了不同的输入小部件以供使用。当他们准备好接收所选输入的结果时,他们单击提交按钮,然后在 mainPanel 中获得结果。

问题:我的 renderUI 仅在单击提交按钮后对下拉选择做出反应。据我所知,我需要隔离某些东西或使用observeEvent,但我无法弄清楚。

简化示例:

rm(list = ls())
library(shiny)

ui  <- fluidPage(
  fluidRow(
column(4,wellPanel(
  selectInput("analysis", label = "Type of Analysis:", 
              c("Award Total" = "total", 
                "Award Average" = "average"),
              width = validateCssUnit("70%")),
  uiOutput("filter_box"),
  submitButton()
    )),
column(8, textOutput("sample_text"))
  )
)

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

  output$filter_box <- renderUI({
if(input$analysis == "total"){
  tagList(radioButtons(inputId = "input1", label = "Select One:",c("A", "B", "C"), selected = "A"))
} else {
  tagList(checkboxGroupInput(inputId = "input2", label = "Select all that apply:",c("1","2","3","4","5")),
          dateRangeInput(inputId = "input3", label = "Enter Date Range"))
}
})

output$sample_text <- renderText({
  if(input$analysis == "total"){
    input$input1
  } else if(input$analysis == "average") {
    c(input$input2, input$input3)
  }
 })
}

runApp(list(ui = ui, server = server))

【问题讨论】:

  • 您不需要使用 submitButton,而是使用 actionButton。 submitButtons 很糟糕(闪亮的作者自己反对它),因为它们会导致奇怪的反应行为。抱歉我不会给你解决方案,我只是建议你改用actionButton,这样就不会出现这个问题了

标签: r shiny


【解决方案1】:

您需要引入两个更改。

  1. submitButton 更改为actionButton(参见@daattali 的评论)

  2. 隔离renderText,并使其响应actionButton。

参见下面的代码。

rm(list = ls())
library(shiny)

ui  <- fluidPage(
  fluidRow(
    column(4,wellPanel(
      selectInput("analysis", label = "Type of Analysis:", 
                  c("Award Total" = "total", 
                    "Award Average" = "average"),
                  width = validateCssUnit("70%")),
      uiOutput("filter_box"),
      actionButton(inputId = 'button_1',label = 'Apply Changes')
    )),
    column(8, textOutput("sample_text"))
  )
)

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

  output$filter_box <- renderUI({
    if(input$analysis == "total"){
      tagList(radioButtons(inputId = "input1", label = "Select One:",c("A", "B", "C"), selected = "A"))
    } else {
      tagList(checkboxGroupInput(inputId = "input2", label = "Select all that apply:",c("1","2","3","4","5")),
              dateRangeInput(inputId = "input3", label = "Enter Date Range"))
    }
  })

  output$sample_text <- renderText({
    input$button_1
    isolate({
      if(input$analysis == "total"){
        input$input1
      } else if(input$analysis == "average") {
        c(input$input2, input$input3)
      }
    })

  })
}

runApp(list(ui = ui, server = server))

【讨论】:

  • 太好了,谢谢。我很惊讶我的谷歌搜索没有发现 actionButtons 比 submitButtons 更受欢迎(感谢@daattali)。另一个问题:在 output@sample_text 的 renderText 中,我认为 input$button_1 在那里触发反应,但为什么 input$button_1 的值实际上没有在结果文本中呈现?
  • 渲染的内容是 renderText() 函数返回的内容。在这种情况下,由于没有 return() 语句,这将是最后执行的语句(这里这是以 if/else 为条件的)。您可以使用 return() 使其更明确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
  • 2016-09-05
  • 2013-06-20
相关资源
最近更新 更多