-
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.
Merge commit '7f8d7d567200dbca472d28d1d264333ec576c625'
- Loading branch information
Showing
8 changed files
with
145 additions
and
83 deletions.
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#' An emulation of `flush.console()` in `evaluate()` | ||
#' | ||
#' @description | ||
#' 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. | ||
#' | ||
#' 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
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 |
---|---|---|
@@ -1 +1,6 @@ | ||
defer <- function(expr, frame = parent.frame(), after = FALSE) { | ||
thunk <- as.call(list(function() expr)) | ||
do.call(on.exit, list(thunk, TRUE, after), envir = frame) | ||
} | ||
|
||
`%||%` <- function(a, b) if (is.null(a)) b else a | ||
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
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
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,27 @@ | ||
|
||
|
||
test_that("flush_console() is a null op by default", { | ||
expect_no_error(flush_console()) | ||
}) | ||
|
||
test_that("can set and restore output handler", { | ||
f <- function() message("Hi") | ||
old <- set_output_handler(function() message("Hi")) | ||
expect_equal(.env$output_handler, f) | ||
expect_equal(old, NULL) | ||
|
||
expect_message(flush_console(), "Hi") | ||
old2 <- set_output_handler(old) | ||
expect_equal(old2, f) | ||
}) | ||
|
||
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") | ||
}) |