Periodically taking out the cat's output to output R brilliant output (renderPrint)

Hope someone can help me.

Say there is an example function that is similar to

##function from a package example<-function(f){ #does something cat("step 1 done....") # etc etc cat("step 2 done....") return(some_data_frame) } ##server ui code example2<-reactive({ if(input$some_action_button==0) return() result<-isolate(example(input$f1)) return(result) }) output$f2<-renderPrint({ example2() }) 

Is there a way to periodically write "cat" output from a function in renderPrint? Assuming this is a long function to handle, and it would be nice if the user got some feedbabk. invalidateLater does not work for things that are already inside the function (at least it seems so when I tried it here).

In addition, as a secondary problem, writing code in the manner described above will cause renderPrint to capture both "cat" and data.frame together, possibly due to a "return".

If someone could point me in the right direction, that would be very helpful! Thanks!

+7
r shiny shinyjs
source share
1 answer

Firstly, the big question I was thinking a lot about this.

Since it’s brilliant single-threaded, it’s a bit complicated function to capture output and display it in brilliant form from what I know.

The work for this will be to use a non-blocking connection to the file and launch the function that you want to capture in the background while reading the file to display the function (check the history of changes to see how to do this).

Another way to do this would be to override the cat function to write to stderr (just switch cat with message ) and capture the output of the function as follows:

 library(shiny) library(shinyjs) myPeriodicFunction <- function(){ for(i in 1:5){ msg <- paste(sprintf("Step %d done.... \n",i)) cat(msg) Sys.sleep(1) } } # Override cat function cat <- message runApp(shinyApp( ui = fluidPage( shinyjs::useShinyjs(), actionButton("btn","Click me"), textOutput("text") ), server = function(input,output, session) { observeEvent(input$btn, { withCallingHandlers({ shinyjs::text("text", "") myPeriodicFunction() }, message = function(m) { shinyjs::text(id = "text", text = m$message, add = FALSE) }) }) } )) 

This example is mainly based on this daattali question.

+3
source share

All Articles