Skip to content

Commit 25f6a4f

Browse files
committed
feat: change to keep .Last.value in parent environment
1 parent c8ec257 commit 25f6a4f

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

R/qenv-eval_code.R

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,11 @@ setMethod("eval_code", signature = c(object = "qenv.error"), function(object, co
6363
x <- withCallingHandlers(
6464
tryCatch(
6565
{
66-
.Last.value <- eval(current_call, envir = object@.xData)
67-
attr(object@.xData, ".Last.value") <- .Last.value
68-
if (!identical(parent.env(object@.xData), parent.env(.GlobalEnv))) {
69-
# needed to make sure that @.xData is always a sibling of .GlobalEnv
70-
# could be changed when any new package is added to search path (through library or require call)
71-
parent.env(object@.xData) <- parent.env(.GlobalEnv)
72-
}
66+
# needed to make sure that @.xData inherits from .GlobalEnv
67+
# could be changed when any new package is added to search path (through library or require call)
68+
new_parent <- new.env(parent = parent.env(.GlobalEnv))
69+
new_parent[[".Last.value"]] <- eval(current_call, envir = object@.xData)
70+
parent.env(object@.xData) <- new_parent
7371
NULL
7472
},
7573
error = function(e) {

man/within.qenv.Rd

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

tests/testthat/test-qenv_eval_code.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ testthat::test_that("comments passed alone to eval_code that contain @linksto ta
189189

190190
testthat::test_that("eval_code keeps .Last.value as an attribute of the environment", {
191191
q <- eval_code(qenv(), quote(x <- 1))
192-
env <- as.environment(q)
193-
testthat::expect_true(".Last.value" %in% names(attributes(env)))
194-
testthat::expect_equal(attr(env, ".Last.value"), 1)
192+
env <- parent.env(q)
193+
testthat::expect_true(".Last.value" %in% names(env))
194+
testthat::expect_equal(env$.Last.value, 1)
195195
})

tests/testthat/test-qenv_within.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ testthat::test_that("Code executed with integer shorthand (1L) is the same as or
157157

158158
testthat::test_that("within keeps .Last.value as an attribute of the environment", {
159159
q <- within(qenv(), x <- 1)
160-
env <- as.environment(q)
161-
testthat::expect_true(".Last.value" %in% names(attributes(env)))
162-
testthat::expect_equal(attr(env, ".Last.value"), 1)
160+
env <- parent.env(q)
161+
testthat::expect_true(".Last.value" %in% names(env))
162+
testthat::expect_equal(env$.Last.value, 1)
163163
})

0 commit comments

Comments
 (0)