knitrProgressBar Package

Ever wanted a progress bar output visible in a knitr document? Now you can!

packages
R
developement
Author

Robert M Flight

Published

February 19, 2018

TL;DR

If you like dplyr progress bars, and wished you could use them everywhere, including from within Rmd documents, non-interactive shells, etc, then you should check out knitrProgressBar (cran github).

Why Yet Another Progress Bar??

I didn’t set out to create another progress bar package. But I really liked dplyrs style of progress bar, and how they worked under the hood (thanks to the examples from Bob Rudis).

As I used them, I noticed that no progress was displayed if you did rmarkdown::render() or knitr::knit(). That just didn’t seem right to me, as that means you get no progress indicator if you want to use caching facilities of knitr. So this package was born.

How??

These are pretty easy to setup and use.

library(knitrProgressBar)

# borrowed from example by @hrbrmstr
arduously_long_nchar <- function(input_var, .pb=NULL) {
  
  update_progress(.pb) # function from knitrProgressBar
  
  Sys.sleep(0.01)
  
  nchar(input_var)
  
}

# using stdout() here so progress is part of document
pb <- progress_estimated(26, progress_location = stdout())

purrr::map(letters, arduously_long_nchar, .pb = pb)

|======                                                | 12% ~0 s remaining     
|================                                      | 31% ~0 s remaining     
|===========================                           | 50% ~0 s remaining     
|=====================================                 | 69% ~0 s remaining     
|===============================================       | 88% ~0 s remaining     
Completed after 0 s                                                             
[[1]]
[1] 1

[[2]]
[1] 1

[[3]]
[1] 1

[[4]]
[1] 1

[[5]]
[1] 1

[[6]]
[1] 1

[[7]]
[1] 1

[[8]]
[1] 1

[[9]]
[1] 1

[[10]]
[1] 1

[[11]]
[1] 1

[[12]]
[1] 1

[[13]]
[1] 1

[[14]]
[1] 1

[[15]]
[1] 1

[[16]]
[1] 1

[[17]]
[1] 1

[[18]]
[1] 1

[[19]]
[1] 1

[[20]]
[1] 1

[[21]]
[1] 1

[[22]]
[1] 1

[[23]]
[1] 1

[[24]]
[1] 1

[[25]]
[1] 1

[[26]]
[1] 1

The main difference to dplyrs progress bars is that here you have the option to set where the progress gets written to, either automatically using the built-in make_kpb_output_decisions(), or directly. Also, I have provided the update_progress function to actually do the updating or finalizing properly.

There are also package specific options to control how the decisions are made.

See the main documentation, as well as the included vignette.

Multi-Processing

As of V1.1.0 (should be on CRAN soon), the package also supports indicating progress on multi-processed jobs. See the included vignette for more information.

By the way, I know this method is not ideal, but I could not get the combination of later and processx to work in my case. If anyone is willing to help out, that would be great.

Reuse

Citation

BibTeX citation:
@online{mflight2018,
  author = {Robert M Flight},
  title = {knitrProgressBar {Package}},
  date = {2018-02-19},
  url = {https://rmflight.github.io/posts/2018-02-19-knitrprogressbar},
  langid = {en}
}
For attribution, please cite this work as:
Robert M Flight. 2018. “knitrProgressBar Package.” February 19, 2018. https://rmflight.github.io/posts/2018-02-19-knitrprogressbar.