R闪亮的进度条用于pblapply功能

时间:2015-08-13 14:03:26

标签: r progress-bar shiny lapply

是否有办法在pblapply函数中插入闪亮的进度函数(incProgress),以便在右上角显示百分比的进度。每次lapply函数再次启动时,都需要评估IncProgress(...),就像在基于文本的进度条中更新控制台一样。

这是我的测试示例:

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))))
  })
})
)
)

1 个答案:

答案 0 :(得分:3)

我不知道您是否可以从pblapply获得百分比,但withProgress中的您可以这样做:

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); 
})

我添加了Sys.sleep以减慢循环速度。如果您不需要在R控制台中查看进度,则可以使用lapply