Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123 linhas
4.1KB

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