2

I made a function that is performing some complex calculations via a for loop. In order to show progress, the function will print out the current progress via something like message(...), and the final outcome of this function is a data frame.

But when I implement this in Shiny, the for loop counter is printed only in the R console rather than the Shiny document as intended. Is there a way to showing the outputs in the R console in real time during executions?

A very minimal example is here. Notice that in the Shiny interface, the counter is not present.

foo <- function() {
  ComplexResult = NULL # vector initiation 


  for(i in 1:5){   
    ComplexResult[i] = letters[i] 
  # At each stage of the for loop, we store some complex calculations
    message(paste0("For loop counter is on i = ", i)) 
  # This shows progress of the for loop, also other relevant messages if necessary.
   Sys.sleep(0.1) # Comment this out to remove pauses during execution.
 }

  return(as.data.frame(ComplexResult)) 
}



runApp(shinyApp(

  ui = fluidPage(
    dataTableOutput("VeryFinalOutcome")
  ),

  server = function(input,output, session) {
    fooOutcome = foo()
    output$VeryFinalOutcome = renderDataTable(fooOutcome) # This will only display the function output (i.e. the letters) in Shiny.
  }
))

My attempt: the capture.output(foo(),type="message") function did not help. Even though it captured the messages successfully, but it can only be displayed after all execution. And there is a extra issue of not being able to store the actual foo() outputs.

Thanks

Kevin
  • 333
  • 1
  • 3
  • 9
  • Try with special `shiny` function `withProgress`. Look here: http://shiny.rstudio.com/reference/shiny/latest/withProgress.html – Marta Jan 26 '16 at 08:39
  • Have a look here http://stackoverflow.com/questions/33049090/extend-time-progress-bar-displays-message/33097449#33097449 – Pork Chop Jan 26 '16 at 08:52
  • 1
    Hi, I have looked into that option before. Although it helps with the problem, but I am looking at a way of mirroring all of R console's outputs into Shiny's output. This is because the for loop is actually inside a function which itself is inside a package, and I believe it is not possible to insert the `incProgress(...)` function into the function script of the package... – Kevin Jan 26 '16 at 08:58
  • So maybe here: http://stackoverflow.com/questions/30474538/possible-to-show-console-messages-written-with-message-in-a-shiny-ui – Marta Jan 26 '16 at 09:16

0 Answers0