r-闪亮:让renderui对下拉列表反应而不是submitbutton



如何使renderui对用户做出反应,而无需单击我的submitbutton,从下拉列表中选择不同的值?

我有一个包含3件事的wellpanel:
1)我的下拉列表
2)一组动态输入(由我的renderui函数创建,取决于#1中的选择)
3)tismbutton

所需的行为:更改下拉式选择可为用户提供不同的输入小部件。当他们准备好以获取所选输入的结果时,他们单击"提交",然后在Mainpanel中获得结果。

问题:我的renderui仅在单击"提交"后的下拉选择中反应。据我所知,我需要隔离某些东西或使用观察者,但是我无法弄清楚。

简化的示例:

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))

您需要引入两个更改。

  1. submitButton更改为actionButton(请参阅@Daattali的注释)

  2. 隔离renderText,并使其对动作键反应。

请参见下面的代码。

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))

最新更新