-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move flush_console() to its own file
* Make it more self-contained * Add a test
- Loading branch information
Showing
4 changed files
with
66 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#' An emulation of flush.console() in evaluate() | ||
#' | ||
#' When [evaluate()] is evaluating code, the text output is diverted into | ||
#' an internal connection, and there is no way to flush that connection. This | ||
#' function provides a way to "flush" the connection so that any text output can | ||
#' be immediately written out, and more importantly, the `text` handler | ||
#' (specified in the `output_handler` argument of `evaluate()`) will | ||
#' be called, which makes it possible for users to know it when the code | ||
#' produces text output using the handler. | ||
#' @note This function is supposed to be called inside `evaluate()` (e.g. | ||
#' either a direct `evaluate()` call or in \pkg{knitr} code chunks). | ||
#' @export | ||
flush_console = function() { | ||
if (!is.null(.env$output_handler)) { | ||
.env$output_handler() | ||
} | ||
invisible() | ||
} | ||
|
||
.env = new.env() | ||
.env$output_handler <- NULL | ||
|
||
set_output_handler <- function(handler) { | ||
old <- .env$output_handler | ||
.env$output_handler <- handler | ||
invisible(old) | ||
} | ||
|
||
local_output_handler <- function(handler, frame = parent.frame()) { | ||
old <- set_output_handler(handler) | ||
defer(set_output_handler(old), frame) | ||
invisible() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
defer <- function(expr, frame = parent.frame(), after = FALSE) { | ||
thunk <- as.call(list(function() expr)) | ||
do.call(on.exit, list(thunk, TRUE, after), envir = frame) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
test_that("flush_console() is a null op at top-level", { | ||
expect_no_error(flush_console()) | ||
|
||
}) | ||
|
||
test_that("can use flush_console() inside evaluate", { | ||
test <- function() { | ||
cat("hi") | ||
flush_console() | ||
cat("bye") | ||
} | ||
ev <- evaluate("test()") | ||
expect_equal(ev[[2]], "hi") | ||
expect_equal(ev[[3]], "bye") | ||
}) |