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)
GIFs are also easy to open through web-based browsers, including Chrome, Firefox, and Internet Explorer. In the case of Internet Explorer, simply click on the File menu and then Open. Select Browse followed by All Files. Click on the GIF file name and then Open.
Go to GIPHY's site and select the “CREATE” button in their header. For the animated GIF, select Slideshow. Organize them in the order that you want them to animate. Using the slider tool, select how fast you want the layers to loop.
Insert By URL in Docs, Slides, Sheets, Drawings, etc. Inside your document/slide, go to the Insert menu. Copy and paste the image address you copied above. Click Select to insert the image. Voila!
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)
I was dealing with the same issue and found only your question and no answers... But the way you phrased it reminded me that renderPlot
is finicky:
it won’t send just any image file to the browser – the image must be generated by code that uses R’s graphical output device system. Other methods of creating images can’t be sent by
renderPlot()
... The solution in these cases is therenderImage()
function. source
Modifying the code from that article gives you the following:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With