Reputation: 3225
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?
Upvotes: 2
Views: 288
Reputation: 3225
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)
Upvotes: 3