|
7 | 7 | #' See below for an example. |
8 | 8 | #' |
9 | 9 | #' There are some situations where `join()` cannot be properly performed, such as these three scenarios: |
10 | | -#' \enumerate{ |
11 | | -#' \item Both `qenv` objects contain an object of the same name but are not identical. \cr\cr |
12 | | -#' Example: |
13 | | -#' \preformatted{ |
14 | | -#' x <- qenv( |
15 | | -#' code = c(mtcars1 = "mtcars1 <- mtcars"), |
16 | | -#' env = list2env(list(mtcars1 = mtcars)) |
17 | | -#' ) |
18 | | -#' y <- qenv( |
19 | | -#' code = c(mtcars1 = "mtcars1 <- mtcars['wt']"), |
20 | | -#' env = list2env(list(mtcars1 = mtcars['wt'])) |
21 | | -#' ) |
22 | | -#' z <- join(x, y) |
23 | | -#' # Error message will occur |
24 | | -#' } |
25 | | -#' In this example, `mtcars1` object exists in both `x` and `y` objects but the content are not identical.\cr |
26 | | -#' `mtcars1` in the `x qenv` object has more columns than `mtcars1` in the `y qenv` object (only has one column). |
27 | | -#' \item `join()` will look for identical `@id` values in both `qenv` objects. |
28 | | -#' The index position of these `@id`s must be the same to determine the evaluation order. |
29 | | -#' Otherwise, `join()` will throw an error message.\cr\cr |
30 | | -#' Example: |
31 | | -#' \preformatted{ |
32 | | -#' common_q <- qenv(code = "v <- 1", env = list2env(list(v = 1))) |
33 | | -#' x <- eval_code( |
34 | | -#' common_q, |
35 | | -#' "x <- v" |
36 | | -#' ) |
37 | | -#' y <- eval_code( |
38 | | -#' common_q, |
39 | | -#' "y <- v" |
40 | | -#' ) |
41 | | -#' z <- eval_code( |
42 | | -#' y, |
43 | | -#' "z <- v" |
44 | | -#' ) |
45 | | -#' q <- join(x, y) |
46 | | -#' join_q <- join(q, z) |
47 | | -#' # Error message will occur |
48 | | -#' |
49 | | -#' # Check the order of evaluation based on the id slot |
50 | | -#' shared_ids <- intersect(q@id, z@id) |
51 | | -#' match(shared_ids, q@id) # Output: 1 3 |
52 | | -#' match(shared_ids, z@id) # Output: 1 2 |
53 | | -#' } |
54 | | -#' The error occurs because the index position of identical `@id` between the two objects is not the same. |
55 | | -#' \item The usage of temporary variable in the code expression could cause `join()` to fail. \cr\cr |
56 | | -#' Example: |
57 | | -#' \preformatted{ |
58 | | -#' common_q <- qenv() |
59 | | -#' x <- eval_code( |
60 | | -#' common_q, |
61 | | -#' "x <- numeric(0) |
62 | | -#' for (i in 1:2) { |
63 | | -#' x <- c(x, i) |
64 | | -#' }" |
65 | | -#' ) |
66 | | -#' y <- eval_code( |
67 | | -#' common_q, |
68 | | -#' "y <- numeric(0) |
69 | | -#' for (i in 1:3) { |
70 | | -#' y <- c(y, i) |
71 | | -#' }" |
72 | | -#' ) |
73 | | -#' q <- join(x,y) |
74 | | -#' # Error message will occur |
75 | | -#' |
76 | | -#' # Check the value of temporary variable i in both objects |
77 | | -#' x@env$i # Output: 2 |
78 | | -#' y@env$i # Output: 3 |
79 | | -#' } |
80 | | -#' `join()` fails to provide a proper result because of the temporary variable `i` exists |
81 | | -#' in both objects but has different value.\cr |
82 | | -#' To fix this, we can set `i <- NULL` in the code expression for both objects. |
83 | | -#' \preformatted{ |
84 | | -#' common_q <- qenv() |
85 | | -#' x <- eval_code( |
86 | | -#' common_q, |
87 | | -#' "x <- numeric(0) |
88 | | -#' for (i in 1:2) { |
89 | | -#' x <- c(x, i) |
90 | | -#' } |
91 | | -#' # dummy i variable to fix it |
92 | | -#' i <- NULL" |
93 | | -#' ) |
94 | | -#' y <- eval_code( |
95 | | -#' common_q, |
96 | | -#' "y <- numeric(0) |
97 | | -#' for (i in 1:3) { |
98 | | -#' y <- c(y, i) |
99 | | -#' } |
100 | | -#' # dummy i variable to fix it |
101 | | -#' i <- NULL" |
102 | | -#' ) |
103 | | -#' q <- join(x,y) |
104 | | -#' } |
105 | | -#' } |
| 10 | +#' 1. Both `qenv` objects contain an object of the same name but are not identical. |
| 11 | +#' |
| 12 | +#' Example: |
| 13 | +#' |
| 14 | +#' ```r |
| 15 | +#' x <- eval_code(qenv(), expression(mtcars1 <- mtcars)) |
| 16 | +#' y <- eval_code(qenv(), expression(mtcars1 <- mtcars['wt'])) |
| 17 | +#' |
| 18 | +#' z <- join(x, y) |
| 19 | +#' # Error message will occur |
| 20 | +#' ``` |
| 21 | +#' In this example, `mtcars1` object exists in both `x` and `y` objects but the content are not identical. |
| 22 | +#' `mtcars1` in the `x qenv` object has more columns than `mtcars1` in the `y qenv` object (only has one column). |
| 23 | +#' |
| 24 | +#' 2. `join()` will look for identical `@id` values in both `qenv` objects. |
| 25 | +#' The index position of these `@id`s must be the same to determine the evaluation order. |
| 26 | +#' Otherwise, `join()` will throw an error message. |
| 27 | +#' |
| 28 | +#' Example: |
| 29 | +#' ```r |
| 30 | +#' common_q <- eval_code(qenv(), expression(v <- 1)) |
| 31 | +#' x <- eval_code( |
| 32 | +#' common_q, |
| 33 | +#' "x <- v" |
| 34 | +#' ) |
| 35 | +#' y <- eval_code( |
| 36 | +#' common_q, |
| 37 | +#' "y <- v" |
| 38 | +#' ) |
| 39 | +#' z <- eval_code( |
| 40 | +#' y, |
| 41 | +#' "z <- v" |
| 42 | +#' ) |
| 43 | +#' q <- join(x, y) |
| 44 | +#' join_q <- join(q, z) |
| 45 | +#' # Error message will occur |
| 46 | +#' |
| 47 | +#' # Check the order of evaluation based on the id slot |
| 48 | +#' shared_ids <- intersect(q@id, z@id) |
| 49 | +#' match(shared_ids, q@id) # Output: 1 3 |
| 50 | +#' match(shared_ids, z@id) # Output: 1 2 |
| 51 | +#' ``` |
| 52 | +#' The error occurs because the index position of identical `@id` between the two objects is not the same. |
| 53 | +#' |
| 54 | +#' 3. The usage of temporary variable in the code expression could cause `join()` to fail. |
| 55 | +#' |
| 56 | +#' Example: |
| 57 | +#' ```r |
| 58 | +#' common_q <- qenv() |
| 59 | +#' x <- eval_code( |
| 60 | +#' common_q, |
| 61 | +#' "x <- numeric(0) |
| 62 | +#' for (i in 1:2) { |
| 63 | +#' x <- c(x, i) |
| 64 | +#' }" |
| 65 | +#' ) |
| 66 | +#' y <- eval_code( |
| 67 | +#' common_q, |
| 68 | +#' "y <- numeric(0) |
| 69 | +#' for (i in 1:3) { |
| 70 | +#' y <- c(y, i) |
| 71 | +#' }" |
| 72 | +#' ) |
| 73 | +#' q <- join(x,y) |
| 74 | +#' # Error message will occur |
| 75 | +#' |
| 76 | +#' # Check the value of temporary variable i in both objects |
| 77 | +#' x@env$i # Output: 2 |
| 78 | +#' y@env$i # Output: 3 |
| 79 | +#' ``` |
| 80 | +#' `join()` fails to provide a proper result because of the temporary variable `i` exists |
| 81 | +#' in both objects but has different value. |
| 82 | +#' To fix this, we can set `i <- NULL` in the code expression for both objects. |
| 83 | +#' ```r |
| 84 | +#' common_q <- qenv() |
| 85 | +#' x <- eval_code( |
| 86 | +#' common_q, |
| 87 | +#' "x <- numeric(0) |
| 88 | +#' for (i in 1:2) { |
| 89 | +#' x <- c(x, i) |
| 90 | +#' } |
| 91 | +#' # dummy i variable to fix it |
| 92 | +#' i <- NULL" |
| 93 | +#' ) |
| 94 | +#' y <- eval_code( |
| 95 | +#' common_q, |
| 96 | +#' "y <- numeric(0) |
| 97 | +#' for (i in 1:3) { |
| 98 | +#' y <- c(y, i) |
| 99 | +#' } |
| 100 | +#' # dummy i variable to fix it |
| 101 | +#' i <- NULL" |
| 102 | +#' ) |
| 103 | +#' q <- join(x,y) |
| 104 | +#' ``` |
106 | 105 | #' |
107 | 106 | #' @param x (`qenv`) |
108 | 107 | #' @param y (`qenv`) |
@@ -167,7 +166,7 @@ setMethod("join", signature = c("qenv.error", "ANY"), function(x, y) { |
167 | 166 | #' If two `qenv` can be joined |
168 | 167 | #' |
169 | 168 | #' Checks if two `qenv` objects can be combined. |
170 | | -#' For more information, please see \code{\link{join}} |
| 169 | +#' For more information, please see [`join`] |
171 | 170 | #' @param x (`qenv`) |
172 | 171 | #' @param y (`qenv`) |
173 | 172 | #' @return `TRUE` if able to join or `character` used to print error message. |
|
0 commit comments