Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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