Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/qenv-c.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ c.qenv <- function(...) {
x@code <- utils::modifyList(x@code, y@code)

# insert (and overwrite) objects from y to x
x@.xData <- rlang::env_clone(x@.xData, parent = parent.env(.GlobalEnv))
x@.xData <- rlang::env_clone(x@.xData)
rlang::env_coalesce(env = x@.xData, from = y@.xData)
x
}
Expand Down
2 changes: 1 addition & 1 deletion R/qenv-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ setMethod(
"initialize",
"qenv",
function(.Object, .xData, code = list(), ...) { # nolint: object_name.
parent <- parent.env(.GlobalEnv)
parent <- parent.env(default_env)
new_xdata <- if (rlang::is_missing(.xData)) {
new.env(parent = parent)
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/qenv-concat.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ setMethod("concat", signature = c("qenv", "qenv"), function(x, y) {
y@code <- c(x@code, y@code)

# insert (and overwrite) objects from y to x
y@.xData <- rlang::env_clone(y@.xData, parent = parent.env(.GlobalEnv))
y@.xData <- rlang::env_clone(y@.xData)
rlang::env_coalesce(env = y@.xData, from = x@.xData)
y
})
Expand Down
6 changes: 3 additions & 3 deletions R/qenv-eval_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ setMethod("eval_code", signature = c(object = "qenv.error"), function(object, co
}
code <- paste(split_code(code), collapse = "\n")

object@.xData <- rlang::env_clone(object@.xData, parent = parent.env(object@.xData))
object@.xData <- rlang::env_clone(object@.xData)
parsed_code <- parse(text = code, keep.source = TRUE, encoding = "UTF-8")

old <- evaluate::inject_funs(
library = function(...) {
x <- library(...)
if (!identical(parent.env(object@.xData), parent.env(.GlobalEnv))) {
parent.env(object@.xData) <- parent.env(.GlobalEnv)
if (!identical(parent.env(object@.xData), parent.env(default_env))) {
parent.env(object@.xData) <- parent.env(default_env)
}
invisible(x)
}
Expand Down
7 changes: 5 additions & 2 deletions R/qenv-extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
checkmate::assert_character(names, any.missing = FALSE)
possible_names <- ls(get_env(x), all.names = TRUE)
names_corrected <- intersect(names, possible_names)
parent <- parent.env(default_env)

env <- if (length(names_corrected)) {
names_missing <- setdiff(names, possible_names)
if (length(names_missing)) {
Expand All @@ -32,7 +34,7 @@
)
)
}
list2env(as.list(x, all.names = TRUE)[names_corrected], parent = parent.env(.GlobalEnv))
list2env(as.list(x, all.names = TRUE)[names_corrected], parent = parent)
} else {
warning(
sprintf(
Expand All @@ -41,7 +43,8 @@
),
call. = FALSE
)
new.env(parent = parent.env(.GlobalEnv))
# Ensures that an empty environment is returned
new.env(parent = parent)
}
lockEnvironment(env)
x@.xData <- env
Expand Down
3 changes: 3 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ get_warn_message_util <- function(object, what) {
paste(get_code(object), collapse = "\n")
)
}

# Setting a default environment
default_env <- .GlobalEnv
56 changes: 56 additions & 0 deletions tests/testthat/test-qenv_constructor.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,60 @@ describe("parent of qenv environment is the parent of .GlobalEnv", {
q <- qenv()
expect_identical(parent.env(q), parent.env(.GlobalEnv))
})

it("but doesn't mess up with library calls inside qenv", {
skip_if_not_installed("knitr")
expect_s4_class(q <- within(qenv(), {
library(knitr)
labels <- knitr::all_labels()
}), "qenv")
})

it("cannot get variable after subsetting", {
q <- qenv() |> within({
a <- 2
b <- 3
})
q2 <- q["a"]
expect_error(get("b", envir = q2), "not found")
})

it("does not change the original environment when used to create", {
e <- list2env(list(a = 2))
q <- new("qenv", .xData = e)
expect_no_error(e$b <- 3)
expect_no_error(e$a <- 3)
expect_equal(q$a, 2)
expect_equal(e$a, 3)
})


it("with the right environment", {
c <- 5
q <- qenv() |> within(aa <- 2)
q2 <- q |> within(1 + 1)

expect_identical(get("c", envir = q), base::c)

parent.env(q2@.xData) <- parent.env(.GlobalEnv)
expect_identical(get("c", envir = q2), base::c)
})
Comment thread
averissimo marked this conversation as resolved.
Resolved

it("does not get variables from global environment", {
.GlobalEnv$a_variable <- 99 # nolintr object_name_linter.
q <- qenv() |> within({
some_variable <- 42
})
expect_error(get("a_variable", envir = q), "not found")
rm("a_variable", envir = .GlobalEnv)
})

it("does not get variables from current environment", {
a_variable <- 99
q <- qenv() |> within({
some_variable <- 42
})
expect_error(get("a_variable", envir = q), "not found")
rm("a_variable")
})
})
Loading