问题
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)
回答1:
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!
来源:https://stackoverflow.com/questions/49013525/render-or-download-animated-gif-on-shiny