In this miniapp, the goal is to display raw series and it's average over the selected range:
library(dygraphs)
library(datasets)
server <- function(input, output) {
reacteddata <- reactive({
dt = cbind(as.xts(ldeaths),ave=NA)
if (!is.null(input$dygraph_date_window)){
start=strftime(input$dygraph_date_window[[1]])
end=strftime(input$dygraph_date_window[[2]])
subset = window(as.xts(ldeaths), start=start, end=end)
ave = rep(mean(subset), length(subset))
dt[index(as.xts(subset)),"ave"] = ave
dt = dt[index(as.xts(subset))]
} else {
dt[,"ave"] = rep(mean(ldeaths), length(ldeaths))
}
dt
})
output$dygraph <- renderDygraph({
dygraph(reacteddata(), main = "Predicted Deaths/Month")
})
}
ui <- fluidPage(
sidebarLayout(
mainPanel(
dygraphOutput("dygraph")
)
)
)
shinyApp(ui = ui, server = server)
It works, even redraws the average line on the zoom ins (using mouse to select zoom date range):
However the catch is that it loses data on each redraw, hence it is impossible to zoom out. Any ideas how to rework it?