gpt4 book ai didi

r - Shiny - 在 Ui.R 中检测输入变化并发出警告

转载 作者:行者123 更新时间:2023-12-05 01:44:14 26 4
gpt4 key购买 nike

我有一个有很多输入的应用程序,它使用 actionButton() 进行一些计算,这需要花费大量时间,包括读取一些文件和绘制 map 。

在一些用户测试中,一些反馈说在调整输入后需要重新按下 actionButton() 是不直观的。我想呈现一条警告消息(阅读“有用的提醒”),用户需要重新按下 actionButton()。如果此消息仅在自上次按下 actionButton() 后输入更改后呈现,我希望如此。

到目前为止,我已经尝试在“eventReactive()”中使用全局变量并尝试使用 identical() 但没有成功。

我的问题是:当任何输入自上次按下按钮后发生变化时,我如何才能提供有用的提醒以重新按下 actionButton()?。

这是一个应用程序的最小示例,它有很多输入,需要一些时间才能输出一些内容

## app.R ##
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
#arbritray inputs
sliderInput("in1", label = "Input 1", value=1,min=0,max=5),
sliderInput("in2", label = "Input 2", value=2,min=0,max=5),
sliderInput("in3", label = "Input 3", value=3,min=0,max=5),
actionButton("StartCalculation","Calculate")
),
dashboardBody(
textOutput("answer")
)
)

server <- function(input, output) {

out <- eventReactive(input$StartCalculation,{
Sys.sleep(2) #simulate long computation
input$in1+input$in2+input$in3
})

output$answer <- renderText({
out()
})
}

shinyApp(ui, server)

最佳答案

您可以在服务器内部使用 reactiveVal 来保存“状态”(已更改或未更改)。只需确保在按下按钮或输入更改时更新状态即可。

library(shiny)
library(shinydashboard)

ui <- fluidPage(
inputPanel(
#arbritray inputs
sliderInput("in1", label = "Input 1", value=1,min=0,max=5),
sliderInput("in2", label = "Input 2", value=2,min=0,max=5),
sliderInput("in3", label = "Input 3", value=3,min=0,max=5),
actionButton("StartCalculation","Calculate")
),
textOutput("answer"),
textOutput("status")
)

server <- function(input, output) {
status <- reactiveVal()

out <- eventReactive(input$StartCalculation,{
status("up to date")
Sys.sleep(2) #simulate long computation
input$in1+input$in2+input$in3
})

observeEvent({list(input$in1,input$in2,input$in3)},
{status("Needs recalculation")})

output$answer <- renderText({out()})

output$status <- renderText({status()})
}

shinyApp(ui, server)

关于r - Shiny - 在 Ui.R 中检测输入变化并发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46732849/

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