From 8a163810eecee955c7807b85e13ea7aba959a8f4 Mon Sep 17 00:00:00 2001 From: Mitchell Date: Tue, 19 May 2020 18:17:51 +1000 Subject: [PATCH] Use progressr instead of dplyr for progress Resolves #184 Ref #192 --- DESCRIPTION | 3 ++- R/model.R | 18 +++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4e4a6b7a..6afd6157 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,7 +34,8 @@ Imports: R6, utils, vctrs (>= 0.2.2), - distributional + distributional, + progressr Suggests: covr, crayon, diff --git a/R/model.R b/R/model.R index ad510a4e..73685cf8 100644 --- a/R/model.R +++ b/R/model.R @@ -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") @@ -85,13 +86,19 @@ 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 ) @@ -99,16 +106,9 @@ Check that specified model(s) are model definitions.", nm[which(!is_mdl)[1]])) } } 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) }) } }