-
Notifications
You must be signed in to change notification settings - Fork 35
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
Move all plot watching logic into watchout #151
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
796e6e9
Move check of active device to watchout
hadley 36628ba
Separate output and plot capture
hadley be153f8
Record plot state in watcher
hadley 1754e24
Move devices check up
hadley 0748e3c
Record current devices in watchout
hadley a7bd643
Inline plot snapshot
hadley 07a4f7a
Also handle creation of graphics device
hadley d0ac039
Remove outdated comment
hadley 27e3983
Simplify test
hadley 0b27207
Merged origin/main into watch-plot
hadley f39fbcd
Drop unneeded code
hadley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,40 +69,18 @@ evaluate <- function(input, | |
} | ||
local_inject_funs(envir) | ||
|
||
if (new_device) { | ||
# Ensure we have a graphics device available for recording, but choose | ||
# one that's available on all platforms and doesn't write to disk | ||
pdf(file = NULL) | ||
dev.control(displaylist = "enable") | ||
dev <- dev.cur() | ||
on.exit(dev.off(dev)) | ||
} | ||
# record the list of current devices | ||
devs <- .env$dev_list; on.exit(.env$dev_list <- devs, add = TRUE) | ||
devn <- length(.env$dev_list <- dev.list()) | ||
dev <- dev.cur() | ||
|
||
# clean up the last_plot object after an evaluate() call (cf yihui/knitr#722) | ||
on.exit(assign("last_plot", NULL, envir = environment(plot_snapshot)), add = TRUE) | ||
|
||
# if this env var is set to true, always bypass messages | ||
if (tolower(Sys.getenv('R_EVALUATE_BYPASS_MESSAGES')) == 'true') | ||
keep_message = keep_warning = NA | ||
|
||
# Capture output | ||
watcher <- watchout(output_handler, debug = debug) | ||
watcher <- watchout(output_handler, new_device = new_device, debug = debug) | ||
|
||
out <- vector("list", nrow(parsed)) | ||
for (i in seq_along(out)) { | ||
if (debug) { | ||
message(parsed$src[[i]]) | ||
} | ||
|
||
# if dev.off() was called, make sure to restore device to the one opened by | ||
# evaluate() or existed before evaluate() | ||
if (length(dev.list()) < devn) dev.set(dev) | ||
devn <- length(dev.list()) | ||
|
||
out[[i]] <- evaluate_top_level_expression( | ||
exprs = parsed$expr[[i]], | ||
src = parsed$src[[i]], | ||
|
@@ -117,6 +95,7 @@ evaluate <- function(input, | |
output_handler = output_handler, | ||
include_timing = include_timing | ||
) | ||
watcher$check_devices() | ||
|
||
if (stop_on_error > 0L) { | ||
errs <- vapply(out[[i]], is.error, logical(1)) | ||
|
@@ -154,12 +133,12 @@ evaluate_top_level_expression <- function(exprs, | |
source <- new_source(src, exprs[[1]], output_handler$source) | ||
output <- list(source) | ||
|
||
dev <- dev.cur() | ||
handle_output <- function(plot = TRUE, incomplete_plots = FALSE) { | ||
# if dev.cur() has changed, we should not record plots any more | ||
plot <- plot && identical(dev, dev.cur()) | ||
out <- watcher(plot, incomplete_plots) | ||
output <<- c(output, out) | ||
out <- list( | ||
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. I don't like this interface so it's likely to change in a future PR. |
||
if (plot) watcher$capture_plot(incomplete_plots), | ||
watcher$capture_output() | ||
) | ||
output <<- c(output, compact(out)) | ||
} | ||
|
||
local_output_handler(function() handle_output(FALSE)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just curious: what does motivate the move for this refactored code after
evaluate_top_level_expression
?All tests are passing so seems good - but wanted to understand the thinking as I would have place it in the same place by precaution
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.
Over time, my plan is to move to more and more logic in to
evaluate()
so the logic is more clear. In this case specifically, I think it makes sense to check the devices after you've run the user code, rather than before. (Although the net effect is generally going to be the same.)