You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.4KB

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