如何在Rmarkdown中运行代码块时获取进度信息?

3

我有一个Rmarkdown文档,其中包含许多代码块。每个代码块都在循环内运行模拟。模拟的持续时间从几分钟到20分钟左右不等。我想要跟踪循环何时结束。然而,在代码块运行时,渲染面板中通常的输出不会改变。目前,当循环运行完成时,我会将一些信息写入文本文件,但这似乎有点不太正规。是否有更优雅的方法来实现此功能?

2个回答

2
原来有一个专门用于这个的包:knitrProgressBar
根据该包的文档,以下是一个最简示例的用法:
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"  

执行使用进度对象的代码块(或编织Rmd文件),将为该代码块生成进度更新。
purrr::map_int(1:n_iterations, ~slow_function(.x, .pb = pb))

enter image description here

编辑:更接近问题 - 这也适用于循环,不仅适用于purrr::map。
pb <- progress_estimated(n_iterations)
for(i in 1:n_iterations) {
  slow_function(i, .pb=pb)
}

enter image description here


0

这正是我正在寻找的。谢谢

沃尔夫冈


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接