Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion R/eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 11 additions & 12 deletions R/flush-console.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
6 changes: 3 additions & 3 deletions tests/testthat/test-flush-console.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down