+++
title = "Progress bar in purrr::map_df"
slug = "progress-bar-in-purrr-map-df"
+++

I like the [purrr](https://purrr.tidyverse.org/) 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.

```r
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)
}
```
+++
title = "Progress bar in purrr::map_df"
slug = "progress-bar-in-purrr-map-df"
+++

I like the [purrr](https://purrr.tidyverse.org/) 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](https://cran.r-project.org/web/packages/progress/) package to be installed.

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

Clarify dependencies for purrr progress bar
Clarify dependencies for purrr progress bar