Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

156 lines
4.9KB

  1. #' Preprocess data
  2. #'
  3. #' @param x a left dataset
  4. #' @param y a right dataset
  5. #' @param by a by argument for joins / set operations
  6. #' @param fill if missing ids should be filled
  7. #' @param ... further arguments passed to add_color
  8. #' @param ao anim_options()
  9. #'
  10. #' @return a preprocessed dataset
  11. #'
  12. #' @examples
  13. #' NULL
  14. process_join <- function(x, y, by, fill = TRUE, ...,
  15. ao = anim_options(...)) {
  16. #' test for
  17. #' a <- c("unique", "mult", "mult", "also unique")
  18. #' add_duplicate_number(a)
  19. add_duplicate_number <- function(a) {
  20. data_frame(v = a) %>%
  21. group_by(v) %>%
  22. mutate(id = paste(v, 1:dplyr::n(), sep = "-")) %>%
  23. pull(id)
  24. }
  25. x <- x %>%
  26. tidyr::unite(dplyr::one_of(by), col = ".id", remove = FALSE) %>%
  27. mutate(.id_long = add_duplicate_number(.id))
  28. y <- y %>%
  29. tidyr::unite(dplyr::one_of(by), col = ".id", remove = FALSE) %>%
  30. mutate(.id_long = add_duplicate_number(.id))
  31. ids <- dplyr::union(x %>% dplyr::select(.id, .id_long),
  32. y %>% dplyr::select(.id, .id_long))
  33. x_ <- process_data_join(x, ids, by, fill = fill, ao = ao)
  34. y_ <- process_data_join(y, ids, by, fill = fill, ao = ao) %>%
  35. mutate(.x = .x + ncol(x) - 1)
  36. list(x = x_, y = y_)
  37. }
  38. #' Processes the data
  39. #'
  40. #' @param x a preprocessed dataset
  41. #' @param ids a data_frame of ids (.id and .id_long)
  42. #' @param by a vector of by-arguments
  43. #' @param width the width of the tiles
  44. #' @param side the side (x or y, lhs or rhs, etc)
  45. #' @param fill if missing ids should be filled
  46. #' @param ... further arguments passed to add_color
  47. #' @param ao anim_options
  48. #'
  49. #' @return a data_frame including all necessary information
  50. #'
  51. #' @examples
  52. #' NULL
  53. process_data_join <- function(x, ids, by, width = 1, side = NA, fill = TRUE,
  54. ...,
  55. ao = anim_options(...)) {
  56. if (is.na(side)) side <- deparse(substitute(x))
  57. x_names <- names(x)[grepl("^[^\\.]", names(x))]
  58. x_keys <- 1:length(x_names)
  59. names(x_keys) <- x_names
  60. special_vars <- names(x)[grepl("^\\.", names(x))]
  61. x <- x %>%
  62. mutate(.r = row_number()) %>%
  63. tidyr::gather_(key = ".col", value = ".val", names(x)[grepl("^[^.]", names(x))]) %>%
  64. mutate(.x = x_keys[.col],
  65. .y = -.r) %>%
  66. bind_rows(data_frame(.id = ".header",
  67. .id_long = paste(".header", x_names, sep = "_"),
  68. .r = 0,
  69. .col = x_names,
  70. .val = x_names,
  71. .x = x_keys, .y = 0), .) %>%
  72. mutate(.width = width,
  73. .side = side)
  74. # if there are multiple values in the ids (-2, -3 etc) but they are not present
  75. # in x, because it is in the second/other dataset, add these values here
  76. id_long <- ids$.id_long
  77. mis_ids <- id_long[!id_long %in% x$.id_long]
  78. # if the missing value is a -1, that means the missing value comes not from
  79. # missing dublicate ids
  80. mis_ids <- mis_ids[grepl("[^-1]$", mis_ids)]
  81. if (length(mis_ids) > 0 && fill) {
  82. mis_ids_short <- gsub("-[0-9]+$", "", mis_ids)
  83. # insert the missing ids at the right place
  84. for (i in mis_ids_short) {
  85. irow <- (1:nrow(x))[x$.id == i]
  86. irow <- irow[1]
  87. x <- bind_rows(
  88. x %>% slice(1:irow),
  89. x %>% filter(.id %in% mis_ids_short) %>% mutate(.id_long = mis_ids),
  90. x %>% slice((irow + 1):nrow(x))
  91. )
  92. }
  93. }
  94. add_color_join(x, rev(ids$.id), by, ao)
  95. }
  96. #' Adds Color to a processed data_frame
  97. #'
  98. #' @param x a processed data_frame
  99. #' @param ids a vector of ids for the color-matching
  100. #' @param by a vector of column names that constitute the by-argument of joins/sets
  101. #' @param color_header color for the header
  102. #' @param color_other color for "inactive" values
  103. #' @param color_missing color for missing values
  104. #' @param color_fun the function to generate the colors
  105. #' @param text_color the color for the text inside the tiles,
  106. #' defaults to white/black depending on tile color
  107. #' @param ...
  108. #'
  109. #' @return the processed data_frame with a new column .color
  110. #'
  111. #' @examples
  112. #' NULL
  113. add_color_join <- function(x, ids, by, ao, ...) {
  114. color_header <- ao$color_header %||% get_anim_opt("color_header")
  115. color_other <- ao$color_other %||% get_anim_opt("color_other")
  116. color_missing <- ao$color_missing %||% get_anim_opt("color_missing")
  117. color_fun <- ao$color_fun %||% get_anim_opt("color_fun")
  118. text_color <- ao$text_color %||% get_anim_opt("text_color")
  119. colors <- c(color_header, color_fun(length(ids)))
  120. names(colors) <- c(".header", ids)
  121. res <- x %>%
  122. mutate(
  123. .color = ifelse(is.na(.val),
  124. color_missing,
  125. ifelse(.col %in% by,
  126. colors[.id],
  127. color_other)),
  128. .color = ifelse(.id == ".header", color_header, .color),
  129. .textcolor = text_color)
  130. if (is.na(text_color))
  131. res <- res %>% mutate(.textcolor = choose_text_color(.color))
  132. return(res)
  133. }