R foreach: modify .combine to create equivalent of dplyr::bind_rows and include progress bar

Viewed 70

Take the following MWE:

df <- data.frame(id =seq(1,100,1),random = seq(2,200,2))

cl <- parallel::makeForkCluster(5) # do not change
doParallel::registerDoParallel(cl) # do not change


results <- foreach::foreach(i=unique(df$id),.combine =dplyr::bind_rows) %dopar% {
data <- df %>% dplyr::filter(id == i) %>% 
dplyr::mutate(result = random^2) # just a non-sense calculation
}

parallel::stopCluster(cl)

For several reasons I want to stick to makeForkCluster to start a parallel backend and I also want to get a data.frame and not a list.

There are two questions:

  1. How do I have to create
comb <- function() 

to produce exactly the same results as above with .combine = 'comb'. I'm really struggling with .combine.

  1. Is there any way to include a simple progress bar in that MWE - I know there are several other packages/backends to inlcude progress bars, but I want to stick to the MWE above using makeForkCluster from package parallel.
0 Answers
Related