gpt4 book ai didi

用于 pblapply 函数的 R Shiny 进度条

转载 作者:行者123 更新时间:2023-12-04 09:34:43 24 4
gpt4 key购买 nike

有没有办法在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com