選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

202 行
7.4KB

  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. print.anim_opts <- function(ao) {
  76. aop <- purrr::discard(ao, is.null)
  77. # Replace ggproto (enter/exit functions) with their names
  78. if ("enter" %in% names(aop)) aop$enter <- paste("ggproto:", names(ao$enter))
  79. if ("exit" %in% names(aop)) aop$exit <- paste("ggproto:", names(ao$exit))
  80. str(aop)
  81. invisible(ao)
  82. }
  83. is.anim_opts <- function(ao) inherits(ao, "anim_opts")
  84. # Fill, Validate, Merge Animation Options ---------------------------------
  85. # Fills in default animation options
  86. fill_anim_opts <- function(ao) {
  87. ao$transition_length <- ao$transition_length %||% get_anim_opt("transition_length")
  88. ao$state_length <- ao$state_length %||% get_anim_opt("state_length")
  89. ao$ease_default <- ao$ease_default %||% get_anim_opt("ease_default")
  90. ao$ease_other <- ao$ease_other %||% get_anim_opt("ease_other")
  91. ao$enter <- ao$enter %||% get_anim_opt("enter")
  92. ao$exit <- ao$exit %||% get_anim_opt("exit")
  93. ao$text_family <- ao$text_family %||% get_anim_opt("text_family")
  94. ao$title_family <- ao$title_family %||% get_anim_opt("title_family")
  95. ao
  96. }
  97. validate_anim_opts <- function(ao, quiet = FALSE, strict = getOption("tidyexplain.strict_dots", FALSE)) {
  98. if (!inherits(ao, "anim_opts")) {
  99. rlang::warn("Use `anim_options()` to set `anim_opts`")
  100. }
  101. ao <- fill_anim_opts(ao)
  102. stopifnot(is.ggproto(ao$enter[[1]]), is.ggproto(ao$exit[[1]]))
  103. extra_names <- setdiff(names(ao), names(formals(anim_options)))
  104. if (!quiet && length(extra_names)) {
  105. extra_names <- paste0(sprintf("`%s`", extra_names), collapse = ", ")
  106. msg <- paste("Unknown animation options will be ignored:", extra_names)
  107. if (isTrue(strict)) rlang::abort(msg) else rlang::warn(msg)
  108. }
  109. invisible(ao)
  110. }
  111. merge.anim_opts <- function(ao_new, ao_base = anim_options()) {
  112. ao_new <- purrr::discard(ao_new, is.null)
  113. ao_base <- purrr::discard(ao_base, is.null)
  114. unique_base <- setdiff(names(ao_base), names(ao_new))
  115. ao <- append(ao_new, ao_base[unique_base])
  116. ao <- ao[names(formals(anim_options))]
  117. ao <- purrr::discard(ao, is.null)
  118. class(ao) <- "anim_opts"
  119. ao
  120. }
  121. # Default Animation Options for Verb Families -----------------------------
  122. default_anim_opts <- function(family, ao_custom = NULL) {
  123. family_options <- c("join", "set", "gather", "spread")
  124. family <- match.arg(family, family_options, several.ok = FALSE)
  125. ao_default <- switch(
  126. family,
  127. "gather" = anim_options(enter = enter_fade(), exit = exit_fade(),
  128. ease_default = "sine-in-out",
  129. ease_other = list(y = "cubic-out", x = "cubic-in")),
  130. "spread" = anim_options(enter = enter_fade(), exit = exit_fade(),
  131. ease_default = "sine-in-out",
  132. ease_other = list(y = "cubic-out", x = "cubic-in")),
  133. anim_options()
  134. )
  135. if (is.null(ao_custom)) ao_default else merge(ao_custom, ao_default)
  136. }
  137. # Font Size Setters and Getters -------------------------------------------
  138. #' Set Default Text Sizes for Animation Plots
  139. #'
  140. #' Sets the default text sizes for the animated and static plots produced by
  141. #' this package during the current session.
  142. #'
  143. #' @param text_size Font size of value labels inside the data frame squares
  144. #' @param title_size Font size of the function call or plot title
  145. #' @export
  146. set_font_size <- function(text_size = NULL, title_size = NULL) {
  147. old <- list()
  148. if (!is.null(text_size)) old$text_size <- set_text_size(text_size)
  149. if (!is.null(title_size)) old$title_size <- set_title_size(title_size)
  150. invisible(old)
  151. }
  152. #' @describeIn set_font_size Get current global font sizes
  153. #' @export
  154. get_font_size <- function() {
  155. list("text_size" = get_text_size(), "title_size" = get_title_size())
  156. }
  157. set_text_size <- function(size) {
  158. old <- plot_settings$text_size
  159. anim_options_set(anim_options(text_size = size))
  160. invisible(old)
  161. }
  162. set_title_size <- function(size) {
  163. old <- plot_settings$title_size
  164. anim_options_set(anim_options(title_size = size))
  165. invisible(old)
  166. }
  167. get_text_size <- function(x = NULL) {
  168. if (!is.null(x)) return(x)
  169. plot_settings$anim_opts$text_size %||%
  170. getFromNamespace("theme_env", "ggplot2")$current$text$size %||%
  171. plot_settings$default$text_size
  172. }
  173. get_title_size <- function(x = NULL) {
  174. if (!is.null(x)) return(x)
  175. plot_settings$anim_opts$title_size %||%
  176. getFromNamespace("theme_env", "ggplot2")$current$plot.title$size %||%
  177. plot_settings$default$title_size
  178. }