diff --git a/R/eval.R b/R/eval.R index 0ffe530..cb204de 100644 --- a/R/eval.R +++ b/R/eval.R @@ -143,7 +143,7 @@ evaluate_top_level_expression <- function(exprs, if (!is.null(source)) watcher$push(source) - local_output_handler(watcher$capture_output) + local_console_flusher(watcher$capture_output) local_plot_hooks(watcher$capture_plot_and_output) # Handlers for warnings, errors and messages diff --git a/R/flush-console.R b/R/flush-console.R index cdf8931..2ca3d01 100644 --- a/R/flush-console.R +++ b/R/flush-console.R @@ -13,23 +13,22 @@ #' either a direct `evaluate()` call or in \pkg{knitr} code chunks). #' @export flush_console = function() { - if (!is.null(the$output_handler)) { - the$output_handler() + if (!is.null(the$console_flusher)) { + the$console_flusher() } invisible() } +the$console_flusher <- NULL -the$output_handler <- NULL +local_console_flusher <- function(flusher, frame = parent.frame()) { + old <- set_console_flusher(flusher) + defer(set_console_flusher(old), frame) + invisible() +} -set_output_handler <- function(handler) { - old <- the$output_handler - the$output_handler <- handler +set_console_flusher <- function(flusher) { + old <- the$console_flusher + the$console_flusher <- flusher invisible(old) } - -local_output_handler <- function(handler, frame = parent.frame()) { - old <- set_output_handler(handler) - defer(set_output_handler(old), frame) - invisible() -} diff --git a/tests/testthat/test-flush-console.R b/tests/testthat/test-flush-console.R index 849af35..efa6486 100644 --- a/tests/testthat/test-flush-console.R +++ b/tests/testthat/test-flush-console.R @@ -6,12 +6,12 @@ test_that("flush_console() is a null op by default", { test_that("can set and restore output handler", { f <- function() message("Hi") - old <- set_output_handler(function() message("Hi")) - expect_equal(the$output_handler, f) + old <- set_console_flusher(function() message("Hi")) + expect_equal(the$console_flusher, f) expect_equal(old, NULL) expect_message(flush_console(), "Hi") - old2 <- set_output_handler(old) + old2 <- set_console_flusher(old) expect_equal(old2, f) })