|
| 1 | +#' If two `qenv` can be joined |
| 2 | +#' |
| 3 | +#' Checks if two `qenv` objects can be combined. |
| 4 | +#' For more information, please see [`join`] |
| 5 | +#' @param x (`qenv`) |
| 6 | +#' @param y (`qenv`) |
| 7 | +#' @return `TRUE` if able to join or `character` used to print error message. |
| 8 | +#' @keywords internal |
| 9 | +.check_joinable <- function(x, y) { |
| 10 | + checkmate::assert_class(x, "qenv") |
| 11 | + checkmate::assert_class(y, "qenv") |
| 12 | + |
| 13 | + common_names <- intersect(rlang::env_names(x@.xData), rlang::env_names(y@.xData)) |
| 14 | + is_overwritten <- vapply(common_names, function(el) { |
| 15 | + !identical(get(el, x@.xData), get(el, y@.xData)) |
| 16 | + }, logical(1)) |
| 17 | + if (any(is_overwritten)) { |
| 18 | + return( |
| 19 | + paste( |
| 20 | + "Not possible to join qenv objects if anything in their environment has been modified.\n", |
| 21 | + "Following object(s) have been modified:\n - ", |
| 22 | + paste(common_names[is_overwritten], collapse = "\n - ") |
| 23 | + ) |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + shared_ids <- intersect(x@id, y@id) |
| 28 | + if (length(shared_ids) == 0) { |
| 29 | + return(TRUE) |
| 30 | + } |
| 31 | + |
| 32 | + shared_in_x <- match(shared_ids, x@id) |
| 33 | + shared_in_y <- match(shared_ids, y@id) |
| 34 | + |
| 35 | + # indices of shared ids should be 1:n in both slots |
| 36 | + if (identical(shared_in_x, shared_in_y) && identical(shared_in_x, seq_along(shared_ids))) { |
| 37 | + TRUE |
| 38 | + } else if (!identical(shared_in_x, shared_in_y)) { |
| 39 | + paste( |
| 40 | + "The common shared code of the qenvs does not occur in the same position in both qenv objects", |
| 41 | + "so they cannot be joined together as it's impossible to determine the evaluation's order.", |
| 42 | + collapse = "" |
| 43 | + ) |
| 44 | + } else { |
| 45 | + paste( |
| 46 | + "There is code in the qenv objects before their common shared code", |
| 47 | + "which means these objects cannot be joined.", |
| 48 | + collapse = "" |
| 49 | + ) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#' @rdname join |
| 54 | +#' @param ... (`qenv` or `qenv.error`). |
| 55 | +#' @examples |
| 56 | +#' q <- qenv() |
| 57 | +#' q1 <- within(q, { |
| 58 | +#' iris1 <- iris |
| 59 | +#' mtcars1 <- mtcars |
| 60 | +#' }) |
| 61 | +#' q1 <- within(q1, iris2 <- iris) |
| 62 | +#' q2 <- within(q1, mtcars2 <- mtcars) |
| 63 | +#' qq <- c(q1, q2) |
| 64 | +#' cat(get_code(qq)) |
| 65 | +#' |
| 66 | +#' @export |
| 67 | +c.qenv <- function(...) { |
| 68 | + dots <- rlang::list2(...) |
| 69 | + if (!checkmate::test_list(dots[-1], types = c("qenv", "qenv.error"))) { |
| 70 | + return(NextMethod(c, dots[[1]])) |
| 71 | + } |
| 72 | + |
| 73 | + first_non_qenv_ix <- which.min(vapply(dots, inherits, what = "qenv", logical(1))) |
| 74 | + if (first_non_qenv_ix > 1) { |
| 75 | + return(dots[[first_non_qenv_ix]]) |
| 76 | + } |
| 77 | + |
| 78 | + Reduce( |
| 79 | + x = dots[-1], |
| 80 | + init = dots[[1]], |
| 81 | + f = function(x, y) { |
| 82 | + join_validation <- .check_joinable(x, y) |
| 83 | + |
| 84 | + # join expressions |
| 85 | + if (!isTRUE(join_validation)) { |
| 86 | + stop(join_validation) |
| 87 | + } |
| 88 | + |
| 89 | + id_unique <- !y@id %in% x@id |
| 90 | + x@id <- c(x@id, y@id[id_unique]) |
| 91 | + x@code <- c(x@code, y@code[id_unique]) |
| 92 | + x@warnings <- c(x@warnings, y@warnings[id_unique]) |
| 93 | + x@messages <- c(x@messages, y@messages[id_unique]) |
| 94 | + |
| 95 | + # insert (and overwrite) objects from y to x |
| 96 | + x@.xData <- rlang::env_clone(x@.xData, parent = parent.env(.GlobalEnv)) |
| 97 | + rlang::env_coalesce(env = x@.xData, from = y@.xData) |
| 98 | + x |
| 99 | + } |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | +#' @rdname join |
| 104 | +#' @export |
| 105 | +c.qenv.error <- function(...) { |
| 106 | + rlang::list2(...)[[1]] |
| 107 | +} |
0 commit comments