如何在Shiny中使用带有reactValues的去抖动

时间:2019-05-24 16:22:24

标签: r shiny reactive

我知道我可以将debounce与react()一起使用,这是我需要的行为,但我想改用reactValues()。

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)
}

但是我宁愿使用reactValues()而不是react()。 有什么方法可以将debounce与reactValues()结合使用吗? 这不起作用:

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不是反应式吗?

1 个答案:

答案 0 :(得分:2)

尝试一下。我在()之后删除了values$text,因为您需要函数/表达式,而不是解析后的值:

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)