I have a main function which performs a handful of variously complicated (and long-running) computations on some data, it performs these steps using the pipe from tidyverse / magrittr. I would like a progress bar to report on the stage of the processing as it works through it, however, I'm at a loss. I've looked at the cli
, progress
and progressr
packages, and out of them I could only get cli
to work (in a manner of speaking.
Here's a minimal example:
library(tidyverse)
library(cli)
main_fun <- function() {
cli_progress_step(msg = "Running main function")
tibble(a = 1:5) %>%
fun1() %>%
fun2() %>%
fun3()
}
fun1 <- function(data) {
cli_progress_step(msg = "Doing sub function 1")
Sys.sleep(2)
return(data)
}
fun2 <- function(data) {
cli_progress_step(msg = "Doing sub function 2")
Sys.sleep(1)
return(data)
}
fun3 <- function(data) {
cli_progress_step(msg = "Doing sub function 3")
Sys.sleep(3)
return(data)
}
main_fun()
#> ℹ Running main function
#> ℹ Doing sub function 3
#> ℹ Doing sub function 2
#> ℹ Doing sub function 1
#> ✔ Doing sub function 1 [2s]
#>
#> ℹ Doing sub function 2✔ Doing sub function 2 [3s]
#>
#> ℹ Doing sub function 3✔ Doing sub function 3 [6.1s]
#>
#> ℹ Running main function✔ Running main function [6.1s]
#> # A tibble: 10 × 1
#> a
#> <int>
#> 1 1
#> 2 2
#> 3 3
#> 4 4
#> 5 5
This displays the progress bars but in 'reverse' order i.e. 3 then 2 then 1. Once it's all completed all are shown, which is about the only bit I'm happy with.