Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and display an animated GIF in Shiny?

I'm able to load a saved file as an image but unable to use gganimate to do it directly. Alternate ways of rendering GIFs would be nice to know but knowing how to render gganimate specifically would really solve my problem.

library(gapminder)
library(ggplot2)
library(shiny)
library(gganimate)
theme_set(theme_bw())

ui <- basicPage(
    plotOutput("plot1")
)

server <- function(input, output) {
    output$plot1 <- renderPlot({
        p = ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) +
  geom_point() +
  scale_x_log10()

       gg_animate(p)

    })

}

shinyApp(ui, server)
like image 391
TheComeOnMan Avatar asked Feb 16 '16 01:02

TheComeOnMan


People also ask

How do you display a GIF?

GIFs are also easy to open through web-based browsers, including Chrome, Firefox, and Internet Explorer. In the case of Internet Explorer, simply click on the File menu and then Open. Select Browse followed by All Files. Click on the GIF file name and then Open.

How do you make a dynamic GIF?

Go to GIPHY's site and select the “CREATE” button in their header. For the animated GIF, select Slideshow. Organize them in the order that you want them to animate. Using the slider tool, select how fast you want the layers to loop.

Can you embed GIF in Google Sheets?

Insert By URL in Docs, Slides, Sheets, Drawings, etc. Inside your document/slide, go to the Insert menu. Copy and paste the image address you copied above. Click Select to insert the image. Voila!


2 Answers

Now that there's a newer breaking version of gganimate, @kt.leap's answer is deprecated. Here's what worked for me with the new gganimate:

library(gapminder)
library(ggplot2)
library(shiny)
library(gganimate)
theme_set(theme_bw())

ui <- basicPage(
    imageOutput("plot1"))

server <- function(input, output) {
    output$plot1 <- renderImage({
    # A temp file to save the output.
    # This file will be removed later by renderImage
    outfile <- tempfile(fileext='.gif')

    # now make the animation
    p = ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, 
      color = continent)) + geom_point() + scale_x_log10() +
      transition_time(year) # New

    anim_save("outfile.gif", animate(p)) # New

    # Return a list containing the filename
     list(src = "outfile.gif",
         contentType = 'image/gif'
         # width = 400,
         # height = 300,
         # alt = "This is alternate text"
         )}, deleteFile = TRUE)}

shinyApp(ui, server)
like image 83
Phil Avatar answered Oct 03 '22 06:10

Phil


I was dealing with the same issue and found only your question and no answers... But the way you phrased it reminded me that renderPlot is finicky:

it won’t send just any image file to the browser – the image must be generated by code that uses R’s graphical output device system. Other methods of creating images can’t be sent by renderPlot()... The solution in these cases is the renderImage() function. source

Modifying the code from that article gives you the following:

library(gapminder)
library(ggplot2)
library(shiny)
library(gganimate)
theme_set(theme_bw())

ui <- basicPage(
    imageOutput("plot1"))

server <- function(input, output) {
    output$plot1 <- renderImage({
    # A temp file to save the output.
    # This file will be removed later by renderImage
    outfile <- tempfile(fileext='.gif')

    # now make the animation
    p = ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, 
      color = continent, frame = year)) + geom_point() + scale_x_log10()

    gg_animate(p,"outfile.gif")

    # Return a list containing the filename
     list(src = "outfile.gif",
         contentType = 'image/gif'
         # width = 400,
         # height = 300,
         # alt = "This is alternate text"
         )}, deleteFile = TRUE)}

shinyApp(ui, server)
like image 40
kt.leap Avatar answered Oct 03 '22 07:10

kt.leap