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

Helper method to capture plot & output #177

Merged
merged 2 commits into from
Jun 25, 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
19 changes: 7 additions & 12 deletions R/eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,12 @@ evaluate_top_level_expression <- function(exprs,
if (!is.null(source))
watcher$push(source)

handle_output <- function(plot = TRUE) {
if (plot) watcher$capture_plot()
watcher$capture_output()
}

local_output_handler(function() handle_output(FALSE))
local_plot_hooks(handle_output)
local_output_handler(watcher$capture_output)
local_plot_hooks(watcher$capture_plot_and_output)

# Handlers for warnings, errors and messages
mHandler <- function(cnd) {
handle_output()
watcher$capture_plot_and_output()
if (isTRUE(keep_message)) {
watcher$push(cnd)
output_handler$message(cnd)
Expand All @@ -157,7 +152,7 @@ evaluate_top_level_expression <- function(exprs,
cat_line(format_condition(cnd), file = stderr())
}

handle_output()
watcher$capture_plot_and_output()
if (isTRUE(keep_warning)) {
cnd <- reset_call(cnd)
watcher$push(cnd)
Expand All @@ -168,7 +163,7 @@ evaluate_top_level_expression <- function(exprs,
}
}
eHandler <- function(cnd) {
handle_output()
watcher$capture_plot_and_output()
if (use_try) {
cnd <- reset_call(cnd)
watcher$errored()
Expand Down Expand Up @@ -199,7 +194,7 @@ evaluate_top_level_expression <- function(exprs,
handlers
)
)
handle_output(TRUE)
watcher$capture_plot_and_output()

if (show_value(output_handler, ev$visible)) {
# Ideally we'd evaluate the print() generic in envir in order to find
Expand All @@ -214,7 +209,7 @@ evaluate_top_level_expression <- function(exprs,
handlers
)
)
handle_output(TRUE)
watcher$capture_plot_and_output()
# If the return value is visible, save the value to the output
if (pv$visible) {
watcher$push(pv$value)
Expand Down
6 changes: 6 additions & 0 deletions R/watcher.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ watchout <- function(handler = new_output_handler(),
invisible()
}

capture_plot_and_output <- function() {
capture_plot()
capture_output()
}

check_devices <- function() {
# if dev.off() was called, make sure to restore device to the one opened
# when watchout() was called
Expand All @@ -92,6 +97,7 @@ watchout <- function(handler = new_output_handler(),
list(
capture_plot = capture_plot,
capture_output = capture_output,
capture_plot_and_output = capture_plot_and_output,
check_devices = check_devices,
push = push,
get = function() new_evaluation(output),
Expand Down
Loading