diff --git a/R/forecast.R b/R/forecast.R index ae10226d..478d61c4 100644 --- a/R/forecast.R +++ b/R/forecast.R @@ -123,9 +123,12 @@ forecast.mdl_df <- function(object, new_data = NULL, h = NULL, #' @export forecast.lst_mdl <- function(object, new_data = NULL, key_data, ...){ - map2(object, - new_data %||% rep(list(NULL), length.out = length(object)), - forecast, ...) + mapply_maybe_parallel( + .f = forecast, + object, + new_data %||% rep(list(NULL), length.out = length(object)), + MoreArgs = dots_list(...) + ) } #' @rdname forecast diff --git a/R/utils.R b/R/utils.R index aabdc125..801f980b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -241,4 +241,26 @@ flatten_with_names <- function (x, sep = "_") { if(!is.null(names(x))) set_names(x, paste(nm, names(x), sep = sep)) else x }) flatten(unname(map(x, flatten_with_names, sep = sep))) +} + +mapply_maybe_parallel <- function (.f, ..., MoreArgs = list(), SIMPLIFY = FALSE) { + if(is_attached("package:future")){ + require_package("future.apply") + + future.apply::future_mapply( + FUN = .f, + ..., + MoreArgs = MoreArgs, + SIMPLIFY = SIMPLIFY, + future.globals = FALSE + ) + } + else{ + mapply( + FUN = .f, + ..., + MoreArgs = static_args, + SIMPLIFY = SIMPLIFY + ) + } } \ No newline at end of file