admin管理员组

文章数量:1287668

I'm working on a shiny app that includes (among other things) a time series animation with sliderInput for the dates. I want to show the time series data as a line graph slowly showing over time. The animation runs, but becomes choppy, since the full plot gets re-drawn every time. I'm certain I've seen a way to smooth it out at some point, but haven't been able to find it again. Ideally, the app wouldn't need to redraw the entire plot at every time stamp.

library(dplyr)
library(ggplot2)
library(shiny)

data <- expand.grid(Date = seq(as.Date("2001-01-01"), as.Date("2001-12-31"), "1 day"), 
            Var = c("Variable1", "Variable2"), Group = c("a", "b")) %>%
        mutate(Val = rnorm(n(), 0, 1),
            Val = ifelse(Group == "b", Val * 2, Val))

server <- function(input, output, session) {
    output$Plot <- renderPlot({
        req(input$DateSlider)
        sub <- data %>%
                filter(Date <= input$DateSlider) 
                            
        ggplot(sub) +
            geom_line(aes(x = Date, y = Val, group = Group, colour = Group)) +
            facet_wrap(~ Var, scales = "free_y", ncol = 1) +
            scale_x_date("Date", limits = range(data$Date))
                                })
                                            }

ui <- fluidPage(
            fluidRow(
                column(7, plotOutput("Plot", width = "100%")),
                column(3, sliderInput("DateSlider", "Animation period",
                          min = min(data$Date), max = max(data$Date), step = 1, ## by day 
                          value = min(data$Date), animate = animationOptions(loop = FALSE, interval = 10[![enter image description here][1]][1]0)))))

shinyApp(ui, server)

本文标签: rAnimated time series in shinychoppyStack Overflow