sam*_*san 5 r lapply progress-bar shiny
有没有办法在 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))))
})
})
)
)
Run Code Online (Sandbox Code Playgroud)
我不知道你是否可以获得百分比,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);
})
Run Code Online (Sandbox Code Playgroud)
我添加了Sys.sleep来减慢循环速度。lapply如果您不需要在 R 控制台中查看进度,则可以使用。
| 归档时间: |
|
| 查看次数: |
1962 次 |
| 最近记录: |