Waiting for a future to complete in Shiny in combination with progressbar

430 views Asked by At

I have a very long task in R that does a lot of computations and writes out several files. This function is called from within a Shiny-reactive. Since it takes so much time I wanted to parallelize it on several processes but at the same time I wanted to include a progress-bar for the end user. I closely followed the answer to this post that uses futures: Using standard R shiny progress bar in parallel foreach calculations .

However: The files that my function creates are later used to do some further processing in another reactive. Somehow I am unable to let this second reactive wait until my future has fully completed - it already tries to read files that either are not yet present or incomplete. How do I tell the second reactive to wait until the future is complete?

My code so far:

library(promises)
library(future)
library(ipc)

plan(multisession)

r1 <- eventReactive(input$go_Button, {
  Runs <- 1:3  #3 parallel runs
  prog <- list()   #Setup for Progressbar
  
  for (j in Runs) {
    prog[[j]] <- AsyncProgress$new(message = "Calculation")
    future_promise({
      for (k in 1:length(rawdata_entries)) {
        prog[[j]]$inc(1 / length(rawdata_entries))
        
        <<< VERY LONG TASK THAT CREATES SEVERAL FILES >>>
          
      }
      prog[[j]]$close()
    }) # end of future_promise
  } # end of for loop
  
  return(list("Done"=TRUE))   
  
}) # end of eventReactive



r2 <- reactive({
  if (r1()$Done == TRUE) {  #my naive approach that doesn't work 
    <<<do some further processing with the files r1() created >>>
    <<< creates the data-frame "some_results" >>>    
  
      return(some_result)
  }
})

The most important things I want to achieve are:

  • parallelise the time consuming calculations
  • have a progressbar for the end user
  • fully evaluate the promise before moving on with further steps

Where is / are my mistake(s)?

0

There are 0 answers