rcommand-line-interfacepurrr

How to combine CLI message with purrr family functions


I am wondering how to convert this example code to purrr style by using the walk() function instead?

library(cli)

n <- 10
cli_alert_info("About to start downloads of {n} file{?s}")

i <- 0
cli_progress_step("Got {i}/{n} {qty(i)}file{?s}.")

for (i in seq_len(n)) {
  Sys.sleep(0.5)
  cli_progress_update()
}

Any comments and suggestions are greatly appreciated.


Solution

  • You could use the .progress argument. See Progress bars in purrr for more details.

    library(cli)
    
    n <- 10
    cli_alert_info("About to start downloads of {n} file{?s}")
    
    purrr::walk(seq_len(n), ~ {
      Sys.sleep(0.5)
    }, .progress = list(type = "iterator",
                        format = "Got {pb_current}/{pb_total} {qty(pb_current)}file{?s}.",
                        clear = FALSE))
    
    # ℹ About to start downloads of 10 files
    # Got 10/10 files.