2

I am using foreach package inside my shiny app to run operations in parallel. I don't know how to make regular shiny progress bar work. Below an simple example that illustrates my problem:

library(shiny)
library(foreach)
library(doParallel)
library(DT)

app <- shinyApp(
    ui = fluidPage(
        DT::dataTableOutput("table")
    ),
    
    server = function(input, output) {
        
        output$table <- renderDataTable({
            
            withProgressShiny(message = "Calculation in progress",
                              detail = "This may take a while ...", value = 0, {
                output <- data.frame()
                cores.detected <- parallel::detectCores()
                cl <- parallel::makeCluster(cores.detected - 1)
                doParallel::registerDoParallel(cl)
                
                output <- foreach(i = 1:10000, .combine = 'rbind') %dopar% {
                    output[1,1] = i
                    output[1,2] = round(sqrt(i),2)
                    return(output)
                    
                    
                    incProgress(1/10000, detail = paste0("Calculation progress: ", round(i/10000*100, 0), "%"))
                    Sys.sleep(0.1)
                }
                parallel::stopCluster(cl)
            })
            
            DT::datatable(output)
        
        })
    }
)
            
runApp(app)
   

Outside of shiny I would use doSNOW package but as far as I know it is not working with shiny Any hints would be much appreciated

Mark Perez
  • 177
  • 7

0 Answers0