-
-
Notifications
You must be signed in to change notification settings - Fork 8
Keeps last value of eval_code()/within()
#257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fb5cf2b
6971dca
7abc267
a075fce
78396d6
bb1ab11
2eab262
7959973
5605340
3cd6246
7e1f07a
0110716
f08c90b
e7d2759
909c0f0
c8ec257
25f6a4f
be517a3
d6c9c0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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) { | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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", { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| q <- eval_code(qenv(), quote(x <- 1)) | ||||||
| env <- parent.env(q) | ||||||
| testthat::expect_true(".Last.value" %in% names(env)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_equal(env$.Last.value, 1) | ||||||
| }) | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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", { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| q <- within(qenv(), x <- 1) | ||||||
| env <- parent.env(q) | ||||||
| testthat::expect_true(".Last.value" %in% names(env)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_equal(env$.Last.value, 1) | ||||||
| }) | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.