Skip to content
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

Ensure on.exit() runs at end of chunk #204

Merged
merged 6 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# evaluate (development version)

* `on.exit()` will now run at the end of the evaluate code, rather than immediately. This makes the scope of knitr chunks resemble the scope of R functions (#201).
* `parse_all()` adds a `\n` to the end of every line, even the last one if it didn't have one in the input.
* Setting `ACTIONS_STEP_DEBUG=1` (as in a failing GHA workflow) will automatically set `log_echo` and `log_warning` to `TRUE` (#175).
* New `local_reproducible_output()` helper that sets various options and env vars to help ensure consistency of output across environments.
Expand Down
2 changes: 1 addition & 1 deletion R/conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ with_handlers <- function(code, handlers) {
}

sanitize_call <- function(cnd) {
if (identical(cnd$call, quote(eval(expr, envir)))) {
if (identical(cnd$call, quote(withVisible(do)))) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
cnd$call <- NULL
}
cnd
Expand Down
65 changes: 40 additions & 25 deletions R/evaluate.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,34 +134,49 @@ evaluate <- function(input,
# The user's condition handlers have priority over ours
handlers <- c(user_handlers, evaluate_handlers)

for (tle in tles) {
watcher$push_source(tle$src, tle$exprs)
if (debug || log_echo) {
cat_line(tle$src, file = stderr())
}
cb <- function() {
hadley marked this conversation as resolved.
Show resolved Hide resolved
do <- NULL # silence R CMD check note

continue <- withRestarts(
with_handlers(
{
for (expr in tle$exprs) {
ev <- withVisible(eval(expr, envir))
watcher$capture_plot_and_output()
watcher$print_value(ev$value, ev$visible)
}
TRUE
},
handlers
),
eval_continue = function() TRUE,
eval_stop = function() FALSE,
eval_error = function(cnd) stop(cnd)
)
watcher$check_devices()

if (!continue) {
break
for (tle in tles) {
watcher$push_source(tle$src, tle$exprs)
if (debug || log_echo) {
cat_line(tle$src, file = stderr())
}

continue <- withRestarts(
with_handlers(
{
for (expr in tle$exprs) {
# Using `delayedAssign()` as an interface to the C-level function
# `Rf_eval()`. Unlike the R-level `eval()`, this doesn't create
# an unwinding scope.
eval(bquote(delayedAssign("do", .(expr), eval.env = envir)))

ev <- withVisible(do)
watcher$capture_plot_and_output()
watcher$print_value(ev$value, ev$visible)
}
TRUE
},
handlers
),
eval_continue = function() TRUE,
eval_stop = function() FALSE,
eval_error = function(cnd) stop(cnd)
)
watcher$check_devices()

if (!continue) {
break
}
}
}

# Here we use `eval()` to create an unwinding scope for `envir`.
# We call ourselves back immediately once the scope is created.
eval(as.call(list(cb)), envir)
watcher$capture_output()

# Always capture last plot, even if incomplete
watcher$capture_plot(TRUE)

Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/_snaps/evaluate.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# on.exit is evaluated at end of code

Code
ev
Output
<evaluation>
Source code:
on.exit(print('bye'))
Source code:
print('hi')
Text output:
[1] "hi"
Text output:
[1] "bye"

# check_stop_on_error converts integer to enum

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-evaluate.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ test_that("multiple lines of comments do not lose the terminating \\n", {
expect_equal(ev[[1]]$src, "# foo\n")
})

test_that("on.exit is evaluated at end of code", {
ev <- evaluate::evaluate(c(
"on.exit(print('bye'))",
"print('hi')"
))
expect_snapshot(ev)
})

test_that("check_stop_on_error converts integer to enum", {
expect_equal(check_stop_on_error(0), "continue")
expect_equal(check_stop_on_error(1), "stop")
Expand Down
Loading