(自从我得到0个答案后,来自闪亮讨论Google小组的交叉帖子).
我不太了解R的消息vs cat vs print vs等,但我想知道是否有可能捕获消息以及他们是谁在一个闪亮的应用程序?
示例:以下应用程序可以捕获cat语句(以及print语句)但不捕获消息语句
runApp(shinyApp(
ui = fluidPage(
textOutput("test")
),
server = function(input,output, session) {
output$test <- renderPrint({
cat("test cat")
message("test message")
})
}
))
Run Code Online (Sandbox Code Playgroud)
链接到原始问题:https://groups.google.com/forum/#!topic/shiny-discuss/UCacIquFJQY
谢谢
Dea*_*ali 37
易辉建议我使用withCallingHandlers,这确实让我找到了解决方案.我不太确定如何以一种完全符合我需要的方式使用该功能,因为我的问题是我有一个函数可以一次打印几条消息并使用简单的方法只打印最后一条消息.这是我的第一次尝试(如果您只显示一条消息,则可以使用):
foo <- function() {
message("one")
message("two")
}
runApp(shinyApp(
ui = fluidPage(
actionButton("btn","Click me"),
textOutput("text")
),
server = function(input,output, session) {
observeEvent(input$btn, {
withCallingHandlers(
foo(),
message = function(m) output$text <- renderPrint(m$message)
)
})
}
))
Run Code Online (Sandbox Code Playgroud)
注意如何two\n输出.所以我的最终解决方案是使用包中的html函数shinyjs(免责声明:我编写了该包),它允许我更改或附加到元素内的HTML.它工作得很好 - 现在这两条消息都是实时打印出来的.
foo <- function() {
message("one")
Sys.sleep(0.5)
message("two")
}
runApp(shinyApp(
ui = fluidPage(
shinyjs::useShinyjs(),
actionButton("btn","Click me"),
textOutput("text")
),
server = function(input,output, session) {
observeEvent(input$btn, {
withCallingHandlers({
shinyjs::html("text", "")
foo()
},
message = function(m) {
shinyjs::html(id = "text", html = m$message, add = TRUE)
})
})
}
))
Run Code Online (Sandbox Code Playgroud)