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.

98 lines
3.1KB

  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 <- bind_rows(lhs, rhs)
  14. # separate column and row-filter (ids)
  15. x_cols <- lhs %>% distinct(.col)
  16. y_cols <- rhs %>% distinct(.col)
  17. # separate header columns from ids and treat them as columns
  18. x_ids <- lhs %>% distinct(.id, .id_long)
  19. y_ids <- rhs %>% distinct(.id, .id_long)
  20. x_headers <- x_ids %>% filter(str_detect(.id_long, "^\\.header"))
  21. y_headers <- y_ids %>% filter(str_detect(.id_long, "^\\.header"))
  22. x_ids <- x_ids %>% filter(!str_detect(.id_long, "^\\.header"))
  23. y_ids <- y_ids %>% filter(!str_detect(.id_long, "^\\.header"))
  24. if (type == "full_join") {
  25. col_combiner <- dplyr::full_join
  26. row_combiner <- dplyr::full_join
  27. } else if (type == "inner_join") {
  28. col_combiner <- dplyr::inner_join
  29. row_combiner <- dplyr::inner_join
  30. } else if (type == "left_join") {
  31. col_combiner <- dplyr::full_join
  32. row_combiner <- dplyr::left_join
  33. } else if (type == "right_join") {
  34. col_combiner <- dplyr::full_join
  35. row_combiner <- dplyr::right_join
  36. } else if (type == "semi_join") {
  37. col_combiner <- dplyr::left_join
  38. row_combiner <- dplyr::semi_join
  39. } else if (type == "anti_join") {
  40. col_combiner <- dplyr::semi_join
  41. row_combiner <- dplyr::anti_join
  42. } else if (type == "union") {
  43. col_combiner <- dplyr::full_join
  44. row_combiner <- dplyr::union
  45. } else if (type == "union_all") {
  46. col_combiner <- dplyr::full_join
  47. row_combiner <- dplyr::union_all
  48. } else if (type == "intersect") {
  49. col_combiner <- dplyr::full_join
  50. row_combiner <- dplyr::intersect
  51. } else if (type == "setdiff") {
  52. col_combiner <- dplyr::full_join
  53. row_combiner <- dplyr::anti_join
  54. } else {
  55. stop("Unknown func")
  56. }
  57. take_cols <- col_combiner(x_cols, y_cols, by = ".col")
  58. take_ids <- row_combiner(x_ids, y_ids, by = c(".id", ".id_long"))
  59. take_headers <- col_combiner(x_headers, y_headers, by = c(".id", ".id_long"))
  60. take_ids <- bind_rows(take_headers, take_ids)
  61. take <- tidyr::crossing(take_ids, take_cols)
  62. mid <- (2 + length(unique(lhs$.col)) + length(unique(rhs$.col))) / 2
  63. xvals <- 1:nrow(take_cols)
  64. xvals <- xvals - mean(xvals) + mid
  65. names(xvals) <- take_cols %>% pull(.col)
  66. yvals <- cumsum(ifelse(str_detect(take_ids$.id_long, "^\\.header"), 0, -1))
  67. names(yvals) <- take_ids %>% pull(.id_long)
  68. take_vals <- semi_join(all, take %>% select(".id", ".col"),
  69. by = c(".id", ".col")) %>%
  70. mutate(.alpha = 1,
  71. .x = xvals[.col],
  72. .y = yvals[.id_long])
  73. res <- bind_rows(
  74. # take,
  75. take_vals,
  76. # fade in place:
  77. all %>% filter(!.id_long %in% take_ids$.id_long) %>% mutate(.alpha = 0),
  78. # moving fade or fade in place as well:
  79. all %>% filter(.id_long %in% take_ids$.id_long & !.col %in% take_cols$.col) %>%
  80. mutate(.alpha = 0)
  81. )
  82. return(res)
  83. }