gganimate icon indicating copy to clipboard operation
gganimate copied to clipboard

Use of shiny progress indicators with animate?

Open ixodid198 opened this issue 5 years ago • 1 comments

I have a shiny app that allows a user to create an animated gif. This takes some time. Is it possible to use a shiny progress indicator to provide feedback to the user?

The code below uses a "please wait" indicator from shinycssloaders. However, a progress bar would convey more information.

library(shiny)
library(tidyverse)
library(gganimate)
library(shinycssloaders)

ui <- fluidPage(
  plotOutput("anim_plot") %>% withSpinner(color="red", type = 6),
  
  fluidRow(
    column(3,  
           actionButton("make_plot", "Create")
    )
  )
)

server <- function(input, output, session) {
  library(shiny)

  output$anim_plot <- renderImage({
    list(src = "www/tmp/sq_border.png",
         contentType = 'image/png',
         height = 400,
         width = 400
    )
  }, deleteFile = FALSE)
  
  plot <- function(){
    ggplot(mtcars, aes(factor(cyl), mpg)) + 
      geom_boxplot() + 
      
      transition_states(
        gear,
        transition_length = 2,
        state_length = 1
      )  
  }
  
  make_gif <- function(p, speed){
    animate(p, nframes = speed, fps = 20,
            renderer = gifski_renderer(loop = FALSE), start_pause = 15)
  }
  
  # Button Pressed
  observeEvent(input$make_plot, {
    output$anim_plot <- renderPlot({
      p <- plot()
      speed <- 60
      ani <- make_gif(p, speed)  
      
      # Save animated gif
      anim_save(filename = "www/tmp/ani.gif", animation = ani)
      
      output$anim_plot <- renderImage({
        list(src = "www/tmp/ani.gif",
             contentType = 'image/gif')
      }, deleteFile = FALSE)  
    })
  })
}  

shinyApp(ui = ui, server = server)  

ixodid198 avatar Mar 11 '19 21:03 ixodid198

Love to see this in next release.

AndyBunn avatar Jun 20 '23 21:06 AndyBunn