Skip to content

Commit 015f11c

Browse files
committed
fix: problems with check
1 parent 2f553eb commit 015f11c

File tree

11 files changed

+108
-158
lines changed

11 files changed

+108
-158
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Language: en-US
5353
Roxygen: list(markdown = TRUE)
5454
RoxygenNote: 7.3.2
5555
Collate:
56+
'qenv-c.R'
5657
'qenv-class.R'
5758
'qenv-errors.R'
5859
'qenv-concat.R'

R/qenv-c.R

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
#' @export
54+
c.qenv.error <- function(...) {
55+
rlang::list2(...)[[1]]
56+
}
57+
58+
#' @export
59+
c.qenv <- function(...) {
60+
dots <- rlang::list2(...)
61+
if (!checkmate::test_list(dots[-1], types = c("qenv", "qenv.error"))) {
62+
return(NextMethod(c, dots[[1]]))
63+
}
64+
65+
first_non_qenv_ix <- which.min(vapply(dots, inherits, what = "qenv", logical(1)))
66+
if (first_non_qenv_ix > 1) {
67+
return(dots[[first_non_qenv_ix]])
68+
}
69+
70+
Reduce(
71+
x = dots[-1],
72+
init = dots[[1]],
73+
f = function(x, y) {
74+
join_validation <- .check_joinable(x, y)
75+
76+
# join expressions
77+
if (!isTRUE(join_validation)) {
78+
stop(join_validation)
79+
}
80+
81+
id_unique <- !y@id %in% x@id
82+
x@id <- c(x@id, y@id[id_unique])
83+
x@code <- c(x@code, y@code[id_unique])
84+
x@warnings <- c(x@warnings, y@warnings[id_unique])
85+
x@messages <- c(x@messages, y@messages[id_unique])
86+
87+
# insert (and overwrite) objects from y to x
88+
x@.xData <- rlang::env_clone(x@.xData, parent = parent.env(.GlobalEnv))
89+
rlang::env_coalesce(env = x@.xData, from = y@.xData)
90+
x
91+
}
92+
)
93+
}

R/qenv-constructor.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#'
1313
#' @name qenv
1414
#'
15-
#' @return Returns a `qenv` object.
15+
#' @return `qenv` returns a `qenv` object.
1616
#'
1717
#' @seealso [`base::within()`], [`get_var()`], [`get_env()`], [`get_warnings()`], [`join()`], [`concat()`]
1818
#' @examples

R/qenv-get_code.R

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
#' @name qenv-inheritted
22
#' @rdname qenv
33
#'
4-
#' @param x (`qenv`) object.
5-
#' @param name (`character`) name of object.
6-
#' @param pos,envir,all.names,pattern,sorted see [ls()] function for details.
7-
#'
8-
#' @usage x[[name]]
9-
#' x$name
10-
#' names(x)
11-
#' ls(name, pos = -1L, envir = as.environment(pos),
12-
#' all.names = FALSE, pattern, sorted = TRUE)
13-
#'
144
#' @details
5+
#'
156
#' `x[[name]]`, `x$name` and `get(name, x)` are generic \R operators to access the objects in the environment.
167
#' See [`[[`] for more details.
17-
#' `names(x)` and `ls(x)` calls on the `qenv` object and will list all objects in the environment.
8+
#' `names(x)` calls on the `qenv` object and will list all objects in the environment.
189
#'
1910
#' @return `[[`, `$` and `get` return the value of the object named `name` in the `qenv` object.
2011
#' @return `names` return a character vector of all the names of the objects in the `qenv` object.
@@ -31,7 +22,6 @@
3122
#' names(q)
3223
NULL
3324

34-
3525
#' Get code from `qenv`
3626
#'
3727
#' @details

R/qenv-get_var.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ setMethod("get_var", signature = c("qenv.error", "ANY"), function(object, var) {
4646
))
4747
})
4848

