Skip to content
Closed
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# teal.code 0.6.1.9003

### Enhancements

* Code evaluation keeps the last evaluated expression in the `.Last.value` attribute of the environment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Code evaluation keeps the last evaluated expression in the `.Last.value` attribute of the environment.
* Code evaluation keeps the last evaluated expression in the `parent.env(<qenv>)$.Last.value`.


### Bug fixes

* Fix a problem detecting co-occurrences when expression has multiple lines.
Expand Down
14 changes: 8 additions & 6 deletions R/qenv-eval_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#'
#' @return
#' `qenv` environment with `code/expr` evaluated or `qenv.error` if evaluation fails.
#' The environment contains an attribute called `".Last.value"` which is the last evaluated value,
#' similarly to [base::.Last.value].
#'
#' @examples
#' # evaluate code in qenv
Expand Down Expand Up @@ -61,12 +63,12 @@ setMethod("eval_code", signature = c(object = "qenv.error"), function(object, co
x <- withCallingHandlers(
tryCatch(
{
eval(current_call, envir = object@.xData)
if (!identical(parent.env([email protected]), parent.env(.GlobalEnv))) {
# needed to make sure that @.xData is always a sibling of .GlobalEnv
# could be changed when any new package is added to search path (through library or require call)
parent.env([email protected]) <- parent.env(.GlobalEnv)
}
# needed to make sure that @.xData inherits from .GlobalEnv
# could be changed when any new package is added to search path (through library or require call)
qenv_last_value <- eval(current_call, envir = object@.xData)
new_parent <- new.env(parent = parent.env(.GlobalEnv))
new_parent[[".Last.value"]] <- qenv_last_value
parent.env([email protected]) <- new_parent
NULL
},
error = function(e) {
Expand Down
2 changes: 2 additions & 0 deletions man/eval_code.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-qenv_eval_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,10 @@ testthat::test_that("comments passed alone to eval_code that contain @linksto ta
"x"
)
})

testthat::test_that("eval_code keeps .Last.value as an attribute of the environment", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
testthat::test_that("eval_code keeps .Last.value as an attribute of the environment", {
testthat::test_that("eval_code stores .Last.value in the parent environment", {

q <- eval_code(qenv(), quote(x <- 1))
env <- parent.env(q)
testthat::expect_true(".Last.value" %in% names(env))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not needed as the next one confirms this also

Suggested change
testthat::expect_true(".Last.value" %in% names(env))

testthat::expect_equal(env$.Last.value, 1)
})
7 changes: 7 additions & 0 deletions tests/testthat/test-qenv_within.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,10 @@ testthat::test_that("Code executed with integer shorthand (1L) is the same as or
q <- within(qenv(), a <- 1L)
testthat::expect_identical(get_code(q), "a <- 1L")
})

testthat::test_that("within keeps .Last.value as an attribute of the environment", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
testthat::test_that("within keeps .Last.value as an attribute of the environment", {
testthat::test_that("within eval_code stores .Last.value in the parent environment", {

q <- within(qenv(), x <- 1)
env <- parent.env(q)
testthat::expect_true(".Last.value" %in% names(env))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not needed as the next one confirms this also

Suggested change
testthat::expect_true(".Last.value" %in% names(env))

testthat::expect_equal(env$.Last.value, 1)
})
Loading