Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

91 lines
2.6KB

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