No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

124 líneas
4.2KB

  1. #' Animates the gather function
  2. #'
  3. #' @param w a data_frame in the wide format
  4. #' @param key the key
  5. #' @param value the value
  6. #' @param ... further arguments passed to [tidyr::gather()], [process_wide()],
  7. #' or [process_long()]
  8. #' @param detailed boolean value if the animation should show one step for each
  9. #' key value
  10. #' @inheritParams animate_join
  11. #' @inheritParams anim_options
  12. #'
  13. #' @return a gif or a ggplot
  14. #' @export
  15. #'
  16. #' @examples
  17. #' wide <- data_frame(
  18. #' year = 2010:2011,
  19. #' Alice = c(105, 110),
  20. #' Bob = c(100, 97),
  21. #' Charlie = c(90, 95)
  22. #' )
  23. #' animate_gather(wide, "person", "sales", -year, export = "first")
  24. #' animate_gather(wide, "person", "sales", -year, export = "last")
  25. #'
  26. #' \donttest{
  27. #' animate_gather(wide, "person", "sales", -year, export = "gif")
  28. #' # if you want to have a less detailed animation, you can also use
  29. #' animate_gather(wide, "person", "sales", -year, export = "gif", detailed = FALSE)
  30. #' }
  31. animate_gather <- function(w, key, value, ..., export = "gif", detailed = TRUE, anim_opts = anim_options()) {
  32. anim_opts <- default_anim_opts("gather", anim_opts)
  33. lhs <- w
  34. rhs <- tidyr::gather(w, !!key, !!value, ...)
  35. # construct the title sequence
  36. wname <- deparse(substitute(w))
  37. tidyr_selection <- get_quos_names(...)
  38. ids <- setdiff(colnames(w), tidyselect::vars_select(colnames(w), ...))
  39. id_string <- paste0(", ", paste(sprintf("%s", tidyr_selection), collapse = ", "))
  40. sequence <- c(
  41. current_state = "wide",
  42. final_state = "long",
  43. operation = sprintf("gather(%s, %s, %s%s)",
  44. wname,
  45. dput_parser(key),
  46. dput_parser(value),
  47. id_string),
  48. reverse_operation = sprintf("spread(%s, %s, %s)",
  49. "long",
  50. dput_parser(key),
  51. dput_parser(value))
  52. )
  53. key_values <- rhs %>% pull(key) %>% unique()
  54. lhs_proc <- process_wide(lhs, ids, key, key_values, value, ...)
  55. rhs_proc <- process_long(rhs, ids, key, value, ...)
  56. gather_spread(lhs_proc, rhs_proc, sequence = sequence, key_values = key_values,
  57. export = export, detailed = detailed, ..., anim_opts = anim_opts)
  58. }
  59. #' Animates the spread function
  60. #'
  61. #' @param l a data_frame in the long/tidy format
  62. #' @param ... further arguments passed to [process_long] or [process_wide]
  63. #' @inheritParams animate_gather
  64. #' @inheritParams animate_join
  65. #' @inheritParams anim_options
  66. #'
  67. #' @return a ggplot or a gif
  68. #' @export
  69. #'
  70. #' @examples
  71. #' long <- data_frame(
  72. #' year = c(2010L, 2011L, 2010L, 2011L, 2010L, 2011L),
  73. #' person = c("Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie"),
  74. #' sales = c(105, 110, 100, 97, 90, 95)
  75. #' )
  76. #' animate_spread(long, key = "person", value = "sales", export = "first")
  77. #' animate_spread(long, key = "person", value = "sales", export = "last")
  78. #'
  79. #' \donttest{
  80. #' animate_spread(long, key = "person", value = "sales", export = "gif")
  81. #' # if you want to have a less detailed animation, you can also use
  82. #' animate_spread(long, key = "person", value = "sales", export = "gif", detailed = FALSE)
  83. #' }
  84. animate_spread <- function(l, key, value, export = "gif", detailed = TRUE, ..., anim_opts = anim_options()) {
  85. anim_opts <- default_anim_opts("spread", anim_opts)
  86. lhs <- l
  87. rhs <- tidyr::spread(l, key = key, value = value)
  88. # construct the title sequence
  89. lname <- deparse(substitute(l))
  90. ids <- names(lhs)
  91. ids <- ids[!ids %in% c(key, value)]
  92. id_string <- paste0(", ", paste(sprintf("-%s", ids), collapse = ", "))
  93. sequence <- c(
  94. current_state = "long",
  95. final_state = "wide",
  96. operation = sprintf("spread(%s, %s, %s)",
  97. lname,
  98. dput_parser(key),
  99. dput_parser(value)),
  100. reverse_operation = sprintf("gather(%s, %s, %s%s)",
  101. "wide",
  102. dput_parser(key),
  103. dput_parser(value),
  104. id_string)
  105. )
  106. lhs_proc <- process_long(lhs, ids, key, value, ...)
  107. rhs_proc <- process_wide(rhs, ids, key, value, ...)
  108. key_values <- lhs %>% pull(key) %>% unique()
  109. gather_spread(lhs_proc, rhs_proc, sequence, key_values, export, detailed, ..., anim_opts = anim_opts)
  110. }