Skip to content

Commit defa490

Browse files
committed
WIP - postmerge
1 parent b807058 commit defa490

File tree

14 files changed

+126
-341
lines changed

14 files changed

+126
-341
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
S3method("[",qenv)
43
S3method("$",qenv.error)
4+
S3method("[",qenv)
55
S3method("[[",qenv.error)
66
S3method(as.list,qenv.error)
77
S3method(c,qenv)

R/qenv-c.R

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
)
2525
}
2626

27-
shared_ids <- intersect(x@id, y@id)
27+
x_id <- get_code_attr(x, "id")
28+
y_id <- get_code_attr(y, "id")
29+
30+
shared_ids <- intersect(x_id, y_id)
2831
if (length(shared_ids) == 0) {
2932
return(TRUE)
3033
}
3134

32-
shared_in_x <- match(shared_ids, x@id)
33-
shared_in_y <- match(shared_ids, y@id)
35+
shared_in_x <- match(shared_ids, x_id)
36+
shared_in_y <- match(shared_ids, y_id)
3437

3538
# indices of shared ids should be 1:n in both slots
3639
if (identical(shared_in_x, shared_in_y) && identical(shared_in_x, seq_along(shared_ids))) {
@@ -86,11 +89,7 @@ c.qenv <- function(...) {
8689
stop(join_validation)
8790
}
8891

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])
92+
x@code <- union(x@code, y@code)
9493

9594
# insert (and overwrite) objects from y to x
9695
x@.xData <- rlang::env_clone(x@.xData, parent = parent.env(.GlobalEnv))

R/qenv-class.R

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@
2121
#' @exportClass qenv
2222
setClass(
2323
"qenv",
24-
slots = c(
25-
code = "character",
26-
id = "integer",
27-
warnings = "character",
28-
messages = "character"
29-
),
24+
slots = c(code = "list"),
3025
contains = "environment"
3126
)
3227

