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.

106 lines
3.5KB

  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 <- dplyr::distinct(lhs, .col)
  16. y_cols <- dplyr::distinct(rhs, .col)
  17. # separate header columns from ids and treat them as columns
  18. x_ids <- dplyr::distinct(lhs, .id, .id_long)
  19. y_ids <- dplyr::distinct(rhs, .id, .id_long)
  20. x_headers <- filter(x_ids, grepl("^\\.header", .id_long))
  21. y_headers <- filter(y_ids, grepl("^\\.header", .id_long))
  22. x_ids <- x_ids %>% filter(!grepl("^\\.header", .id_long))
  23. y_ids <- y_ids %>% filter(!grepl("^\\.header", .id_long))
  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 if (type == "bind_rows") {
  58. col_combiner <- dplyr::full_join
  59. row_combiner <- dplyr::bind_rows
  60. } else if (type == "bind_cols") {
  61. col_combiner <- dplyr::full_join
  62. row_combiner <- dplyr::left_join
  63. } else {
  64. stop("Unknown func")
  65. }
  66. take_cols <- col_combiner(x_cols, y_cols, by = ".col")
  67. take_ids <- row_combiner(x_ids, y_ids, by = c(".id", ".id_long"))
  68. take_headers <- col_combiner(x_headers, y_headers, by = c(".id", ".id_long"))
  69. take_ids <- bind_rows(take_headers, take_ids)
  70. take <- tidyr::crossing(take_ids, take_cols)
  71. mid <- (2 + length(unique(lhs$.col)) + length(unique(rhs$.col))) / 2
  72. xvals <- 1:nrow(take_cols)
  73. xvals <- xvals - mean(xvals) + mid
  74. names(xvals) <- pull(take_cols, .col)
  75. yvals <- cumsum(ifelse(grepl("^\\.header", take_ids$.id_long), 0, -1))
  76. names(yvals) <- pull(take_ids, .id_long)
  77. take_vals <- semi_join(all, take %>% select(".id", ".col"),
  78. by = c(".id", ".col")) %>%
  79. mutate(.alpha = 1,
  80. .x = xvals[.col],
  81. .y = yvals[.id_long])
  82. bind_rows(
  83. # take,
  84. take_vals,
  85. # fade in place:
  86. all %>% filter(!.id_long %in% take_ids$.id_long) %>% mutate(.alpha = 0),
  87. # moving fade or fade in place as well:
  88. all %>% filter(.id_long %in% take_ids$.id_long & !.col %in% take_cols$.col) %>%
  89. mutate(.alpha = 0)
  90. )
  91. }