Skip to content

Commit

Permalink
Use progressr instead of dplyr for progress
Browse files Browse the repository at this point in the history
Resolves #184
Ref #192
  • Loading branch information
mitchelloharawild committed May 19, 2020
1 parent 7d95178 commit 8a16381
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Imports:
R6,
utils,
vctrs (>= 0.2.2),
distributional
distributional,
progressr
Suggests:
covr,
crayon,
Expand Down
18 changes: 9 additions & 9 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Check that specified model(s) are model definitions.", nm[which(!is_mdl)[1]]))
num_key <- n_keys(.data)
num_mdl <- length(models)
num_est <- num_mdl * num_key
p <- progressr::progressor(num_est)

kv <- key_vars(.data)
.data <- nest_keys(.data, "lst_data")
Expand All @@ -85,30 +86,29 @@ Check that specified model(s) are model definitions.", nm[which(!is_mdl)[1]]))
}
}

estimate_progress <- function(dt, mdl){
out <- estimate(dt, mdl)
p()
out
}

if(is_attached("package:future")){
require_package("future.apply")
eval_models <- function(models, lst_data){
out <- future.apply::future_mapply(
rep(lst_data, length(models)),
rep(models, each = length(lst_data)),
FUN = estimate,
FUN = estimate_progress,
SIMPLIFY = FALSE,
future.globals = FALSE
)
unname(split(out, rep(seq_len(num_mdl), each = num_key)))
}
}
else{
pb <- if(num_est > 1) dplyr::progress_estimated(num_est, min_time = 5) else NULL
eval_models <- function(models, lst_data){
map(models, function(model){
map(lst_data, function(dt, mdl){
out <- estimate(dt, mdl)
if(!is.null(pb)){
pb$tick()$print()
}
out
}, model)
map(lst_data, estimate_progress, model)
})
}
}
Expand Down

0 comments on commit 8a16381

Please sign in to comment.