在更新之前,我可以让Shiny等待更长时间的numericInput吗?

时间:2016-01-22 01:52:23

标签: r shiny

在我的Shiny App中,有一些numericInputselectInput

在键入期间闪烁更新输出,尤其是当用户输入的数字输入速度较慢时。

sumbitButton可以用来停止自动更新。但我宁愿不使用它。

我怎么能让Shiny等待numericInput的更长时间?

感谢您的任何建议。如果我的问题不明确,请告诉我。

1 个答案:

答案 0 :(得分:2)

您可以对使用输入的被动功能使用debounce。 将它设置为2000毫秒对我来说感觉还可以。 如果直接在渲染函数中使用输入,则可能需要在反应函数中创建要在渲染函数中使用的数据。

这里有一个例子:https://shiny.rstudio.com/reference/shiny/latest/debounce.html

## Only run examples in interactive R sessions
if (interactive()) {
options(device.ask.default = FALSE)

library(shiny)
library(magrittr)

ui <- fluidPage(
  plotOutput("plot", click = clickOpts("hover")),
  helpText("Quickly click on the plot above, while watching the result table below:"),
  tableOutput("result")
)

server <- function(input, output, session) {
  hover <- reactive({
    if (is.null(input$hover))
      list(x = NA, y = NA)
    else
      input$hover
  })
  hover_d <- hover %>% debounce(1000)
  hover_t <- hover %>% throttle(1000)

  output$plot <- renderPlot({
    plot(cars)
  })

  output$result <- renderTable({
    data.frame(
      mode = c("raw", "throttle", "debounce"),
      x = c(hover()$x, hover_t()$x, hover_d()$x),
      y = c(hover()$y, hover_t()$y, hover_d()$y)
    )
  })
}

shinyApp(ui, server)
}