Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

134 lines
3.6KB

  1. #' Animates a set operation
  2. #'
  3. #' Functions to visualise the set operations either static as a ggplot, or
  4. #' dynamic as a gif.
  5. #'
  6. #' @param x the x dataset
  7. #' @param y the y dataset
  8. #' @param export the export type, either gif, first or last. The latter two
  9. #' export ggplots of the first/last state of the join
  10. #' @param type type of the set, i.e., intersect, setdiff, etc.
  11. #' @param ... further argument passed to anim_options()
  12. #'
  13. #' @return either a gif or a ggplot
  14. #'
  15. #' @seealso \code{\link[dplyr]{setops}}
  16. #'
  17. #' @examples
  18. #' x <- data_frame(x = c(1, 1, 2), y = c("a", "b", "a"))
  19. #' y <- data_frame(x = c(1, 2), y = c("a", "b"))
  20. #'
  21. #' # Animate the first or last state of the set
  22. #' animate_union(x, y, export = "first")
  23. #' animate_union(x, y, export = "last")
  24. #'
  25. #' # animate the transition as a gif (default)
  26. #' \donttest{
  27. #' animate_union(x, y, export = "gif")
  28. #' }
  29. #'
  30. #' # different options include
  31. #' \donttest{
  32. #' animate_union(x, y)
  33. #' animate_union_all(x, y)
  34. #' animate_intersect(x, y)
  35. #' animate_setdiff(x, y)
  36. #'
  37. #' # further arguments can be passed to all animate_* functions
  38. #' animate_union(
  39. #' x, y,
  40. #' text_size = 5, title_size = 25,
  41. #' color_header = "black",
  42. #' color_fun = viridis::viridis
  43. #' )
  44. #' }
  45. #'
  46. #' # Save the results
  47. #' \dontrun{
  48. #' # to save the ggplot, use
  49. #' un <- animate_union(x, y, by = "id", export = "last")
  50. #' ggsave("union.pdf", un)
  51. #'
  52. #' animate_union(x, y, by = "id", export = "gif")
  53. #' # to save the gif, use
  54. #' un <- animate_union(x, y, by = "id", export = "gif")
  55. #' anim_save(un, "union.gif")
  56. #' }
  57. animate_set <- function(
  58. x, y,
  59. type = c("union", "union_all", "intersect", "setdiff"),
  60. export = c("gif", "first", "last"),
  61. ...
  62. ) {
  63. type <- match.arg(type)
  64. export <- match.arg(export)
  65. x_name <- get_input_text(x)
  66. y_name <- get_input_text(y)
  67. data <- make_named_data(x, y)
  68. col_names <- purrr::map(data, names)
  69. if (!all(names(data$x) %in% names(data$y)) && ncol(data$x) == ncol(data$y))
  70. stop("x and y must have the same variables/column-names")
  71. title <- sprintf(paste0(type, "(%s, %s)"), x_name, y_name)
  72. if (type %in% c("union", "intersect", "setdiff")) {
  73. data <- purrr::map(data, dplyr::distinct)
  74. }
  75. if (type == "union_all") {
  76. ll <- process_join(data$x, data$y, by = names(data$x), fill = FALSE, ...)
  77. ll <- purrr::map(ll, ~ mutate(., .id_long = paste(.id_long, .side, sep = "-")))
  78. } else {
  79. ll <- process_join(data$x, data$y, by = names(data$x), ...)
  80. }
  81. step0 <- bind_rows(ll$x, ll$y) %>% mutate(.frame = 0, .alpha = 1)
  82. step1 <- move_together(ll$x, ll$y, type) %>% mutate(.frame = 1)
  83. all <- bind_rows(step0, step1)
  84. if (export == "gif") {
  85. animate_plot(all, title, ...)
  86. } else if (export == "first") {
  87. title <- ""
  88. static_plot(step0, title, ...)
  89. } else if (export == "last") {
  90. static_plot(step1, title, ...)
  91. }
  92. }
  93. #' @rdname animate_set
  94. #' @export
  95. animate_union <- function(x, y, export = "gif", ...) {
  96. x <- rlang::enquo(x)
  97. y <- rlang::enquo(y)
  98. animate_set(x, y, type = "union", export = export, ...)
  99. }
  100. #' @rdname animate_set
  101. #' @export
  102. animate_union_all <- function(x, y, export = "gif", ...) {
  103. x <- rlang::enquo(x)
  104. y <- rlang::enquo(y)
  105. animate_set(x, y, type = "union_all", export = export, ...)
  106. }
  107. #' @rdname animate_set
  108. #' @export
  109. animate_intersect <- function(x, y, export = "gif", ...) {
  110. x <- rlang::enquo(x)
  111. y <- rlang::enquo(y)
  112. animate_set(x, y, type = "intersect", export = export, ...)
  113. }
  114. #' @rdname animate_set
  115. #' @export
  116. animate_setdiff <- function(x, y, export = "gif", ...) {
  117. x <- rlang::enquo(x)
  118. y <- rlang::enquo(y)
  119. animate_set(x, y, type = "setdiff", export = export, ...)
  120. }