8

我可以将保存的文件加载为图像,但无法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)
4

2 回答 2

11

现在有一个更新的破坏版本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)
于 2018-11-01T16:19:18.730 回答
5

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

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

修改该文章中的代码可为您提供以下内容:

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)
于 2016-05-17T02:00:41.310 回答