Progress bar in purrr::map_df

I like purrr collection of functions rather than the base R equivalents. But when working with big datasets, it is helpful to have some indication of how long an operation is going to take.

This simple function creates a version of the purrr’s map_df function but with an added progress bar.

map_df_progress <- function(.x, .f, ..., .id = NULL) {
  .f <- purrr::as_mapper(.f, ...)
  pb <- progress::progress_bar$new(total = length(.x), force = TRUE)
  
  f <- function(...) {
    pb$tick()
    .f(...)
  }
  purrr::map_df(.x, f, ..., .id = .id)
}