@@ -37,25 +32,7 @@ setMethod(
3732
"qenv",
3833
function(.Object, # nolint: object_name.
3934
.xData, # nolint: object_name.
40-
code = character(0L),
41-
warnings = rep("", length(code)),
42-
messages = rep("", length(code)),
43-
id = integer(0L),
4435
...) {
45-
# # Pre-process parameters to ensure they are ready to be used by parent constructors
46-
stopifnot("`code` must be a character or language object." = any(is.language(code), is.character(code)))
47-
48-
if (is.language(code)) {
49-
code <- paste(lang2calls(code), collapse = "\n")
50-
}
51-
if (length(code)) {
52-
code <- paste(code, collapse = "\n")
53-
}
54-
55-
if (length(id) == 0L) {
56-
id <- sample.int(.Machine$integer.max, size = length(code))
57-
}
58-
5936
new_xdata <- if (rlang::is_missing(.xData)) {
6037
new.env(parent = parent.env(.GlobalEnv))
6138
} else {
@@ -67,28 +44,22 @@ setMethod(
6744
# .xData needs to be unnamed as the `.environment` constructor allows at
6845
# most 1 unnamed formal argument of class `environment`.
6946
# See methods::findMethods("initialize")$.environment
70-
.Object <- methods::callNextMethod( # nolint: object_name.
47+
methods::callNextMethod(
7148
# Mandatory use of `xData` to build a correct [email protected]
72-
.Object, new_xdata,
73-
code = code, messages = messages, warnings = warnings, id = id, ...
49+
.Object, new_xdata, ...
7450
)
75-
76-
.Object
7751
}
7852
)
7953

8054
#' It takes a `qenv` class and returns `TRUE` if the input is valid
8155
#' @name qenv-class
8256
#' @keywords internal
8357
setValidity("qenv", function(object) {
84-
if (length(object@code) != length(object@id)) {
85-
"@code and @id slots must have the same length."
86-
} else if (length(object@code) != length(object@warnings)) {
87-
"@code and @warnings slots must have the same length"
88-
} else if (length(object@code) != length(object@messages)) {
89-
"@code and @messages slots must have the same length"
90-
} else if (any(duplicated(object@id))) {
91-
"@id contains duplicated values."
58+
ids <- lapply(object@code, "attr", "id")
59+
if (any(sapply(ids, is.null))) {
60+
"All @code slots must have an 'id' attribute"
61+
} else if (any(duplicated(unlist(ids)))) {
62+
"@code contains duplicated 'id' attributes."
9263
} else if (!environmentIsLocked(object@.xData)) {
9364
"@.xData must be locked."
9465
} else {

R/qenv-eval_code.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ setGeneric("eval_code", function(object, code) standardGeneric("eval_code"))
2929

3030
setMethod("eval_code", signature = c("qenv", "character"), function(object, code) {
3131
parsed_code <- parse(text = code, keep.source = TRUE)
32-
object@env <- rlang::env_clone(object@env, parent = parent.env(.GlobalEnv))
32+
object@.xData <- rlang::env_clone(object@.xData, parent = parent.env(.GlobalEnv))
3333
if (length(parsed_code) == 0) {
3434
# empty code, or just comments
3535
attr(code, "id") <- sample.int(.Machine$integer.max, size = 1)
@@ -48,9 +48,9 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
4848
x <- withCallingHandlers(
4949
tryCatch(
5050
{
51-
eval(current_call, envir = object@env)
52-
if (!identical(parent.env(object@env), parent.env(.GlobalEnv))) {
53-
# needed to make sure that @env is always a sibling of .GlobalEnv
51+
eval(current_call, envir = object@.xData)
52+
if (!identical(parent.env(object@.xData), parent.env(.GlobalEnv))) {
53+
# needed to make sure that @.xData is always a sibling of .GlobalEnv
5454
# could be changed when any new package is added to search path (through library or require call)
5555
parent.env(object@.xData) <- parent.env(.GlobalEnv)
5656
}
@@ -87,7 +87,7 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
8787
object@code <- c(object@code, list(current_code))
8888
}
8989

90-
lockEnvironment(object@env, bindings = TRUE)
90+
lockEnvironment(object@.xData, bindings = TRUE)
9191
object
9292
})
9393

R/qenv-extract.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
)
4343
}
4444

45-
x@env <- list2env(mget(x = names, envir = get_env(x)))
45+
x@.xData <- list2env(as.list(x)[names])
4646
names <- gsub("^`(.*)`$", "\\1", names)
4747
x@code <- get_code_dependency(x@code, names = names, ...)
4848

R/qenv-join.R

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,6 @@ setGeneric("join", function(x, y) standardGeneric("join"))
139139
setMethod("join", signature = c("qenv", "qenv"), function(x, y) {
140140
lifecycle::deprecate_soft("0.5.1", "join()", "c()")
141141
c(x, y)
142-
join_validation <- .check_joinable(x, y)
143-
144-
# join expressions
145-
if (!isTRUE(join_validation)) {
146-
stop(join_validation)
147-
}
148-
149-
id_unique <- !get_code_attr(y, "id") %in% get_code_attr(x, "id")
150-
x@code <- c(x@code, y@code[id_unique])
151-
152-
# insert (and overwrite) objects from y to x
153-
x@env <- rlang::env_clone(x@env, parent = parent.env(.GlobalEnv))
154-
rlang::env_coalesce(env = x@env, from = y@env)
155-
x
156142
})
157143

158144
setMethod("join", signature = c("qenv", "qenv.error"), function(x, y) {
@@ -164,57 +150,3 @@ setMethod("join", signature = c("qenv.error", "ANY"), function(x, y) {
164150
lifecycle::deprecate_soft("0.5.1", "join()", "c()")
165151
x
166152
})
167-
168-
#' If two `qenv` can be joined
169-
#'
170-
#' Checks if two `qenv` objects can be combined.
171-
#' For more information, please see [`join`]
172-
#' @param x (`qenv`)
173-
#' @param y (`qenv`)
174-
#' @return `TRUE` if able to join or `character` used to print error message.
175-
#' @keywords internal
176-
.check_joinable <- function(x, y) {
177-
checkmate::assert_class(x, "qenv")
178-
checkmate::assert_class(y, "qenv")
179-
180-
common_names <- intersect(rlang::env_names(x@env), rlang::env_names(y@env))
181-
is_overwritten <- vapply(common_names, function(el) {
182-
!identical(get(el, x@env), get(el, y@env))
183-
}, logical(1))
184-
if (any(is_overwritten)) {
185-
return(
186-
paste(
187-
"Not possible to join qenv objects if anything in their environment has been modified.\n",
188-
"Following object(s) have been modified:\n - ",
189-
paste(common_names[is_overwritten], collapse = "\n - ")
190-
)
191-
)
192-
}
193-
x_id <- get_code_attr(x, "id")
194-
y_id <- get_code_attr(y, "id")
195-
196-
shared_ids <- intersect(x_id, y_id)
197-
if (length(shared_ids) == 0) {
198-
return(TRUE)
199-
}
200-
201-
shared_in_x <- match(shared_ids, x_id)
202-
shared_in_y <- match(shared_ids, y_id)
203-
204-
# indices of shared ids should be 1:n in both slots
205-
if (identical(shared_in_x, shared_in_y) && identical(shared_in_x, seq_along(shared_ids))) {
206-
TRUE
207-
} else if (!identical(shared_in_x, shared_in_y)) {
208-
paste(
209-
"The common shared code of the qenvs does not occur in the same position in both qenv objects",
210-
"so they cannot be joined together as it's impossible to determine the evaluation's order.",
211-
collapse = ""
212-
)
213-
} else {
214-
paste(
215-
"There is code in the qenv objects before their common shared code",
216-
"which means these objects cannot be joined.",
217-
collapse = ""
218-
)
219-
}
220-
}

man/get_env.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/qenv-class.Rd

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

man/qenv.Rd

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

tests/testthat/test-qenv_constructor.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ testthat::test_that("constructor returns qenv", {
5151
q <- qenv()
5252
testthat::expect_s4_class(q, "qenv")
5353
testthat::expect_identical(names(q), character(0))
54-
testthat::expect_identical(q@code, character(0))
55-
testthat::expect_identical(q@id, integer(0))
56-
testthat::expect_identical(q@warnings, character(0))
57-
testthat::expect_identical(q@messages, character(0))
54+
testthat::expect_identical(q@code, list())
5855
})
5956

6057
testthat::describe("parent of qenv environment is the parent of .GlobalEnv", {

0 commit comments

Comments
 (0)