Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

237 lines
8.8KB

  1. #' Animation Options
  2. #'
  3. #' Helper function to set animation and plotting options to be passed to
  4. #' [animate_plot()] and [static_plot()].
  5. #'
  6. #' @param color_header Color of the header row.
  7. #' @param color_other Color of the cells that are not highlighted otherwise.
  8. #' @param color_missing Color of the missing cells.
  9. #' @param color_fun A function that generates the colors for the highlighted
  10. #' cells, default is [scales::brewer_pal()] Set1.
  11. #' @param text_color Color of the text of the cells, default is a black or
  12. #' white, based on the background color of the cell.
  13. #' @param text_family Font family for the plot text, default is "Fira Mono". Use
  14. #' [set_font_size()] to set global default font sizes.
  15. #' @param title_family Font family for the plot title, default is "Fira Mono".
  16. #' Use [set_font_size()] to set global default font sizes.
  17. #' @param text_size Font size of the plot text, default is 5.
  18. #' @param title_size Font size of the plot title, default is 17.
  19. #' @param ease_default Default aes easing function. See [tweenr::display_ease()]
  20. #' for more options. The tidyexplain default value is `sine-in-out`.
  21. #' @param ease_other Additional aes easing options, specified as a named list.
  22. #' List entries are named with the aesthetic to which the easeing should be
  23. #' applied, consistent with [gganimate::ease_aes()]. E.g. `list(color =
  24. #' "sine")`.
  25. #' @param enter Enter fading function applied to objects in the animation. See
  26. #' [gganimate::enter_exit] for a complete list of options. The tidyexplain
  27. #' default is [gganimate::enter_fade()].
  28. #' @param exit Exit fading function applied to objects in the animation. See
  29. #' [gganimate::enter_exit] for a complete list of options. The tidyexplain
  30. #' default is [gganimate::exit_fade()].
  31. #' @inheritParams gganimate::transition_states
  32. #' @export
  33. anim_options <- function(
  34. transition_length = NULL,
  35. state_length = NULL,
  36. ease_default = NULL,
  37. ease_other = NULL,
  38. enter = NULL,
  39. exit = NULL,
  40. text_family = NULL,
  41. title_family = NULL,
  42. text_size = NULL,
  43. title_size = NULL,
  44. color_header = NULL,
  45. color_other = NULL,
  46. color_missing = NULL,
  47. color_fun = NULL,
  48. text_color = NULL,
  49. ...
  50. ){
  51. enter_name <- if (!missing(enter)) rlang::quo_name(rlang::enquo(enter))
  52. exit_name <- if (!missing(exit)) rlang::quo_name(rlang::enquo(exit))
  53. ao <- list(
  54. transition_length = transition_length,
  55. state_length = state_length,
  56. ease_default = ease_default,
  57. ease_other = ease_other,
  58. enter = if (!is.null(enter)) setNames(list(enter), enter_name),
  59. exit = if (!is.null(exit)) setNames(list(exit), exit_name),
  60. text_family = text_family,
  61. text_size = text_size,
  62. title_family = title_family,
  63. title_size = title_size,
  64. color_header = color_header,
  65. color_other = color_other,
  66. color_missing = color_missing,
  67. color_fun = color_fun,
  68. text_color = text_color,
  69. ...
  70. )
  71. ao <- purrr::compact(ao)
  72. structure(ao, class = "anim_opts")
  73. }
  74. # Global Animation Options Setters and Getters ----------------------------
  75. #' @describeIn anim_options Set default animation options for the current session.
  76. #' @param anim_opts An [anim_options()] options list.
  77. #' @export
  78. anim_options_set <- function(anim_opts = anim_options()) {
  79. stopifnot(is.anim_opts(anim_opts))
  80. ao_old <- plot_settings$anim_opts
  81. plot_settings$anim_opts <- merge(anim_opts, plot_settings$anim_opts)
  82. invisible(ao_old)
  83. }
  84. get_anim_opt <- function(anim_opt = NULL) {
  85. if (is.null(anim_opt)) return(plot_settings$anim_opts)
  86. if (anim_opt %in% c("text_size", "title_size")) rlang::abort(
  87. "Use get_text_size() or get_title_size()"
  88. )
  89. plot_settings$anim_opts[[anim_opt]] %||% plot_settings$default[[anim_opt]]
  90. }
  91. # Animation Options Methods -----------------------------------------------
  92. #' @export
  93. print.anim_opts <- function(x) {
  94. # Replace ggproto (enter/exit functions) with their names
  95. if ("enter" %in% names(x)) x$enter <- paste("ggproto:", names(x$enter))
  96. if ("exit" %in% names(x)) x$exit <- paste("ggproto:", names(x$exit))
  97. anim_opts <- capture.output(str(x, no.list = TRUE))
  98. cat(
  99. paste0("<anim_options: ", length(x), " options>"),
  100. anim_opts, sep = "\n"
  101. )
  102. }
  103. #' @export
  104. is.anim_opts <- function(ao) inherits(ao, "anim_opts")
  105. # Fill, Validate, Merge Animation Options ---------------------------------
  106. # Fills in default animation options
  107. fill_anim_opts <- function(ao) {
  108. ao$transition_length <- ao$transition_length %||% get_anim_opt("transition_length")
  109. ao$state_length <- ao$state_length %||% get_anim_opt("state_length")
  110. ao$ease_default <- ao$ease_default %||% get_anim_opt("ease_default")
  111. ao$ease_other <- ao$ease_other %||% get_anim_opt("ease_other")
  112. ao$enter <- ao$enter %||% get_anim_opt("enter")
  113. ao$exit <- ao$exit %||% get_anim_opt("exit")
  114. ao$text_family <- ao$text_family %||% get_anim_opt("text_family")
  115. ao$title_family <- ao$title_family %||% get_anim_opt("title_family")
  116. ao$color_header <- ao$color_header %||% get_anim_opt("color_header")
  117. ao$color_other <- ao$color_other %||% get_anim_opt("color_other")
  118. ao$color_missing <- ao$color_missing %||% get_anim_opt("color_missing")
  119. ao$color_fun <- ao$color_fun %||% get_anim_opt("color_fun")
  120. ao$text_color <- ao$text_color %||% get_anim_opt("text_color")
  121. ao
  122. }
  123. validate_anim_opts <- function(ao, quiet = FALSE, strict = getOption("tidyexplain.strict_dots", FALSE)) {
  124. if (!inherits(ao, "anim_opts")) {
  125. rlang::warn("Use `anim_options()` to set `anim_opts`")
  126. }
  127. ao <- fill_anim_opts(ao)
  128. stopifnot(is.ggproto(ao$enter[[1]]), is.ggproto(ao$exit[[1]]))
  129. extra_names <- setdiff(names(ao), names(formals(anim_options)))
  130. if (!quiet && length(extra_names)) {
  131. extra_names <- paste0(sprintf("`%s`", extra_names), collapse = ", ")
  132. msg <- paste("Unknown animation options will be ignored:", extra_names)
  133. if (isTrue(strict)) rlang::abort(msg) else rlang::warn(msg)
  134. }
  135. invisible(ao)
  136. }
  137. merge.anim_opts <- function(ao_new, ao_base = anim_options()) {
  138. ao_new <- purrr::discard(ao_new, is.null)
  139. ao_base <- purrr::discard(ao_base, is.null)
  140. unique_base <- setdiff(names(ao_base), names(ao_new))
  141. ao <- append(ao_new, ao_base[unique_base])
  142. ao <- ao[names(formals(anim_options))]
  143. ao <- purrr::discard(ao, is.null)
  144. class(ao) <- "anim_opts"
  145. ao
  146. }
  147. # Default Animation Options for Verb Families -----------------------------
  148. default_anim_opts <- function(family, ao_custom = NULL) {
  149. family_options <- c("join", "set", "gather", "spread")
  150. family <- match.arg(family, family_options, several.ok = FALSE)
  151. ao_default <- switch(
  152. family,
  153. "gather" = anim_options(enter = enter_fade(), exit = exit_fade(),
  154. ease_default = "sine-in-out",
  155. ease_other = list(y = "cubic-out", x = "cubic-in")),
  156. "spread" = anim_options(enter = enter_fade(), exit = exit_fade(),
  157. ease_default = "sine-in-out",
  158. ease_other = list(y = "cubic-out", x = "cubic-in")),
  159. anim_options()
  160. )
  161. if (is.null(ao_custom)) {
  162. # User set globals override defaults
  163. ao_custom <- get_anim_opt()
  164. } else {
  165. # Opts from function call override user-set globals
  166. ao_custom <- merge(ao_custom, get_anim_opt())
  167. }
  168. # function > user-set global > default (> global default)
  169. if (!is.null(ao_custom)) merge(ao_custom, ao_default) else ao_default
  170. }
  171. # Font Size Setters and Getters -------------------------------------------
  172. #' Set Default Text Sizes for Animation Plots
  173. #'
  174. #' Sets the default text sizes for the animated and static plots produced by
  175. #' this package during the current session.
  176. #'
  177. #' @param text_size Font size of value labels inside the data frame squares
  178. #' @param title_size Font size of the function call or plot title
  179. #' @export
  180. set_font_size <- function(text_size = NULL, title_size = NULL) {
  181. old <- list()
  182. if (!is.null(text_size)) old$text_size <- set_text_size(text_size)
  183. if (!is.null(title_size)) old$title_size <- set_title_size(title_size)
  184. invisible(old)
  185. }
  186. #' @describeIn set_font_size Get current global font sizes
  187. #' @export
  188. get_font_size <- function() {
  189. list("text_size" = get_text_size(), "title_size" = get_title_size())
  190. }
  191. set_text_size <- function(size) {
  192. old <- plot_settings$text_size
  193. anim_options_set(anim_options(text_size = size))
  194. invisible(old)
  195. }
  196. set_title_size <- function(size) {
  197. old <- plot_settings$title_size
  198. anim_options_set(anim_options(title_size = size))
  199. invisible(old)
  200. }
  201. get_text_size <- function(x = NULL) {
  202. if (!is.null(x)) return(x)
  203. plot_settings$anim_opts$text_size %||%
  204. getFromNamespace("theme_void", "ggplot2")()$text$size %||%
  205. plot_settings$default$text_size
  206. }
  207. get_title_size <- function(x = NULL) {
  208. if (!is.null(x)) return(x)
  209. plot_settings$anim_opts$title_size %||%
  210. getFromNamespace("theme_void", "ggplot2")()$plot.title$size %||%
  211. plot_settings$default$title_size
  212. }