在 Shiny 上渲染或下载动画 GIF

Ell*_*ar1 5 shiny shiny-server

我打算制作一个 Shiny 应用程序,用户上传一个包含位置数据的文件,该应用程序返回一个显示这些位置数据的动画 gif(在进行了一些计算之后)。当应用程序在我的计算机上本地运行时,一切正常。

此外,当在线运行时,大多数元素运行良好。我无法弄清楚的是,当应用程序部署在 Shinyapps.io 时,如何将动画 gif 返回给用户。无论我是尝试通过 renderImage 还是在 downloadButton 中返回它,都会出现问题。

我尝试了无数的 SaveGIF 和 gganimate 组合,结合了 downloadHandler 或 renderImage。我在这里这里这里寻求灵感,仅举几例。我也看到过类似问题的线程(例如,这里)。我慢慢开始失去理智,非常感谢解决方案或提示。

我很确定有一些关于闪亮在线的内部文件存储规则和系统的东西我只是不明白。例如,将(计算出的)数据的 png 文件返回给 renderImage 或 downloadHandler(如此)工作正常。但是,当输出格式为 gif 时,我永远不会正确。

下面显示了使用 renderImage 的尝试。我已经从文件输入的东西中抽象出来了,所以下面的最小可复制示例制作了一个简单的虚拟数据集,并在在单独的图片中进行动画处理后将其放入临时目录。当应用程序在审查窗格中时,它工作正常。部署后,我得到“这是替代文本”。似乎我可以在临时目录中看到单个图像,但不知何故动画 GIF 丢失了。关于如何在浏览器中作为图像或准备下载的文件返回动画 GIF 的解决方案或提示,这将是非常高兴的,而不是高调的。

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)
Run Code Online (Sandbox Code Playgroud)

小智 0

https://groups.google.com/forum/#!msg/shiny-discuss/1Pq_awTkj_A/g7jsk6AWjqQJ 实际上,saveGIF 中的 outdir 参数存在一个错误。幸运的是,作者已经解决了这个问题。您可以删除 outdir=...,并保留 movie.name = 'test.gif'。src = Paste0(tempdir(),"/test.gif" 下面的部分应更改为 src='test.gif'。这应该可以!