首页 > 解决方案 > 如何在闪亮的反应值中使用去抖动

问题描述

我知道我可以像这样将 debounce 与 reactive() 一起使用,这是我需要的那种行为,但我想改用 reactiveValues()。

ui <- fluidPage(
      textInput(inputId = "text",
                label = "To see how quickly..."),
      textOutput(outputId = "text")
)

server <- function(input, output, session) {
      text_input <- reactive({
            input$text
      })

      debounce(text_input, 2000)

      output$text <- renderText({
            text_input()
      })
}
shinyApp(ui, server)
}

但我更喜欢使用 reactiveValues() 而不是 reactive()。有什么方法可以通过 reactiveValues() 使用 debounce?这不起作用:

ui <- fluidPage(
  textInput(inputId = "text",
            label = "To see how quickly..."),
  textOutput(outputId = "text")
)

server <- function(input, output, session) {

  values <- reactiveValues()


  observe({
    values$text= function(x)input$text

  values$t <-
    debounce(values$text(),2000)

  })


  output$text <- renderText({
    values$t()
  })
}
shinyApp(ui, server)

我得到一个错误Warning: Error in r: could not find function "r",我猜是因为values不是反应式表达?

标签: rshinyreactive

解决方案


尝试这个。我删除了()aftervalues$text因为你想要函数/表达式,而不是解析的值:

library(shiny)

ui <- fluidPage(
  textInput(inputId = "text",
            label = "To see how quickly..."),
  textOutput(outputId = "text")
)

server <- function(input, output, session) {
  values <- reactiveValues()
  
  observe({
    values$text <- function(x) {input$text}
    values$t <- debounce(values$text, 2000)
  })
  
  output$text <- renderText({
    values$t()
  })
}

shinyApp(ui, server)

推荐阅读