简体   繁体   中英

Retain zoom in dygraph with Shiny observer

Suppose I have the following app: (minimally reproducible example)

library(shiny)
library(dygraphs)
library(xts)
runApp(
  list(
    ui = 
      mainPanel(
        tableOutput(outputId="dataTable")
        ,dygraphOutput("testGraph")
      ),

    server = function(input, output, session) {

      myReact <- reactiveValues(df = data.frame(time_stamp = as.integer(Sys.time()), foo = rnorm(1)))

      readTimestamp <- function() Sys.time()
      readValue <- function(){
        data.frame(time_stamp = as.integer(Sys.time()), foo = rnorm(1))
      }
      data <- reactivePoll(5*1000, session, readTimestamp, readValue)  # Add to data frame every x seconds 

      observe({
        myReact$df <- rbind(data(), isolate(myReact$df))
      })

      output$dataTable <- renderTable({
        head(myReact$df, 10)  
      })

      output$testGraph <- renderDygraph({
        x <- myReact$df
        x.ts <- xts(x = x[,2], order.by = as.POSIXct(x[[1]], origin = "1970-01-01"))
        dygraph(x.ts, main = "This should work")

      })

    })
)

The issue I'm having is: when a user zooms in, and the reactivePoll() executes (in this case, every 5 seconds) thereby appending to the reactiveValues(), the dygraph re-renders and zooms out. Is there a way to retain the zoom?

Shout-out to @jjallaire on Github for helping me out with this: https://github.com/rstudio/dygraphs/issues/162

Basically, all you need to do is add an extra argument in dyOptions called retainDateWindow like so:

dygraph(x.ts, main = "This should work") %>%
   dyOptions(retainDateWindow = TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM