用于 pblapply 函数的 R Shiny 进度条

标签 r progress-bar shiny lapply

有没有办法在 pblapply 函数中插入 Shiny 的进度函数(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))))
  })
})
)
)

最佳答案

不知道能不能从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以减慢循环速度。您可以使用 lapply如果您不需要在 R 控制台中查看进度。

关于用于 pblapply 函数的 R Shiny 进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31990447/

相关文章:

r - ggplot2 中 facet_wrap() 的手动中断

windows - 尝试使用Progressbar运行tkinter时Python崩溃

c - 使用线程实现进度控制

javascript - shinyBS - 拖动时删除工具提示

javascript - 使用 uiOutput 用 Shiny 的键盘触发 actionButton

r - 如何在使用 rredis 从 R 编写的 Redis 中实现数据压缩以减少内存使用?

r - 如何在 Rmarkdown HTML 中并排显示 2 个 flexdashboard 仪表?

r - lattice auto.key - 如何调整线和点?

android - 在另一个类的 ProgressBar 上设置进度

r - 带有 d3treeR 的交互式树形图未在 Shiny 中呈现