Capture progress from foreach() %dopar% in a Shiny App


#1

DeaR useRs,

I am trying to utilize foreach() %dopar% in shiny and would like to display the iteration and information attached to the iteration. I saw examples to progressbar but those were not so reproducible for Shiny app.

I was using withProgress() & incProgress() and wanted to keep that setup and incorporate foreach() %dopar%. . My working code that outputs the status of iteration is something like this:

getData <- data.frame()
data <- eventReactive(input$buttonclick,{

  withProgress(message = 'Extracting Data...', style = 'notification', value = 0,{
     for(i in 1:length(input$a)){
       mydata<- getData(input$a[i], input$b)
       getData <- rbind(getData, mydata)
       incProgress(1/length(input$a), 
                   detail = paste(round(i/length(input$a)*100,2),"% Done",sep = ""))
     }
  })
})

Below example outputs the task number, so on the similar lines I would like to capture task number and display it in above working code. I am able to incorporate the foreach() in above example.

 library(doSNOW)
 library(tcltk)
 library(randomForest)
 cl <- makeSOCKcluster(3)
 registerDoSNOW(cl)
 
 ntasks <- 100
 pb <- tkProgressBar(max=ntasks)
 progress <- function(n) cat(sprintf("task %d is complete\n", n))
 opts <- list(progress=progress)
 
 x <- matrix(runif(500), 100)
 y <- gl(2, 50)
 
 rf <- foreach(ntree=rep(25, ntasks), .combine=combine,
               .multicombine=TRUE, .packages='randomForest',
               .options.snow=opts) %dopar% {
                   randomForest(x, y, ntree=ntree)
               }

Thx,
Tarun