HenrikBengtsson / doFuture

:rocket: R package: doFuture - Use Foreach to Parallelize via Future Framework
https://doFuture.futureverse.org
84 stars 6 forks source link

Text progressbar for parallel foreach loops in R with OS windows #66

Closed Hallo951 closed 2 years ago

Hallo951 commented 2 years ago

Is it possible to update a text progressbar with the function option.future like it was possible with doPar with the snow package?

Here is an example how I had it so far. I would like to do this again:

library(utils) # for textProgressBar
library(foreach)
library(doParallel)
library(doSNOW)

# Progressbar
opts <- list(progress = function(f){setTxtProgressBar(pb, f)})

# initial Progressbar
pb <- txtProgressBar(max = length(data), style = 3)

result <- foreach(a = data, .options.snow = opts, .combine = rbind) %dopar% {
  do anything
}

I tried the same construct with doFuture and the function option.future in the foreach loop but unfortunately this does not work. The use of packages like progressr or progress caused me significant instabilities (Rstudio crashed several times) so I can't use them.

If there is another stable option, can you give me a short minimal example of it? Thanks :)

Edit 1: Oh, I just see that the package "progressr" is also from them. I have tried this but R (R-Studio) crashes regularly when I use this package. Without this everything works without errors. That is why my question about the other solution.

I tried the following with the progessr package:

library(doRNG)
library(doFuture)
registerDoFuture()      ## %dopar% parallelizes via future
plan(multisession)

library(progressr)
handlers(global = TRUE)
handlers("progress", "beepr")

with_progress({
  p <- progressor(step= length(numVector))
  y <- foreach(x = numVector) %dorng% {
    Sys.sleep(6.0-x)
    p()
    sqrt(x)
  }
})

Unfortunately, it was always unstable for me.

Here my session:

R version 4.0.5 (2021-03-31) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows Server x64 (build 17763)

Matrix products: default

locale: [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252 LC_NUMERIC=C LC_TIME=German_Germany.1252

attached base packages: [1] parallel grid stats graphics grDevices utils datasets methods base

other attached packages: [1] doSNOW_1.0.19 snow_0.4-4 dplyr_1.0.7 spdep_1.2-1 sf_1.0-5 spData_2.0.1 Boruta_7.0.0
[8] iterators_1.0.13 progress_1.2.2 rrapply_1.2.3 RColorBrewer_1.1-2 terra_1.5-17 gridExtra_2.3 PresenceAbsence_1.1.9 [15] qs_0.25.2 purrr_0.3.4 ggplotify_0.1.0 cowplot_1.1.1 caret_6.0-90 ggplot2_3.3.5 MuMIn_1.43.17
[22] plyr_1.8.6 formula.tools_1.7.1 rlist_0.4.6.2 data.table_1.14.2 eHOF_1.10 lattice_0.20-41 mgcv_1.8-34
[29] nlme_3.1-152 MLmetrics_1.1.1 usdm_1.1-18 raster_3.5-11 sp_1.4-6 psych_2.1.9 openxlsx_4.2.5
[36] stringr_1.4.0 beepr_1.3 progressr_0.10.0 doRNG_1.8.2 rngtools_1.5.2 doFuture_0.12.0 future.apply_1.8.1
[43] future_1.23.0 foreach_1.5.1

loaded via a namespace (and not attached): [1] utf8_1.2.2 tidyselect_1.1.1 mlr3misc_0.10.0 pROC_1.18.0 munsell_0.5.0 codetools_0.2-18 units_0.7-2
[8] withr_2.4.3 audio_0.1-10 colorspace_2.0-2 knitr_1.37 uuid_1.0-3 rstudioapi_0.13 stats4_4.0.5
[15] mapmisc_1.8.0 wk_0.6.0 ggsignif_0.6.3 tensor_1.5 listenv_0.8.0 mnormt_2.0.2 lgr_0.4.3
[22] polyclip_1.10-0 ENMeval_2.0.3 parallelly_1.30.0 vctrs_0.3.8 generics_0.1.1 ipred_0.9-12 xfun_0.29
[29] R6_2.5.1 lhs_1.1.3 spatstat.utils_2.3-0 gridGraphics_0.5-1 assertthat_0.2.1 scales_1.1.1 nnet_7.3-15
[36] rgeos_0.5-9 gtable_0.3.0 globals_0.14.0 goftest_1.2-3 timeDate_3043.102 rlang_0.4.12 splines_4.0.5
[43] rstatix_0.7.0 rgdal_1.5-28 ModelMetrics_1.2.2.2 paradox_0.7.1 hexbin_1.28.2 spatstat.geom_2.3-1 broom_0.7.11
[50] checkmate_2.0.0 s2_1.0.7 reshape2_1.4.4 abind_1.4-5 mlr3_0.13.1 backports_1.4.1 tools_4.0.5
[57] lava_1.6.10 ellipsis_0.3.2 spatstat.core_2.3-2 proxy_0.4-26 Rcpp_1.0.8 classInt_0.4-3 prettyunits_1.1.1
[64] ggpubr_0.4.0 rpart_4.1-15 deldir_1.0-6 zoo_1.8-9 magrittr_2.0.1 groupdata2_2.0.0 tmvnsim_1.0-2
[71] stringfish_0.15.5 hms_1.1.1 jpeg_0.1-9 compiler_4.0.5 tibble_3.1.6 KernSmooth_2.23-18 crayon_1.4.2
[78] tidyr_1.1.4 RcppParallel_5.1.5 lubridate_1.8.0 DBI_1.1.2 RApiSerialize_0.1.0 MASS_7.3-53.1 boot_1.3-28
[85] SDMtune_1.1.5 Matrix_1.3-2 car_3.0-12 gower_0.2.2 forcats_0.5.1 pkgconfig_2.0.3 spatstat.sparse_2.1-0 [92] recipes_0.1.17 xml2_1.3.3 rmaxent_0.8.5.9000 prodlim_2019.11.13 rvest_1.0.2 yulab.utils_0.0.4 digest_0.6.29
[99] spatstat.data_2.1-2 operator.tools_1.6.3 gtools_3.9.2 lifecycle_1.0.1 dismo_1.3-5 carData_3.0-5 ENMTools_1.0.5
[106] viridisLite_0.4.0 fansi_1.0.2 pillar_1.6.4 spatstat.linnet_2.3-1 httr_1.4.2 survival_3.2-10 glue_1.6.0
[113] remotes_2.4.2 rasterVis_0.51.1 zip_2.2.0 spatstat_2.3-0 png_0.1-7 class_7.3-20 stringi_1.7.6
[120] palmerpenguins_0.1.0 latticeExtra_0.6-29 e1071_1.7-9

HenrikBengtsson commented 2 years ago

AFAIK, the only progress update solution that works with foreach + doFuture, is the one provided by the progressr package. This solution is not specific to doFuture, but rather future.

The doSNOW solution will (obviously) only work for doSNOW and is different in the sense that it's not giving near-live updates from the parallel workers, but only signal progress when a worker is done and the results have been collected.

For the record, the issue regarding progressr crashing in RStudio was moved to https://github.com/HenrikBengtsson/progressr/issues/133.