選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

118 行
3.3KB

  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 static_plot or to add_color
  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. if (type %in% c("union", "intersect", "setdiff")) {
  26. x <- dplyr::distinct(x)
  27. y <- dplyr::distinct(y)
  28. }
  29. if (type == "union_all") {
  30. ll <- process_join(x, y, by = names(x), fill = FALSE, ...)
  31. ll <- lapply(ll, function(a)
  32. a %>% mutate(.id_long = paste(.id_long, .side, sep = "-"))
  33. )
  34. } else {
  35. ll <- process_join(x, y, by = names(x), ...)
  36. }
  37. step0 <- bind_rows(ll$x, ll$y) %>% mutate(.frame = 0, .alpha = 1)
  38. step1 <- move_together(ll$x, ll$y, type) %>% mutate(.frame = 1)
  39. all <- bind_rows(step0, step1)
  40. if (export == "gif") {
  41. animate_plot(all, title, ...) %>% animate()
  42. } else if (export == "first") {
  43. title <- ""
  44. static_plot(step0, title, ...)
  45. } else if (export == "last") {
  46. static_plot(step1, title, ...)
  47. }
  48. }
  49. #' Animates a join - wrapper function
  50. #'
  51. #' @param x left dataset
  52. #' @param y right dataset
  53. #' @param by by arguments for the join
  54. #' @param type type of the join, i.e., left_join, right_join, etc.
  55. #' @param export if the function exports a gif, the first, or last picture
  56. #' @param ... further arguments passed to static_plot or to add_color
  57. #'
  58. #' @return either a gif or a ggplot
  59. #'
  60. #' @name animate_join_function
  61. #'
  62. #' @examples
  63. #' NULL
  64. animate_join <- function(x, y, by, type, export = "gif", ...) {
  65. if (!type %in% c("full_join", "inner_join", "left_join", "right_join",
  66. "semi_join", "anti_join"))
  67. stop("type has to be a dplyr-join")
  68. if (!export %in% c("gif", "first", "last"))
  69. stop("export must be either gif, first, or last")
  70. by_args <- ifelse(length(by) == 1,
  71. sprintf("\"%s\"", by),
  72. sprintf("c(\"%s\")", paste(by, collapse = "\", \""))
  73. )
  74. title <- sprintf(paste0(type, "(%s, %s, by = %s)"),
  75. deparse(substitute(x)),
  76. deparse(substitute(y)),
  77. by_args)
  78. if (type %in% c("semi_join", "anti_join")) {
  79. # for semi and anti_joins, there is no adding of multiple rows
  80. y <- dplyr::distinct(y)
  81. }
  82. ll <- process_join(x, y, by, ...)
  83. step0 <- bind_rows(ll$x, ll$y) %>% mutate(.frame = 0, .alpha = 1)
  84. step1 <- move_together(ll$x, ll$y, type) %>% mutate(.frame = 1)
  85. all <- bind_rows(step0, step1)
  86. if (export == "gif") {
  87. animate_plot(all, title, ...) %>% animate()
  88. } else if (export == "first") {
  89. title <- ""
  90. static_plot(step0, title, ...)
  91. } else if (export == "last") {
  92. static_plot(step1, title, ...)
  93. }
  94. }