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.

101 líneas
3.3KB

  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. # assign two combiner functions depending on the type
  25. # one for combining the columns (col_combiner)
  26. # one for combining the rows (row_combiner)
  27. if (type == "full_join") {
  28. col_combiner <- dplyr::full_join
  29. row_combiner <- dplyr::full_join
  30. } else if (type == "inner_join") {
  31. col_combiner <- dplyr::full_join
  32. row_combiner <- dplyr::inner_join
  33. } else if (type == "left_join") {
  34. col_combiner <- dplyr::full_join
  35. row_combiner <- dplyr::left_join
  36. } else if (type == "right_join") {
  37. col_combiner <- dplyr::full_join
  38. row_combiner <- dplyr::right_join
  39. } else if (type == "semi_join") {
  40. col_combiner <- dplyr::left_join
  41. row_combiner <- dplyr::semi_join
  42. } else if (type == "anti_join") {
  43. col_combiner <- dplyr::left_join
  44. row_combiner <- dplyr::anti_join
  45. } else if (type == "union") {
  46. col_combiner <- dplyr::full_join
  47. row_combiner <- dplyr::union
  48. } else if (type == "union_all") {
  49. col_combiner <- dplyr::full_join
  50. row_combiner <- dplyr::union_all
  51. } else if (type == "intersect") {
  52. col_combiner <- dplyr::full_join
  53. row_combiner <- dplyr::intersect
  54. } else if (type == "setdiff") {
  55. col_combiner <- dplyr::full_join
  56. row_combiner <- dplyr::anti_join
  57. } else {
  58. stop("Unknown func")
  59. }
  60. take_cols <- col_combiner(x_cols, y_cols, by = ".col")
  61. take_ids <- row_combiner(x_ids, y_ids, by = c(".id", ".id_long"))
  62. take_headers <- col_combiner(x_headers, y_headers, by = c(".id", ".id_long"))
  63. take_ids <- bind_rows(take_headers, 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. yvals <- cumsum(ifelse(str_detect(take_ids$.id_long, "^\\.header"), 0, -1))
  70. names(yvals) <- take_ids %>% pull(.id_long)
  71. take_vals <- semi_join(all, take %>% select(".id", ".col"),
  72. by = c(".id", ".col")) %>%
  73. mutate(.alpha = 1,
  74. .x = xvals[.col],
  75. .y = yvals[.id_long])
  76. res <- bind_rows(
  77. # take,
  78. take_vals,
  79. # fade in place:
  80. all %>% filter(!.id_long %in% take_ids$.id_long) %>% mutate(.alpha = 0),
  81. # moving fade or fade in place as well:
  82. all %>% filter(.id_long %in% take_ids$.id_long & !.col %in% take_cols$.col) %>%
  83. mutate(.alpha = 0)
  84. )
  85. return(res)
  86. }