Skip to content

Commit e7d2759

Browse files
committed
feat: keep last value of eval_code/within
1 parent f08c90b commit e7d2759

File tree

6 files changed

+27
-10
lines changed

6 files changed

+27
-10
lines changed

R/qenv-eval_code.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#'
1414
#' @return
1515
#' `qenv` environment with `code/expr` evaluated or `qenv.error` if evaluation fails.
16+
#' The environment contains an attribute called `".Last.value"` which is the last evaluated value,
17+
#' similarly to [base::.Last.value].
1618
#'
1719
#' @examples
1820
#' # evaluate code in qenv
@@ -61,7 +63,8 @@ setMethod("eval_code", signature = c(object = "qenv.error"), function(object, co
6163
x <- withCallingHandlers(
6264
tryCatch(
6365
{
64-
eval(current_call, envir = object@.xData)
66+
.Last.value <- eval(current_call, envir = object@.xData)
67+
attr(object@.xData, ".Last.value") <- .Last.value
6568
if (!identical(parent.env(object@.xData), parent.env(.GlobalEnv))) {
6669
# needed to make sure that @.xData is always a sibling of .GlobalEnv
6770
# could be changed when any new package is added to search path (through library or require call)

R/qenv-within.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#' Evaluate code in `qenv`
2-
#' @details
3-
#' `within()` is a convenience method that wraps `eval_code` to provide a simplified way of passing expression.
2+
#'
3+
#' @description Convenience method that wraps [eval_code()] to provide a simplified way of passing expression.
4+
#'
45
#' `within` accepts only inline expressions (both simple and compound) and allows to substitute `expr`
56
#' with `...` named argument values.
67
#'
7-
#' @section Using language objects with `within`:
8+
#' # Using language objects with `within`:
89
#' Passing language objects to `expr` is generally not intended but can be achieved with `do.call`.
910
#' Only single `expression`s will work and substitution is not available. See examples.
1011
#'

man/eval_code.Rd

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

man/within.qenv.Rd

Lines changed: 3 additions & 6 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,10 @@ testthat::test_that("comments passed alone to eval_code that contain @linksto ta
186186
"x"
187187
)
188188
})
189+
190+
testthat::test_that("eval_code keeps .Last.value as an attribute of the environment", {
191+
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)
195+
})

tests/testthat/test-qenv_within.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,10 @@ testthat::test_that("Code executed with integer shorthand (1L) is the same as or
154154
q <- within(qenv(), a <- 1L)
155155
testthat::expect_identical(get_code(q), "a <- 1L")
156156
})
157+
158+
testthat::test_that("within keeps .Last.value as an attribute of the environment", {
159+
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)
163+
})

0 commit comments

Comments
 (0)