Possible to show console messages (written with `message`) in a shiny ui?

前端 未结 2 1195
感动是毒
感动是毒 2020-12-01 00:07

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:

2条回答
  •  天涯浪人
    2020-12-01 00:45

    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
    ") message("test message") cat("test cat2
    ") message("test message2") })) }), collapse="
    ") )}) }) )

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

提交回复
热议问题