R Plotly animation - initial frame

Viewed 1713

How can I make a plotly animation load on the final frame. That is, when the plot loads, I would like it to load on that last frame (eg. 2017). By default, it loads on the first frame (eg. 1960). Later, when the person clicks play, the animation should start from the first frame (eg. 1960)

 animation_opts(frame = frame, transition = 0, redraw = FALSE) %>% 
  animation_slider(currentvalue = list(prefix = "Year"))
1 Answers

This is not a complete answer, but since there is no other answer yet I decided to post the best that I can do, and maybe someone else has ideas to improve from there?

There is a function animation_slider(), which takes the argument active, which can set the active frame on the slider. However, this does not change the active frame in the plot... So the idea is to add a frame 0, (as also mentioned by Sixiang.Hu in the comments) and combine this with active frame. The downside with this solution is obviously that there is always a frame 0 when you move back on the animation slider.

# sample data
df = data.frame(a=seq(1,20),b=seq(1,20),frame=seq(1,20))

# add initial frame
init_frame = 10
df = rbind(df[df$frame==10,],df)
df$frame[1]=0

library(plotly)
plot_ly(df,x=~a,y=~b,frame=~frame) %>% animation_slider(active=init_frame)
Related