Skip to content

cszang/ppp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ppp: Progressbars that fit into a dplyr::mutate %>% purrr::map* pipeline

Installation

devtools::install_github("cszang/ppp")

Examples

A few packages...

library(rlang)
library(tidyr)
library(dplyr)
library(purrr)
library(ppp)

A slow function to be used with purrr::map*:

slow_mean <- function(x, .var) {
  Sys.sleep(1)
  .var <- rlang::enexpr(.var)
  mean(x[[.var]])
}

Setting up and updating progressbar is now part of the pipeline; it's default name is .pb. We decorate our function slow_mean to make it update the progressbar:

mtcars %>%
  dplyr::group_by(carb) %>%
  tidyr::nest() %>%
  spawn_pb() %>%
  dplyr::mutate(mean_qsec = purrr::map_dbl(data, decorate_pb(slow_mean), qsec))

Since we can name the progressbars, we can have multiple sequential progress bars, like so:

mtcars %>%
  dplyr::group_by(carb) %>%
  tidyr::nest() %>%
  spawn_pb(.pb_qsec) %>%
  spawn_pb(.pb_disp) %>%
  dplyr::mutate(mean_qsec = purrr::map_dbl(data, decorate_pb(slow_mean, .pb_qsec), qsec),
                mean_disp = purrr::map_dbl(data, decorate_pb(slow_mean, .pb_disp), disp))

Or one progress bar for all computations by knowing the number of calls to map* in advance, like so:

mtcars %>%
  dplyr::group_by(carb) %>%
  tidyr::nest() %>%
  spawn_pb(.times = 2) %>%
  dplyr::mutate(mean_qsec = purrr::map_dbl(data, decorate_pb(slow_mean), qsec),
                mean_disp = purrr::map_dbl(data, decorate_pb(slow_mean), disp))

About

An idea for a progress bar that fits into a dplyr::mutate %>% purrr::map* pipeline.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages