我偶然发现了this:
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 = "")
)
}
results
}
# Example usage
ui <- fluidPage(
pre(id = "console")
)
server <- function(input, output, session) {
observe({
invalidateLater(1000)
withConsoleRedirect("console", {
str(cars)
})
})
}
shinyApp(ui, server)
R Shiny中类似控制台的输出的解决方案,但是如果控制台太大,它将扩大整个应用程序窗口,而不是在其侧面添加滑块。我还想使控制台本身和文本具有不同的颜色。我应该如何处理?甚至可行吗?