Day 02: cli_progress_*()
Show progress in your code using the cli_progress_*()
family of functions from the cli package.
The cli package (Csárdi 2022) provides helpers for developing attractive command line interfaces and output. It has lots of great features worth checking out, but today we’re going to talk about one of my favorites: progress bars!
This post is just a quick highlight reel, check out cli’s progress-bar vignettes for more details:
Progress bar basics
To lift straight from the progress-bar intro vignette, adding a progress bar is a three-step/three-function process:
Call
cli_progress_bar()
to create a progress bar.Call
cli_progress_update()
to update it.Call
cli_progress_done()
to terminate it.
We’ll use these three steps in our pretend cleaning function, below:
We created our progress bar with cli_progress_bar()
on line 2, told it how often to update (after each iteration) with cli_progress_update()
on line 5, and when to finish with cli_progress_done()
on line 7.
If we were to run that function, we’d get the following output.
clean()
Unknown total
But what if I don’t know how many units total? Don’t worry, there’s still a way to show your progress with an unknown total number of units. You can omit the total number from the cli_progress_bar()
function, and use force = TRUE
inside of cli_progress_update()
, as seen on line 8 of the code below.
Naturally, the output will look different, since you don’t have a known total on which to base your progress percentage.
Progress without bars
Want to know how your code is going but don’t want or need a progress bar along the way? Check out the cli_progress_message()
and cli_progress_step()
functions.
cli_progress_step()
makes use of cli feaatures, such as cli_alert()
themes to show colored icons indicating information, success, and danger, and will show you the duration of each step after it’s completed.
Learn more
This is but a paltry sampling of what you can do with the cli_progress_*()
functions, so be sure to take the time to check out cli documentation!