Possible to show console messages (written with `message`) in a shiny ui?
Solution 1
Yihui suggested I use withCallingHandlers
, and that indeed let me to a solution. I wasn't quite sure how to use that function in a way that would do exactly what I needed because my problem was that I had a function that printed out several messages one at a time and using a naive approach only printed the last message. Here is the my first attempt (which works if you only have one message to show):
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)
)
})
}
))
Notice how only two\n
gets outputted. So my final solution was to use the html
function from shinyjs
package (disclaimer: I wrote that package), which lets me change or append to the HTML inside an element. It worked perfectly - now both messages got printed out in real-time.
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)
})
})
}
))
Solution 2
I know this isn't nearly as elegant, but I worked around a bit similar problem using capture.output
; sadly sink
doesn't allow simultaneous capture of messages and output though. You don't get them in the original order, but you can extract both streams at least (here turned to HTML):
runApp(shinyApp(
ui = fluidPage(
uiOutput("test")
),
server = function(input,output, session) {
output$test <- renderUI({
HTML(
paste(capture.output(type = "message", expr = {
message(capture.output(type = "output", expr = {
cat("test cat<br>")
message("test message")
cat("test cat2<br>")
message("test message2")
}))
}), collapse="<br>")
)})
})
)
Output:
test message
test message2
test cat
test cat2
Perhaps in the case if user wants to capture both but also separate them, this will provide a handy work-around. (Your shinyjs
package seems neat, need to take a look at it!)
DeanAttali
R-Shiny consultant with a MSc in Bioinformatics and a Bachelor of Computer Science. Work experience as a software engineer and web developer. Life experience as a restless traveller with too many homes. Founder of AttaliTech Ltd. Author of the R packages shinyjs, timevis, ggExtra, ezknitr, and more. Creator of "Use Cases in Shiny" course - an interactive, online video course. See my projects page for more info or my shiny server for some of my R-Shiny apps.
Updated on July 18, 2022Comments
-
DeanAttali almost 2 years
I don't understand R's message vs cat vs print vs etc. too deeply, but I'm wondering if it's possible to capture messages and show them in a shiny app?
Example: the following app can capture cat statements (and print statements as well) but not message statements
runApp(shinyApp( ui = fluidPage( textOutput("test") ), server = function(input,output, session) { output$test <- renderPrint({ cat("test cat") message("test message") }) } ))
Cross post from the shiny-discuss Google group since I got 0 answers.
-
DeanAttali over 7 yearsThe problem with this approach is that all the output gets printed at the end, it doesn't come in live. So if you run a slow function that prints output as it runs, you won't see it until it's done
-
geotheory about 7 yearsI tip my hat to you, sir.
-
Fábio over 6 yearsThank you very much. I've had a function that printed message() text. With your solution I can edit the
shinyjs::html(id = "text", html...
line using HTML tags. -
kennyB about 6 yearsThis currently outputs the messages without line returns in between. Is this feasible to do? How would one do it?
-
alvaropr over 5 years@kennyB Did you manage to find a way to inset a line return so messages are displayed in different lines? Thanks!
-
knapply about 5 yearsThis doesn't seem to work. Has anyone used this recently?
-
knapply about 5 years@DeanAttali Something’s wacky on my end then. Thank you for checking!
-
quickreaction about 4 yearsI'm trying to use this in a module framework... doesn't look like the namespace
ns()
is working properly with this solution...? -
DeanAttali about 4 yearsThat's possible, I haven't tried. Though to be honest, I've yet to see many things that break inside modules, usually it's a user error.
-
jbaums almost 4 yearsTo add a line break after each message you can adjust to
shinyjs::html(id = "text", html = paste0(m$message, '<br>'), add = TRUE)
-
panman almost 4 years@DeanAttali: This is very impressive, thank you very much. Is there a way, thou, to show the messages only in the Shiny UI, but not in the R console? That is, fully redirecting them to the UI.
-
DeanAttali almost 4 yearsIs there a way? Probably :) Is it going to be annoying to play around with code until you find it? Also probably :) I think that would be a different solution than what I came up with here. Look into the
sink()
function and perhaps have a sort of timer that checks on any new messages in a regular interval, that would be the first thing I'd look into. -
lz100 over 3 years@DeanAttali, Is it possible to capture
cat
andprint
to UI in real-time? If someone's function use cat or print,withCallingHandlers
ortryCatch
is not working. -
Jeff Parker about 3 yearsIt seems this only works with
observeEvent
. Am I missing something for this to work oneventReactive
? -
Tarun Parmar about 3 yearsI am using sink() with reactiveFileReader() for the generated log file of console output and then using renderPrint() to display output by verbatimTextOutput() but it is not live. Couldn't find an good examples that can do the live line by line display of console to Shiny. @DeanAttali if you have any example of this that you could share, I would really appreciate that.