Search code examples
rtextshinyrenderobservers

Shiny - How to tell client when a process is done


I'm completely new to Shiny and I can't get a textOutput to render when I want it to.

observeEvent(input$btnPersistencia, {
  output$txtProtActual <- renderText("PROCESSING...")
  print("This does print in console")

  #SomeCodeHere that takes about 10 seconds to finish

  output$txtProtActual <- renderText(paste("Archivo Persistencia Actual: ", basename(values$file), "\n Dim: ", isolate(input$sliderDimension), "\n Filtr: ", isolate(input$txtMaxFil)))
})

The text isn't showing "Processing..." while #SomeCodeHere is running. I really don't understand why. Shouldn't it work?

The text is only rendered AFTER the observeEvent is finished. I know this because if I remove the SECOND renderText(), the text takes the value "Processing..." when the processing is over.

If this is the normal behaviour, is there a way to force the render before the observeEvent is over?

EDIT:

Is there another (any) way to achieve what I want?


Solution

  • Posting my comment as an answer (thanks!)

    The article about Progress Bars is here and the reference here. Here's your code with the progress bar:

    observeEvent(input$btnPersistencia, {
       withProgress(message = 'PROCESSING...', value = 0, {
           incProgress(1/2)
           #SomeCodeHere that takes about 10 seconds to finish
           Sys.sleep(10)
       })
    
       output$txtProtActual <- renderText({
         paste("Archivo Persistencia Actual: ", basename(values$file),
           "\n Dim: ", isolate(input$sliderDimension),
           "\n Filtr: ", isolate(input$txtMaxFil)
         )
       })
     })
    

    Although it's not related to your question, I've noticed you've placed an output inside an observeEvent with some isolate wrapping inputs.

    One of the shiny developers talks about observers in the first two videos of shiny's 2016 conference. It helped me understand a lot better how to use observers and I thought you might find it useful. :]