foreach over a list in different cores

How could I proceed to use foreach over a list in different cores?. Here, I am trying to add a row over the existing rows.

require(microbenchmark, quietly=TRUE)
require(doParallel, quietly=TRUE)
require(ggplot2, quietly=TRUE)
detectedCores <- parallel::detectCores()
registerDoParallel(cores=detectedCores - 1)
#Given dataframe
set.seed(10)
dat1=data.frame(
  sl=1:17,
  first=sample(17),
  second=sample(17))
#####################################################
fun_2 <- function(z){
  i=z[,1]
  x=z[,2]
  y=z[,3]
  df_mont=dat1 %>% filter(sl==i)%>% add_row(sl=i+0.5,first = x+2, second = y+3)
  return(df_mont)
  }
##########################################
items <- nrow(dat1)
batches <- detectedCores * 1
batchSets <- split(dat1,  seq(items))

The expected outcome using lapply is

df=lapply(batchSets, fun_2)
f=do.call("rbind", df)
f[order(f$sl),]

Thanks

finalSum <- foreach(
           b = iter(batchSets, by = "row"),
           .combine = rbind,
           .packages = "tidyverse") %dopar% { fun_2(b) } %>% 
         arrange(sl)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.