当数字变更后调用另一个RenderPlot时,如何停止RenderPlot运行?

发布于 2025-01-26 01:11:09 字数 805 浏览 2 评论 0原文

当我通过连续单击“快速”按钮来更改numericInput值时,我的闪亮应用程序的过渡非常长。

一旦新的numericInput已更改,如何停止以前的代码运行?我不知道我是否清楚地解释了我的问题。是否可以将GIF放入Stackoverflow帖子中?

尝试使用按钮了解我的问题

# USER INTERFACE 
ui <-  fluidPage(
  sidebarLayout(
    sidebarPanel(numericInput("sd", p("sd"), value = 0.1, step = 0.1)),
    mainPanel(plotOutput("plot"))
  )
)

# SERVER 
server <- function(input, output) {
  
  output$plot<- renderPlot({
    x <- seq(-1, 1, length.out = 50000)
    plot(x, x + rnorm(50000, sd = input$sd), ylab =  "y")
  })
  
}

shinyApp(ui = ui, server = server)

The transitions of my Shiny app are very long when I change a numericInput value by clicking on the button "quickly" several times in a row .

How can I stop the previous code from running once a new numericInput has changed ? I don't know if I explained my problem clearly. Is it possible to put a GIF in a StackOverflow post ?

Try to play with the button to understand my problem
enter image description here

# USER INTERFACE 
ui <-  fluidPage(
  sidebarLayout(
    sidebarPanel(numericInput("sd", p("sd"), value = 0.1, step = 0.1)),
    mainPanel(plotOutput("plot"))
  )
)

# SERVER 
server <- function(input, output) {
  
  output$plot<- renderPlot({
    x <- seq(-1, 1, length.out = 50000)
    plot(x, x + rnorm(50000, sd = input$sd), ylab =  "y")
  })
  
}

shinyApp(ui = ui, server = server)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2025-02-02 01:11:09

您无法在不设置某种形式的情况下启动计算后取消计算
子过程处理。但是,即使在这种情况下,这也无济于事,
因为耗时的操作是实际的图形渲染。你会
需要一个自定义等效于renderplot()来处理它。

您可以在这里做的最好的是debounce()输入,以便您
直到输入值已经在一段时间内解决了一段时间之前,才会开始绘制:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(numericInput("sd", p("sd"), value = 0.1, step = 0.1)),
    mainPanel(plotOutput("plot"))
  )
)

server <- function(input, output) {
  input_sd <- debounce(reactive(input$sd), 400)
  output$plot <- renderPlot({
    x <- seq(-1, 1, length.out = 50000)
    plot(x, x + rnorm(50000, sd = input_sd()), ylab = "y")
  })
}

shinyApp(ui = ui, server = server)

或者,即使更改输入太快仍然是一个问题,即使拒绝,接受并使用提交按钮明确触发绘图。

You can’t cancel a computation once it’s started without setting up some sort
of subprocess processing. However, even that would not help in this case,
because the time-consuming operation is the actual graphics rendering. You’d
need a custom equivalent to renderPlot() to handle that.

Likely the best you can do here is to debounce() the input, so that you
won’t start plotting until the input value has settled for some amount of time:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(numericInput("sd", p("sd"), value = 0.1, step = 0.1)),
    mainPanel(plotOutput("plot"))
  )
)

server <- function(input, output) {
  input_sd <- debounce(reactive(input$sd), 400)
  output$plot <- renderPlot({
    x <- seq(-1, 1, length.out = 50000)
    plot(x, x + rnorm(50000, sd = input_sd()), ylab = "y")
  })
}

shinyApp(ui = ui, server = server)

Or if changing inputs too quickly is still a problem even when debounced, accept that and use a submit button to explicitly trigger the plotting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文