如何让renderUI响应用户从下拉列表中选择不同的值而不必点击我的submitButton?
我有一个包含3件事的wellPanel:
1)我的下拉列表
2)一组动态输入(由我的renderUI函数创建,取决于#1中的选择)
3)submitButton
所需行为:对下拉选项的更改为用户提供了不同的输入窗口小部件。当他们为所选输入的结果做好准备时,他们会单击submitButton,然后他们会在mainPanel中获得结果。
问题:我的renderUI仅在单击submitButton后对下拉选择作出反应。据我所知,我需要隔离一些东西或使用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))
答案 0 :(得分:5)
您需要引入两项更改。
将submitButton
更改为actionButton
(请参阅@daattali的评论)
隔离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))