To control the animation using start/stop/pause buttons.
The animation
package can save the created animation as a HTML page using the saveHTML()
function (example here). But it requires "expr
= an R expression to be evaluated to create a sequence of images" as the first argument.
However, gganimate
creates a gganim
object, and I can't seem to find a function that can save the animation as a HTML page with buttons. The available anim_save()
function does not save as HTML. Is there a workaround to do that?
If you have any ideas please share. For your reference, I'm putting below the gganimate
code from its website.
library(ggplot2)
library(gganimate)
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
# Here comes the gganimate code
transition_states(
gear,
transition_length = 2,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')
The following worked for me in RStudio, though I didn't see "current" listed as an option for device in gganimate::animate
:
library(gganimate)
library(animation)
# name the above gganimate example as p
p <- ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
transition_states(
gear,
transition_length = 2,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')
# pass the animate command to saveHTML
saveHTML(animate(p,
nframes = 10, # fewer frames for simplification
device = "current"),
img.name = "gganimate_plot",
htmlfile = "gg.html")
I got a html file with animation control buttons & an image folder with 10 png files.