gpt4 book ai didi

shiny - 如何在 Shiny 中创建和显示动画 GIF?

转载 作者:行者123 更新时间:2023-12-04 17:08:16 32 4
gpt4 key购买 nike

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

最佳答案

现在有了更新的突破性版本 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)

关于shiny - 如何在 Shiny 中创建和显示动画 GIF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35421923/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com