49-
setMethod("[[", signature = c("qenv", "ANY"), function(x, i) {
50-
get_var(x, i)
51-
})
52-
49+
#' @rdname get_var
5350
#' @export
5451
`[[.qenv.error` <- function(x, i) {
5552
stop(errorCondition(

R/qenv-join.R

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -153,97 +153,3 @@ setMethod("join", signature = c("qenv.error", "ANY"), function(x, y) {
153153
lifecycle::deprecate_soft("0.5.1", "join()", "c()")
154154
x
155155
})
156-
157-
#' If two `qenv` can be joined
158-
#'
159-
#' Checks if two `qenv` objects can be combined.
160-
#' For more information, please see [`join`]
161-
#' @param x (`qenv`)
162-
#' @param y (`qenv`)
163-
#' @return `TRUE` if able to join or `character` used to print error message.
164-
#' @keywords internal
165-
.check_joinable <- function(x, y) {
166-
checkmate::assert_class(x, "qenv")
167-
checkmate::assert_class(y, "qenv")
168-
169-
common_names <- intersect(rlang::env_names(x@.xData), rlang::env_names(y@.xData))
170-
is_overwritten <- vapply(common_names, function(el) {
171-
!identical(get(el, x@.xData), get(el, y@.xData))
172-
}, logical(1))
173-
if (any(is_overwritten)) {
174-
return(
175-
paste(
176-
"Not possible to join qenv objects if anything in their environment has been modified.\n",
177-
"Following object(s) have been modified:\n - ",
178-
paste(common_names[is_overwritten], collapse = "\n - ")
179-
)
180-
)
181-
}
182-
183-
shared_ids <- intersect(x@id, y@id)
184-
if (length(shared_ids) == 0) {
185-
return(TRUE)
186-
}
187-
188-
shared_in_x <- match(shared_ids, x@id)
189-
shared_in_y <- match(shared_ids, y@id)
190-
191-
# indices of shared ids should be 1:n in both slots
192-
if (identical(shared_in_x, shared_in_y) && identical(shared_in_x, seq_along(shared_ids))) {
193-
TRUE
194-
} else if (!identical(shared_in_x, shared_in_y)) {
195-
paste(
196-
"The common shared code of the qenvs does not occur in the same position in both qenv objects",
197-
"so they cannot be joined together as it's impossible to determine the evaluation's order.",
198-
collapse = ""
199-
)
200-
} else {
201-
paste(
202-
"There is code in the qenv objects before their common shared code",
203-
"which means these objects cannot be joined.",
204-
collapse = ""
205-
)
206-
}
207-
}
208-
209-
#' @export
210-
c.qenv.error <- function(...) {
211-
rlang::list2(...)[[1]]
212-
}
213-
214-
#' @export
215-
c.qenv <- function(...) {
216-
dots <- rlang::list2(...)
217-
if (!checkmate::test_list(dots[-1], types = c("qenv", "qenv.error"))) {
218-
return(NextMethod(c, dots[[1]]))
219-
}
220-
221-
first_non_qenv_ix <- which.min(vapply(dots, inherits, what = "qenv", logical(1)))
222-
if (first_non_qenv_ix > 1) {
223-
return(dots[[first_non_qenv_ix]])
224-
}
225-
226-
Reduce(
227-
x = dots[-1],
228-
init = dots[[1]],
229-
f = function(x, y) {
230-
join_validation <- .check_joinable(x, y)
231-
232-
# join expressions
233-
if (!isTRUE(join_validation)) {
234-
stop(join_validation)
235-
}
236-
237-
id_unique <- !y@id %in% x@id
238-
x@id <- c(x@id, y@id[id_unique])
239-
x@code <- c(x@code, y@code[id_unique])
240-
x@warnings <- c(x@warnings, y@warnings[id_unique])
241-
x@messages <- c(x@messages, y@messages[id_unique])
242-
243-
# insert (and overwrite) objects from y to x
244-
x@.xData <- rlang::env_clone(x@.xData, parent = parent.env(.GlobalEnv))
245-
rlang::env_coalesce(env = x@.xData, from = y@.xData)
246-
x
247-
}
248-
)
249-
}

man/dot-check_joinable.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_var.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/qenv.Rd

Lines changed: 3 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-qenv_get_var.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ testthat::test_that("get_var, `$` and `[[` return object from qenv environment",
1616
testthat::expect_equal(q$x, 1)
1717
})
1818

19+
1920
testthat::test_that("get_var, `$` and `[[` return NULL if object not in qenv environment", {
2021
q <- eval_code(qenv(), quote(x <- 1))
2122
q <- eval_code(q, quote(y <- 5 * x))
@@ -24,7 +25,6 @@ testthat::test_that("get_var, `$` and `[[` return NULL if object not in qenv env
2425
testthat::expect_message(get_var(q, "z"), "object 'z' not found")
2526

2627
testthat::expect_null(q[["w"]])
27-
testthat::expect_message(q[["w"]], "object 'w' not found")
2828
testthat::expect_null(q$w)
2929
})
3030

0 commit comments

Comments
 (0)