Search code examples
r

how to use cli progress bar to show progress in cleaning steps without for loop?


I'm confused how to use cli cli_progress_bar() and cli_progress_update() outside of the demo use case that they have in their documentation here (below is an example) https://cli.r-lib.org/articles/progress.html#the-traditional-progress-bar-api

If you wanted to show the time it takes to download a package do you first down load and record the total time, then use the progress_bar() and progress_update() to replicate the time using the for loop to show the actual time?

If I just had a script that went through a few cleaning steps (eg return a series of interim objects each with different cleaning steps done) how could I use progress bar to show how far along the cleaning steps the function is?

Below is an example of script that has some basic steps where I want to show the progress bar updating as each preparation step is completed without using a for loop.


library(tidyverse)

prepare_data <- function(){
 cli_progress_bar("Cleaning data", total = 3,clear = FALSE)

dat1 <- diamonds |> count(cut)

 cli_progress_update()
 
dat2 <- diamonds |> 
  group_by(cut) |> 
  summarise(
    mean_price=mean(price)
  )
 cli_progress_update()

dat3 <- left_join(
  dat1
  ,dat2
  ,by=join_by(cut)
)
 cli_progress_update()
 
 return(dat3)
}

prepare_data()

What I'm expecting to see is that after each cleaning step the progress bar updates like the in the example below

library(cli)

clean <- function() {
  cli_progress_bar("Cleaning data", total = 100)
  for (i in 1:100) {
    Sys.sleep(5/100)
    cli_progress_update()
  }
  cli_progress_done()
}

clean()

Solution

  • The docs for cli says that it won't show the progress bar at all unless the time exceeds 2 seconds. Your function probably finished too quickly. You can change that limit using options(cli.progress_show_after = 0) to always show it, or some other number (in seconds) for a different limit.

    You also missed the cli_progress_done() call at the end, but that probably isn't the issue here.