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.

95 lines
2.7KB

  1. #' Animates a set - wrapper function
  2. #'
  3. #' @param x left dataset
  4. #' @param y right dataset
  5. #' @param type type of the set, i.e., intersect, setdiff, etc.
  6. #' @param export if the function exports a gif, the first, or last picture
  7. #' @param ... further arguments passed to base_plot
  8. #'
  9. #'
  10. #' @name animate_set_function
  11. #' @return either a gif or a ggplot
  12. #'
  13. #' @examples
  14. #' NULL
  15. animate_set <- function(x, y, type, export = "gif", ...) {
  16. if (!all(names(x) %in% names(y)) && ncol(x) == ncol(y))
  17. stop("x and y must have the same variables/column-names")
  18. if (!type %in% c("union", "union_all", "intersect", "setdiff"))
  19. stop("type has to be a dplyr-set operation")
  20. if (!export %in% c("gif", "first", "last"))
  21. stop("export must be either gif, first, or last")
  22. title <- sprintf(paste0(type, "(%s, %s)"),
  23. deparse(substitute(x)),
  24. deparse(substitute(y)))
  25. ll <- preprocess_data(x, y, by = names(x))
  26. step0 <- bind_rows(ll$x, ll$y) %>% mutate(.frame = 0, .alpha = 1)
  27. step1 <- move_together(ll$x, ll$y, type) %>% mutate(.frame = 1)
  28. all <- bind_rows(step0, step1)
  29. if (export == "gif") {
  30. animate_plot(all, title, ...) %>% animate()
  31. } else if (export == "first") {
  32. title <- ""
  33. base_plot(step0, title, ...)
  34. } else if (export == "last") {
  35. base_plot(step1, title, ...)
  36. }
  37. }
  38. #' Animates a join - wrapper function
  39. #'
  40. #' @param x left dataset
  41. #' @param y right dataset
  42. #' @param by by arguments for the join
  43. #' @param type type of the join, i.e., left_join, right_join, etc.
  44. #' @param export if the function exports a gif, the first, or last picture
  45. #' @param ... further arguments passed to base_plot
  46. #'
  47. #' @return either a gif or a ggplot
  48. #'
  49. #' @name animate_join_function
  50. #' @examples
  51. #' NULL
  52. animate_join <- function(x, y, by, type, export = "gif", ...) {
  53. if (!type %in% c("full_join", "inner_join", "left_join", "right_join",
  54. "semi_join", "anti_join"))
  55. stop("type has to be a dplyr-join")
  56. if (!export %in% c("gif", "first", "last"))
  57. stop("export must be either gif, first, or last")
  58. title <- sprintf(paste0(type, "(%s, %s, by = c(\"%s\"))"),
  59. deparse(substitute(x)),
  60. deparse(substitute(y)),
  61. paste(by, collapse = "\", \""))
  62. ll <- preprocess_data(x, y, by)
  63. step0 <- bind_rows(ll$x, ll$y) %>% mutate(.frame = 0, .alpha = 1)
  64. step1 <- move_together(ll$x, ll$y, type) %>% mutate(.frame = 1)
  65. all <- bind_rows(step0, step1)
  66. if (export == "gif") {
  67. animate_plot(all, title, ...) %>% animate()
  68. } else if (export == "first") {
  69. title <- ""
  70. base_plot(step0, title, ...)
  71. } else if (export == "last") {
  72. base_plot(step1, title, ...)
  73. }
  74. }