5

I intend to make a Shiny app where the user uploads a file with location data and the app returns an animated gif that displays these location data (after having done some calculations as well). It all works well when the app is run locally on my computer.

Also, when running online most elements work well. The thing I cannot figure out is how to return the animated gif to the user when the app is deployed at shinyapps.io. Regardless of whether I try to return it via renderImage or in a downloadButton something goes wrong.

I have tried countless combinations of SaveGIF and gganimate, combined with either a downloadHandler or a renderImage. I have sought inspiration here, here and here just to name a few. I have also seen threads with similar questions (e.g., here). I am slowly beginning to lose my mind over this and would really appreciate a solution or a tip.

I am quite sure there is something about shiny online's internal file storage rules and system I just do not get. For instance, returning png files of the (calculated) data to either renderImage or downloadHandler (as here) works fine. However, when the output format is gif I never get it right.

Below shows an attempt with renderImage. I have abstracted from the file input stuff, so the minimal reproducible example below makes a simple dummy dataset and puts it to the temporary directory after being animated in separate pictures. When the app is in review pane it works fine. When deployed I get "This is alternate text". It seems that I can see the individual images in the temporary directory but somehow the animated GIF gets lost. It would be very happy, no estatic, about a solution or a tip concerning how to return an animated GIF either in the browser as an image or a file ready for download.

library(shiny)
library(animation)

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

#Server
server <- function(input, output) {

data1_reactive <- reactive({
x <- c(1,2,3)
y <- c(2,4,5)
data_try <- data.frame(x,y)
return(data_try)
})

output$plot1 <- renderImage({
saveGIF(

  for (i in 1:3) {
    plot(data1_reactive()$x,data1_reactive()$y,type="p",col="black",xlim=c(0,10),ylim=c(0,10),pch=19)
    lines(data1_reactive()[i,]$x,data1_reactive()[i,]$y,type="p",col="red",xlim=c(0,10),ylim=c(0,10),pch=19)

    ani.pause()}, movie.name = 'test.gif', interval = 0.3,ani.width = 1400, ani.height = 900,outdir=tempdir())

# Return a list containing the filename
list(src = paste0(tempdir(),"/test.gif"),
     contentType = 'image/gif',
      alt = "This is alternate text"
)}, deleteFile = FALSE)


}

shinyApp(ui, server)
Ellesar1
  • 51
  • 1
  • 2
  • have you seen this? https://groups.google.com/forum/#!topic/shiny-discuss/32WtTVOqYK0. Look at Winston's answer. – MLavoie Feb 27 '18 at 20:15
  • Yes, at least similar approaches, I have seen. If I should incorporate Winston's comment I would change output$plot1 like this:. However, I get the exact same result unfortunately. Right before saveGIF i would add: filename <- tempfile(fileext = ".gif") Within saveGIF I would change movie.name='test.gif' to movie.name=filename In returning the list I would replace src=paste0(tempdir(),"/test.gif") with src=filename. – Ellesar1 Feb 28 '18 at 07:59

1 Answers1

0

https://groups.google.com/forum/#!msg/shiny-discuss/1Pq_awTkj_A/g7jsk6AWjqQJ Actually there was a bug regarding the outdir parameter in saveGIF. Fortunately, the author had fixed that. You could just get rid of the outdir=...,and keep movie.name = 'test.gif'. The part below src = paste0(tempdir(),"/test.gif" should change to src='test.gif'. That should work!

  • Hi Angela, thanks for the input. Can you get it to work in the online version with your proposed fix? Unfortunately, I still get "This is alternate text" and no GIF displaying. Let me stress that it works perfectly in the offline-review pane - the problem seems to arise when it is launched in the online Shiny version. – Ellesar1 Jul 07 '18 at 13:11
  • Hi Ellesar! I just realize that it still does not work. Previously the gif was shown on the online Shiny version because I uploaded the gif generated by the offline version. So actually it is a fake one. I think the problem is related to the library SaveGIF. It requires the software ImageMagick, which may not exist in the online Shiny version. – Angela Wu Jul 10 '18 at 02:10