Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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