我有一个Rmarkdown文档,其中包含许多代码块。每个代码块都在循环内运行模拟。模拟的持续时间从几分钟到20分钟左右不等。我想要跟踪循环何时结束。然而,在代码块运行时,渲染面板中通常的输出不会改变。目前,当循环运行完成时,我会将一些信息写入文本文件,但这似乎有点不太正规。是否有更优雅的方法来实现此功能?
我有一个Rmarkdown文档,其中包含许多代码块。每个代码块都在循环内运行模拟。模拟的持续时间从几分钟到20分钟左右不等。我想要跟踪循环何时结束。然而,在代码块运行时,渲染面板中通常的输出不会改变。目前,当循环运行完成时,我会将一些信息写入文本文件,但这似乎有点不太正规。是否有更优雅的方法来实现此功能?
library(knitrProgressBar)
slow_function <- function(i, .pb=NULL) {
update_progress(.pb)
Sys.sleep(0.5)
i
}
# create an R6 progress object for
# the number of loops/iterations in the target chunk
n_iterations <- 20
pb <- progress_estimated(n_iterations)
#class(pb)
#[1] "Progress" "R6"
purrr::map_int(1:n_iterations, ~slow_function(.x, .pb = pb))
编辑:更接近问题 - 这也适用于循环,不仅适用于purrr::map。pb <- progress_estimated(n_iterations)
for(i in 1:n_iterations) {
slow_function(i, .pb=pb)
}
这正是我正在寻找的。谢谢
沃尔夫冈