|
| 1 | +#' Animation configuration options |
| 2 | +#' |
| 3 | +#' Animations can be created by either using the \code{frame} argument in |
| 4 | +#' \code{\link{plot_ly}()} or the (unofficial) \code{frame} ggplot2 aesthetic in |
| 5 | +#' \code{\link{ggplotly}()}. By default, animations populate a play button |
| 6 | +#' and slider component for controlling the state of the animation |
| 7 | +#' (to pause an animation, click on a relevant location on the slider bar). |
| 8 | +#' Both the play button and slider component transition between frames according |
| 9 | +#' rules specified by \code{\link{animation_opts}()}. |
| 10 | +#' |
| 11 | +#' @param p a plotly object. |
| 12 | +#' @param frame The amount of time between frames (in milliseconds). |
| 13 | +#' Note that this amount should include the \code{transition}. |
| 14 | +#' @param transition The duration of the smooth transition between |
| 15 | +#' frames (in milliseconds). |
| 16 | +#' @param easing The type of transition easing. See the list of options here |
| 17 | +#' \url{https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js} |
| 18 | +#' @param redraw Trigger a redraw of the plot at completion of the transition? |
| 19 | +#' A redraw may significantly impact performance, but may be necessary to |
| 20 | +#' update plot attributes that can't be transitioned. |
| 21 | +#' @param mode Describes how a new animate call interacts with currently-running |
| 22 | +#' animations. If `immediate`, current animations are interrupted and |
| 23 | +#' the new animation is started. If `next`, the current frame is allowed |
| 24 | +#' to complete, after which the new animation is started. If `afterall` |
| 25 | +#' all existing frames are animated to completion before the new animation |
| 26 | +#' is started. |
| 27 | +#' @export |
| 28 | +#' @rdname animation |
| 29 | +#' @author Carson Sievert |
| 30 | +#' @examples |
| 31 | +#' |
| 32 | +#' df <- data.frame( |
| 33 | +#' x = c(1, 2, 2, 1, 1, 2), |
| 34 | +#' y = c(1, 2, 2, 1, 1, 2), |
| 35 | +#' z = c(1, 1, 2, 2, 3, 3) |
| 36 | +#' ) |
| 37 | +#' plot_ly(df) %>% |
| 38 | +#' add_markers(x = 1.5, y = 1.5) %>% |
| 39 | +#' add_markers(x = ~x, y = ~y, frame = ~z) |
| 40 | +#' |
| 41 | +#' # it's a good idea to remove smooth transitions when there is |
| 42 | +#' # no relationship between objects in each view |
| 43 | +#' plot_ly(mtcars, x = ~wt, y = ~mpg, frame = ~cyl) %>% |
| 44 | +#' animation_opts(transition = 0) |
| 45 | +#' |
| 46 | +#' # works the same way with ggplotly |
| 47 | +#' if (interactive()) { |
| 48 | +#' p <- ggplot(txhousing, aes(month, median)) + |
| 49 | +#' geom_line(aes(group = year), alpha = 0.3) + |
| 50 | +#' geom_smooth() + |
| 51 | +#' geom_line(aes(frame = year, ids = month), color = "red") + |
| 52 | +#' facet_wrap(~ city) |
| 53 | +#' |
| 54 | +#' ggplotly(p, width = 1200, height = 900) %>% |
| 55 | +#' animation_opts(1000) |
| 56 | +#' } |
| 57 | +#' |
| 58 | +#' |
| 59 | +#' #' # for more, see https://cpsievert.github.io/plotly_book/key-frame-animations.html |
| 60 | +#' |
| 61 | +animation_opts <- function(p, frame = 500, transition = frame, easing = "linear", |
| 62 | + redraw = FALSE, mode = "immediate") { |
| 63 | + if (frame < 0) { |
| 64 | + stop("frame must be non-negative.", call. = FALSE) |
| 65 | + } |
| 66 | + if (transition < 0) { |
| 67 | + stop("frame must be non-negative.", call. = FALSE) |
| 68 | + } |
| 69 | + if (frame < transition) { |
| 70 | + stop("frame must be larger than transition", call. = FALSE) |
| 71 | + } |
| 72 | + |
| 73 | + opts <- list( |
| 74 | + transition = list( |
| 75 | + duration = transition, |
| 76 | + easing = match.arg(easing, easingOpts()) |
| 77 | + ), |
| 78 | + frame = list( |
| 79 | + duration = frame, |
| 80 | + redraw = redraw |
| 81 | + ), |
| 82 | + mode = match.arg(mode, c('immediate', 'next', 'afterall')) |
| 83 | + ) |
| 84 | + |
| 85 | + # build step will ensure we can access the animation frames |
| 86 | + # (required to fill the steps in correctly) |
| 87 | + p <- plotly_build(p) |
| 88 | + |
| 89 | + # overwrite the animation options in the slider/button spec |
| 90 | + supply_ani_slider(supply_ani_button(p, opts = opts), opts = opts) |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +#' @inheritParams animation_opts |
| 95 | +#' @param hide remove the animation slider? |
| 96 | +#' @param ... for \code{animation_slider}, attributes are passed to a special |
| 97 | +#' layout.sliders object tied to the animation frames. |
| 98 | +#' The definition of these attributes may be found here |
| 99 | +#' \url{https://github.com/plotly/plotly.js/blob/master/src/components/sliders/attributes.js} |
| 100 | +#' For \code{animation_button}, arguments are passed to a special |
| 101 | +#' layout.updatemenus button object tied to the animation |
| 102 | +#' \url{https://github.com/plotly/plotly.js/blob/master/src/components/updatemenus/attributes.js} |
| 103 | +#' @export |
| 104 | +#' @rdname animation |
| 105 | +animation_slider <- function(p, hide = FALSE, ...) { |
| 106 | + |
| 107 | + p <- plotly_build(p) |
| 108 | + isAniSlider <- vapply(p$x$layout$sliders, is_ani_slider, logical(1)) |
| 109 | + if (hide) { |
| 110 | + p$x$layout$sliders[isAniSlider] <- NULL |
| 111 | + return(p) |
| 112 | + } |
| 113 | + p$x$layout$sliders[[which(isAniSlider)]] <- modify_list( |
| 114 | + p$x$layout$sliders[[which(isAniSlider)]], list(...) |
| 115 | + ) |
| 116 | + p |
| 117 | + |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +#' @inheritParams animation_slider |
| 122 | +#' @export |
| 123 | +#' @rdname animation |
| 124 | +animation_button <- function(p, ...) { |
| 125 | + |
| 126 | + p <- plotly_build(p) |
| 127 | + isAniButton <- vapply(p$x$layout$updatemenus, is_ani_button, logical(1)) |
| 128 | + p$x$layout$updatemenus[[which(isAniButton)]] <- modify_list( |
| 129 | + p$x$layout$updatemenus[[which(isAniButton)]], list(...) |
| 130 | + ) |
| 131 | + p |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +# supply an animation button if it doesn't exist, |
| 136 | +# and _replace_ an existing animation button |
| 137 | +supply_ani_button <- function(p, opts = NULL) { |
| 138 | + nmenus <- length(p$x$layout$updatemenus) |
| 139 | + isAniButton <- vapply(p$x$layout$updatemenus, is_ani_button, logical(1)) |
| 140 | + idx <- if (sum(isAniButton) == 1) which(isAniButton) else nmenus + 1 |
| 141 | + p$x$layout$updatemenus[[idx]] <- create_ani_button(opts) |
| 142 | + p |
| 143 | +} |
| 144 | + |
| 145 | +create_ani_button <- function(opts) { |
| 146 | + button <- list( |
| 147 | + type = 'buttons', |
| 148 | + direction = 'right', |
| 149 | + showactive = FALSE, |
| 150 | + y = 0, |
| 151 | + x = 0, |
| 152 | + yanchor = 'top', |
| 153 | + xanchor = 'right', |
| 154 | + pad = list(t = 60, r = 5), |
| 155 | + buttons = list(list( |
| 156 | + label = 'Play', |
| 157 | + method = 'animate', |
| 158 | + args = list(list(), modify_list(list(fromcurrent = TRUE, mode = "immediate"), opts)) |
| 159 | + )) |
| 160 | + ) |
| 161 | + structure(button, class = "aniButton") |
| 162 | +} |
| 163 | + |
| 164 | +is_ani_button <- function(obj) { |
| 165 | + class(obj) %in% "aniButton" |
| 166 | +} |
| 167 | + |
| 168 | +# supply an animation slider if it doesn't exist, |
| 169 | +# and _replace_ an existing animation slider |
| 170 | +supply_ani_slider <- function(p, opts = NULL, ...) { |
| 171 | + nsliders <- length(p$x$layout$sliders) |
| 172 | + isAniSlider <- vapply(p$x$layout$sliders, is_ani_slider, logical(1)) |
| 173 | + hasAniSlider <- sum(isAniSlider) == 1 |
| 174 | + idx <- if (hasAniSlider) which(isAniSlider) else nsliders + 1 |
| 175 | + p$x$layout$sliders[[idx]] <- create_ani_slider(p, opts, ...) |
| 176 | + p |
| 177 | +} |
| 178 | + |
| 179 | + |
| 180 | +create_ani_slider <- function(p, opts = NULL, ...) { |
| 181 | + steps <- lapply(p$x$frames, function(f) { |
| 182 | + # frame names should already be formatted |
| 183 | + nm <- f[["name"]] |
| 184 | + args <- list(list(nm)) |
| 185 | + args[[2]] <- opts |
| 186 | + list(method = "animate", args = args, label = nm, value = nm) |
| 187 | + }) |
| 188 | + |
| 189 | + # inherit defaults from any existing slider |
| 190 | + slider <- modify_list( |
| 191 | + p$x$layout$sliders[[vapply(p$x$layout$sliders, is_ani_slider, logical(1))]], list(...) |
| 192 | + ) |
| 193 | + # don't let the user override steps |
| 194 | + slider$steps <- steps |
| 195 | + |
| 196 | + # set some opinionated defaults |
| 197 | + slider$visible <- slider$visible %||% TRUE |
| 198 | + slider$pad$t <- slider$pad[["t"]] %||% 40 |
| 199 | + structure(slider, class = "aniSlider") |
| 200 | +} |
| 201 | + |
| 202 | +is_ani_slider <- function(obj) { |
| 203 | + class(obj) %in% "aniSlider" |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | +easingOpts <- function() { |
| 208 | + c('linear', 'quad', 'cubic', 'sin', 'exp', 'circle', 'elastic', 'back', |
| 209 | + 'bounce', 'linear-in', 'quad-in', 'cubic-in', 'sin-in', 'exp-in', |
| 210 | + 'circle-in', 'elastic-in', 'back-in', 'bounce-in', 'linear-out', |
| 211 | + 'quad-out', 'cubic-out', 'sin-out', 'exp-out', 'circle-out', 'elastic-out', |
| 212 | + 'back-out', 'bounce-out', 'linear-in-out', 'quad-in-out', 'cubic-in-out', |
| 213 | + 'sin-in-out', 'exp-in-out', 'circle-in-out', 'elastic-in-out', |
| 214 | + 'back-in-out', 'bounce-in-out') |
| 215 | +} |
0 commit comments