gganimate
gganimate copied to clipboard
Use of shiny progress indicators with animate?
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)
Love to see this in next release.