Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny progress bar for pblapply functions

Is there a way to insert shiny progress function (incProgress) inside the pblapply function so that it would show the progress in percentages in the upper right corner. IncProgress(...) needs to be evaluated every time the lapply function starts again, just like in the text based progress bar the console is updated.

Here is my test example:

runApp(list(
 ui = shinyUI(
  fluidPage(
  actionButton("calc","Start calculation"),
  dataTableOutput("out")
 )
),
 server = shinyServer(function(session, input, output) {
 library(pbapply)

  #create list
  n <- 100; nn <- 1000
  g <- factor(round(n * runif(n * nn)))
  x <- rnorm(n * nn) + sqrt(as.numeric(g))
  xg <- split(x, g)

  observeEvent(input$calc > 0,{
   withProgress(message = "Initializing data manipulation process", value=0, {
     list = pblapply(xg,mean)
     #insert 'incProgress(percentage, detail = paste0("Progress: ",percentage)' in the pbapply or txtProgress function
  })
   output$out = renderDataTable(datatable(as.data.frame(unlist(list))))
  })
})
)
)
like image 533
samssan Avatar asked Aug 13 '15 14:08

samssan


1 Answers

I don't know if you can get the percentage from pblapply but you in the withProgress could do:

percentage <- 0
list = pblapply(xg,function(x) {
          Sys.sleep(0.05); 
          percentage <<- percentage + 1/length(xg)*100
          incProgress(1/length(xg), detail = paste0("Progress: ",round(percentage,2)))
          mean(x); 
})

I added the Sys.sleep to slow the loop down. You can use lapply if you don't need to see the progress in the R console.

like image 164
NicE Avatar answered Oct 03 '22 12:10

NicE