Progress bar in purrr::map_df

Update 15 April 2023: The map functions in purrr now have built-in progress bars so this is unnecessary. Just pass .progress = TRUE for a simple unnamed progress bar, or pass a string or a list of parameters for more customisation.


I like the 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 purrr's map_df function but with an added progress bar. It requires the progress package to be installed.

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)
}