Is there a way to show separate progress bars simultaneously for nested `purrr::map` calls?
Asked Answered
F

0

6

I have a function using nested map calls to process chunks (the inner map process a single chunk and the outer map runs through all chunks). Both processes are slow enough that it's useful to have progress bars on both.

Here's a reprex using Sys.sleep and .progress. I haven't included the output, since it's nonsense in static form. What happens is that the progress bar alternates between showing the internal one and the "Overall" one. I'm looking for a way to always show the "Overall" one and show the inner ones until they complete (so there would always be two lines of progress bars).

list_of_chunks <- list(
  c(1,2,3),
  c(4,5,6),
  c(7,8,9)
)
n_chunks <- length(list_of_chunks)

slow_process_per_item <- function(item) Sys.sleep(1)

process_per_chunk <- function(chunk_of_items, chunk_id) {
  chunk_of_items |> 
    purrr::walk(
      slow_process_per_item,
      .progress=stringr::str_glue("Chunk #{chunk_id} of {n_chunks}")
    )
}

list_of_chunks |> 
  purrr::iwalk(
    process_per_chunk,
    .progress="Overall"
  )

Created on 2023-06-06 with reprex v2.0.2

Figurine answered 7/6, 2023 at 2:10 Comment(1)
I fear that won't be possible. The progress bar is powered by cli and the documentation is rather clear: "[...]The current progress bar of a function is terminated when the function creates another progress bar or when the function returns, errors or is interrupted."Gallows

© 2022 - 2024 — McMap. All rights reserved.