43

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.

2
  • 6
    I guess you can use withCallingHandlers() to capture messages in an R expression, then print/cat them.
    – Yihui Xie
    May 27, 2015 at 17:54
  • 1
    Thanks Yihui , I was able to use that, that's a huge help
    – DeanAttali
    May 27, 2015 at 18:31

2 Answers 2

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

49

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)
      })
    })
  }
))
14
  • 1
    I tip my hat to you, sir.
    – geotheory
    May 19, 2017 at 23:28
  • Thank 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.
    – Fábio
    Feb 24, 2018 at 4:39
  • 1
    To add a line break after each message you can adjust to shinyjs::html(id = "text", html = paste0(m$message, '<br>'), add = TRUE)
    – jbaums
    Jul 25, 2020 at 9:17
  • 4
    @DeanAttali, Is it possible to capture cat and print to UI in real-time? If someone's function use cat or print, withCallingHandlers or tryCatch is not working.
    – lz100
    Oct 28, 2020 at 5:13
  • 1
    It seems this only works with observeEvent. Am I missing something for this to work on eventReactive? Mar 4, 2021 at 18:30
3

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!)

1
  • 3
    The 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
    – DeanAttali
    Nov 21, 2016 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.