progressr: Render 'knitr' Documents
Henrik Bengtsson
Source:vignettes/progressr-31-knitr.md
progressr-31-knitr.Rmd
When compiling (“knitting”) an knitr-based vignette, for instance,
via knitr::knit()
, knitr shows
the progress of code chunks processed thus far using a progress bar. We
can use progressr also for
this type of progress reporting. To have knitr report
on progress via progressr, set R option
knitr.progress.fun
as:
options(knitr.progress.fun = function(total, labels) {
p <- progressr::progressor(total, on_exit = FALSE)
list(
update = function(i) p(sprintf("chunk: %s", labels[i])),
done = function() p(type = "finish")
)
})
That’s it! With the above knitr.progress.fun
option set,
try to knit a document, e.g.
progressr::handlers(global = TRUE)
infile <- system.file("examples", "knitr-minimal.Rnw", package = "knitr")
outfile <- knitr::knit(infile)
To customize the progress bar, try for instance:
progressr::handlers(global = TRUE)
options(crayon.enabled = TRUE) ## force use of colors
progressr::handlers("pbcol")
This will produce a colored progress bar in the terminal.
Comment: If the above knit example completes too quickly for you to see the progress updates, you can add the “slowdown” handler to slow down the knitting process, e.g.
This will add the overall process to take at least ten seconds (default).
To make knitr report via progressr
in all your R session, set the above R option in your
~/.Rprofile
startup file, e.g.
if (requireNamespace("progressr", quietly = TRUE)) {
options(knitr.progress.fun = function(total, labels) {
p <- progressr::progressor(total, on_exit = FALSE)
list(
update = function(i) p(sprintf("chunk: %s", labels[i])),
done = function() p(type = "finish")
)
})
}