如何在Shiny中创建和显示动画GIF? [英] How to create and display an animated GIF in Shiny?

查看:313
本文介绍了如何在Shiny中创建和显示动画GIF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将保存的文件作为图像加载,但是无法使用gganimate直接执行.知道另一种呈现GIF的方式很高兴,但是知道如何专门呈现gganimate确实可以解决我的问题.

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)

推荐答案

现在有gganimate的较新版本,@ kt.leap的答案已弃用.这是使用新的gganimate对我有用的内容:

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)

这篇关于如何在Shiny中创建和显示动画GIF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