gpt4 book ai didi

r - Shiny:使 renderUI 对下拉列表使用react,而不是 submitButton

转载 作者:行者123 更新时间:2023-12-04 12:32:20 26 4
gpt4 key购买 nike

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

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

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

问题:我的 renderUI 仅在单击提交按钮后对下拉选择使用react。据我所知,我需要隔离某些东西或使用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))

最佳答案

您需要引入两个更改。

  • 更改submitButtonactionButton (见@daattali 的评论)
  • 隔离 renderText ,并使其对 actionButton 产生 react 。

  • 请参阅下面的代码。
    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))

    关于r - Shiny:使 renderUI 对下拉列表使用react,而不是 submitButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41404693/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com