8

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)
TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54

2 Answers2

11

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)
Phil
  • 7,287
  • 3
  • 36
  • 66
  • I get an error message: "animation of gg objects not supported" – jlp Oct 24 '19 at 15:41
  • @jlp This code still works for me. Check that you have everything updated, including the `gifski` package. https://stackoverflow.com/questions/58336048/error-in-animate-default-animation-of-gg-objects-not-supported – Phil Oct 24 '19 at 15:54
5

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)
kt.leap
  • 93
  • 1
  • 7
  • Sorry for the delay in getting back. Life got in the way. Your solution requires saving a file to the disk. I already mentioned in my question that I'm able to load saved files so this doesn't add much. – TheComeOnMan Sep 25 '16 at 09:09
  • As far as I understand, it doesn't save the file to disk. It's a file that is used by `renderImage`, as opposed to the `renderPlot` framework. So it'll render `gganimate` directly, without having to do anything further, but it does so using `renderImage`. – kt.leap Jan 05 '17 at 02:46