Skip to content

Instantly share code, notes, and snippets.

View wilsoncai1992's full-sized avatar

Wilson Cai wilsoncai1992

View GitHub Profile
@wilsoncai1992
wilsoncai1992 / withConsoleRedirect.R
Created June 29, 2016 17:10 — forked from jcheng5/withConsoleRedirect.R
Someone at #useR2016 asked me if you can have Shiny execute code in observers/reactives but send the console output to the browser.
library(shiny)
withConsoleRedirect <- function(containerId, expr) {
# Change type="output" to type="message" to catch stderr
# (messages, warnings, and errors) instead of stdout.
txt <- capture.output(results <- expr, type = "output")
if (length(txt) > 0) {
insertUI(paste0("#", containerId), where = "beforeEnd",
ui = paste0(txt, "\n", collapse = "")
)