在Shiny中更改dygraph的dyRangeSelector的反应时间

eks*_*oem 1 r dygraphs shiny

我正在构建一个Shiny应用程序,我想在其中使用dyRangeSelectorfrom dygraphs提供输入期间。

我的问题是,我只希望在选择器收到“ MouseUp”事件时(即,用户完成选择时间段时)触发反应式更改。现在,选择器移动时将调度事件,这会导致应用程序滞后,因为每个周期完成的计算都需要几秒钟。从本质上讲,闪亮是太这里我的口味反应(我圆知道这是错误的方式-通常我们想要的应用程序是超级反应)。

我可以在调度响应请求时进行修改吗?

这是一个显示问题的小例子。

library(quantmod)
library(shiny)
library(dygraphs)
library(magrittr)

# Create simple user interface
ui <- shinyUI(fluidPage(

    sidebarLayout(
    sidebarPanel(
            dygraphOutput("dygraph")
            ),    
    mainPanel(
            plotOutput("complicatedPlot")
            )
    )
))

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

    ## Read the data once.                                                                                       
    dataInput <- reactive({
    getSymbols("NASDAQ:GOOG", src = "google",
                   from = "2017-01-01",
                   auto.assign = FALSE)
    })

    ## Extract the from and to from the selector    
    values <- reactiveValues()    

    observe({
        if (!is.null(input$dygraph_date_window)) {
            rangewindow <- strftime(input$dygraph_date_window[[1]], "%Y-%m-%d")
            from <- rangewindow[1]
            to <- rangewindow[2]
        } else {
            from <- "2017-02-01"
            to <- Sys.Date()+1
        }
        values[["from"]] <- from
        values[["to"]] <- to
    })

    ## Render the range selector    
    output$dygraph <- renderDygraph({
        dygraph(dataInput()[,4]) %>% dyRangeSelector() %>% dyOptions(retainDateWindow = TRUE)
    })

    ## Render the "complicated" plot
    output$complicatedPlot <- renderPlot({
        plot(1,1)
        text(1,1, values[["from"]])
        Sys.sleep(1) ## Inserted to represent computing time
    })
})

## run app                                                                                                                                                                                                                         
runApp(list(ui=ui, server=server))
Run Code Online (Sandbox Code Playgroud)

zde*_*lak 5

Shiny中有一个函数debounce可能非常适合您的需求。如果将限制重写为反应性表达式(而不是观察值),则可以将其包装到debounce指定时间(以毫秒为单位)中,以等待评估。这是一个1000ms的例子:

library(quantmod)
library(shiny)
library(dygraphs)
library(magrittr)

# Create simple user interface
ui <- shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      dygraphOutput("dygraph")
    ),    
    mainPanel(
      plotOutput("complicatedPlot")
    )
  )
))

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

  ## Read the data once.                                                                                       
  dataInput <- reactive({
    getSymbols("NASDAQ:GOOG", src = "google",
               from = "2017-01-01",
               auto.assign = FALSE)
  })

  ## Extract the from and to from the selector    
  values <- reactiveValues()    

  limits <- debounce(reactive({
    if (!is.null(input$dygraph_date_window)) {
      rangewindow <- strftime(input$dygraph_date_window[[1]], "%Y-%m-%d")
      from <- rangewindow[1]
      to <- rangewindow[2]
    } else {
      from <- "2017-02-01"
      to <- Sys.Date()+1
    }
    list(from = from,
         to = to)
  }), 1000)

  ## Render the range selector    
  output$dygraph <- renderDygraph({
    dygraph(dataInput()[,4]) %>% dyRangeSelector() %>% dyOptions(retainDateWindow = TRUE)
  })

  ## Render the "complicated" plot
  output$complicatedPlot <- renderPlot({
    plot(1,1)
    text(1,1, limits()[["from"]])
    Sys.sleep(1) ## Inserted to represent computing time
  })
})

## run app                                                                                                                                                                                                                         
runApp(list(ui=ui, server=server))
Run Code Online (Sandbox Code Playgroud)

这基本上意味着反应式表达式必须返回相同的值至少1秒钟才能发送到其依赖项。您可以尝试最好的时间。