devtools::install_github("cszang/ppp")
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))