用于 pblapply 函数的 R 闪亮进度条
R shiny progress bar for pblapply functions
有没有办法在 pblapply 函数中插入闪亮的进度函数 (incProgress),以便在右上角以百分比显示进度。 IncProgress(...) 需要在每次 lapply 函数再次启动时进行评估,就像在基于文本的进度条中更新控制台一样。
这是我的测试示例:
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
来减慢循环速度。如果不需要在 R 控制台中查看进度,可以使用 lapply
。
有没有办法在 pblapply 函数中插入闪亮的进度函数 (incProgress),以便在右上角以百分比显示进度。 IncProgress(...) 需要在每次 lapply 函数再次启动时进行评估,就像在基于文本的进度条中更新控制台一样。
这是我的测试示例:
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
来减慢循环速度。如果不需要在 R 控制台中查看进度,可以使用 lapply
。