【问题标题】:How to create and display an animated GIF in Shiny?如何在 Shiny 中创建和显示动画 GIF?
【发布时间】:2016-05-27 02:30:15
【问题描述】:

我可以将保存的文件加载为图像,但无法使用gganimate 直接执行此操作。很高兴知道渲染 GIF 的其他方法,但知道如何渲染 gganimate 会真正解决我的问题。

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)

【问题讨论】:

    标签: shiny gif gganimate


    【解决方案1】:

    现在gganimate 有一个更新的破坏版本,@kt.leap 的答案已被弃用。以下是对我有用的新 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)
    

    【讨论】:

    • 我收到一条错误消息:“不支持 gg 对象的动画”
    • @jlp 此代码仍然适用于我。检查您是否更新了所有内容,包括gifski 包。 stackoverflow.com/questions/58336048/…
    【解决方案2】:

    我正在处理同样的问题,只找到了你的问题,没有答案......但你的措辞提醒我renderPlot 很挑剔:

    它不会向浏览器发送任何图像文件——图像必须由使用 R 的图形输出设备系统的代码生成。 renderPlot()无法发送其他创建图像的方法...在这些情况下的解决方案是renderImage()函数。 source

    修改那篇文章中的代码可以得到以下结果:

    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)
    

    【讨论】:

    • 抱歉延迟回来。生活挡道了。您的解决方案需要将文件保存到磁盘。我已经在我的问题中提到我能够加载保存的文件,所以这不会增加太多。
    • 据我了解,它不会将文件保存到磁盘。这是renderImage 使用的文件,而不是renderPlot 框架。所以它会直接渲染gganimate,而无需做任何进一步的事情,但它使用renderImage
    猜你喜欢
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2019-01-04
    • 2010-11-14
    • 2011-07-18
    相关资源
    最近更新 更多