No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

104 líneas
3.2KB

  1. #' Combines two processed datasets and combines them for a given method
  2. #'
  3. #' @param lhs the left-hand side dataset
  4. #' @param rhs the righ-hand side dataset
  5. #' @param type a string of the desired combination method, allowed are all dplyr
  6. #' joins or sets
  7. #'
  8. #' @return processed dataset of the combined values
  9. #'
  10. #' @examples
  11. #' NULL
  12. move_together <- function(lhs, rhs, type) {
  13. all_ids <- bind_rows(lhs, rhs) %>% distinct(.id)
  14. all <- bind_rows(lhs, rhs)
  15. x_cols <- lhs %>% distinct(col)
  16. y_cols <- rhs %>% distinct(col)
  17. x_ids <- lhs %>% distinct(.id)
  18. y_ids <- rhs %>% distinct(.id)
  19. if (type == "full_join") {
  20. col_combiner <- dplyr::full_join
  21. row_combiner <- dplyr::full_join
  22. } else if (type == "inner_join") {
  23. col_combiner <- dplyr::inner_join
  24. row_combiner <- dplyr::inner_join
  25. } else if (type == "left_join") {
  26. col_combiner <- dplyr::full_join
  27. row_combiner <- dplyr::left_join
  28. } else if (type == "right_join") {
  29. col_combiner <- dplyr::full_join
  30. row_combiner <- dplyr::right_join
  31. } else if (type == "semi_join") {
  32. col_combiner <- dplyr::semi_join
  33. row_combiner <- dplyr::semi_join
  34. } else if (type == "anti_join") {
  35. col_combiner <- dplyr::semi_join
  36. row_combiner <- dplyr::anti_join
  37. } else if (type == "union") {
  38. col_combiner <- dplyr::full_join
  39. row_combiner <- dplyr::union
  40. } else if (type == "union_all") {
  41. col_combiner <- dplyr::full_join
  42. row_combiner <- dplyr::union_all
  43. x_ids <- lhs %>% distinct(.id = .id_long)
  44. y_ids <- rhs %>% distinct(.id = .id_long)
  45. all <- all %>% rename(id_old = .id, .id = .id_long)
  46. # all <- all %>% rename(.id = .id_long)
  47. } else if (type == "intersect") {
  48. col_combiner <- dplyr::full_join
  49. row_combiner <- dplyr::intersect
  50. } else if (type == "setdiff") {
  51. col_combiner <- dplyr::full_join
  52. row_combiner <- dplyr::anti_join
  53. } else {
  54. stop("Unknown func")
  55. }
  56. take_cols <- col_combiner(x_cols, y_cols, by = "col")
  57. take_ids <- row_combiner(x_ids, y_ids, by = ".id")
  58. # make sure .header is always the first
  59. id_number <- which(str_detect(take_ids$.id, "^.header"))
  60. if (length(id_number) != 0)
  61. take_ids <- take_ids[c(id_number, (1:nrow(take_ids))[-id_number]), ]
  62. if (!any(str_detect(take_ids$.id, "^.header")))
  63. take_ids <- bind_rows(data_frame(.id = ".header"), take_ids)
  64. take <- tidyr::crossing(take_ids, take_cols)
  65. mid <- (2 + length(unique(lhs$col)) + length(unique(rhs$col))) / 2
  66. xvals <- 1:nrow(take_cols)
  67. xvals <- xvals - mean(xvals) + mid
  68. names(xvals) <- take_cols %>% pull(col)
  69. n_non_header <- sum(str_detect(take_ids$.id, "^[^\\.header]"))
  70. yvals <- cumsum(ifelse(str_detect(take_ids$.id, "^\\.header"), 0, -1))
  71. names(yvals) <- take_ids %>% pull(.id)
  72. take_vals <- semi_join(all, take, by = c(".id", "col")) %>%
  73. mutate(.alpha = 1,
  74. .x = xvals[col],
  75. .y = yvals[.id])
  76. if (type == "union_all") {
  77. take_vals <- take_vals %>% rename(.id_long = .id, .id = id_old)
  78. }
  79. res <- bind_rows(
  80. # take,
  81. take_vals,
  82. # fade in place:
  83. all %>% filter(!.id %in% take_ids$.id) %>% mutate(.alpha = 0),
  84. # moving fade or fade in place as well:
  85. all %>% filter(.id %in% take_ids$.id & !col %in% take_cols$col) %>%
  86. mutate(.alpha = 0)
  87. )
  88. return(res)
  89. }