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.

125 lines
4.3KB

  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. wname <- deparse(substitute(w))
  35. tidyr_selection <- get_quos_names(...)
  36. ids <- setdiff(colnames(w), tidyselect::vars_select(colnames(w), ...))
  37. id_string <- paste0(", ", paste(sprintf("%s", tidyr_selection), collapse = ", "))
  38. sequence <- c(
  39. current_state = "wide",
  40. final_state = "long",
  41. operation = sprintf("gather(%s, %s, %s%s)",
  42. wname,
  43. dput_parser(key),
  44. dput_parser(value),
  45. id_string),
  46. reverse_operation = sprintf("spread(%s, %s, %s)",
  47. "long",
  48. dput_parser(key),
  49. dput_parser(value))
  50. )
  51. key_values <- rhs %>% pull(key) %>% unique()
  52. lhs_proc <- process_wide(lhs, ids, key, key_values, value, ...)
  53. rhs_proc <- process_long(rhs, ids, key, value, ...)
  54. gather_spread(lhs_proc, rhs_proc, sequence = sequence, key_values = key_values,
  55. export = export, detailed = detailed, ...)
  56. }
  57. #' Animates the spread function
  58. #'
  59. #' @param l a data_frame in the long/tidy format
  60. #' @param key the key
  61. #' @param value the values
  62. #' @param export the export type, either gif, first or last. The latter two
  63. #' export ggplots of the first/last state of the spread function
  64. #' @param detailed boolean value if the animation should show one step for each
  65. #' key value
  66. #' @param ... further arguments passed to static_plot
  67. #'
  68. #' @return a ggplot or a gif
  69. #' @export
  70. #'
  71. #' @examples
  72. #' long <- data_frame(
  73. #' year = c(2010L, 2011L, 2010L, 2011L, 2010L, 2011L),
  74. #' person = c("Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie"),
  75. #' sales = c(105, 110, 100, 97, 90, 95)
  76. #' )
  77. #' animate_spread(long, key = "person", value = "sales", export = "first")
  78. #' animate_spread(long, key = "person", value = "sales", export = "last")
  79. #'
  80. #' \donttest{
  81. #' animate_spread(long, key = "person", value = "sales", export = "gif")
  82. #' # if you want to have a less detailed animation, you can also use
  83. #' animate_spread(long, key = "person", value = "sales", export = "gif", detailed = FALSE)
  84. #' }
  85. animate_spread <- function(l, key, value, export = "gif", detailed = TRUE, ...) {
  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, ...)
  110. }