I like dplyr\'s \"progress_estimated\" function but I can\'t figure out how to get a progress bar to work inside a dplyr chain. I\'ve put a reproducible example with code at the
I dont really like my solution but it works.
print_tick_function <- function(x, p) {
p$tick()$print()
data.frame(x)
}
SunriseSet <- function(dataframe, timezone){
p <- progress_estimated(nrow(dataframe))
dataframe %>%
rowwise() %>%
do(print_tick_function(.,p)) %>%
mutate(
datetime = as.POSIXct(substr(cdatetime, 1, 20), tz = timezone),
sunrise = sunrise.set(latitude, longitude, datetime, timezone, num.days = 1)[1,1]
)
}
test2 <- SunriseSet(test, "CST6CDT")
Rather than using rowwise()
, perhaps try pairing the map*
functions from purrr
with progress_estimated()
. This answer follows the approach from https://rud.is/b/2017/03/27/all-in-on-r%E2%81%B4-progress-bars-on-first-post/.
First, wrap your function in another function that updates the progress bar:
SunriseSet <- function(lat, long, date, timezone, num.days, .pb = NULL) {
if (.pb$i < .pb$n) .pb$tick()$print()
sunrise.set(lat, long, date, timezone, num.days)
}
Then, iterate through your inputs with pmap
, or pmap_df
(to bind the outputs into a dataframe):
library(purrr)
pb <- progress_estimated(nrow(test), 0)
test2 <- test %>%
mutate(
sunrise = pmap_df(
list(
lat = latitude,
long = longitude,
date = as.character(cdatetime)
),
SunriseSet,
timezone = "CST6CDT", num.days = 1, .pb = pb
)$sunrise
)