rparallel-foreach

Does not save file using foreach loop in R


I am encountering difficulties saving data related to "abc" into a file named "myResults.rds". My code is complex and involves multiple operations within a foreach loop. I wish to exclusively save a particular outcome that I have stored in a list named "abc". Here is an illustration of the code:

library(foreach)
library(doParallel)

# create a cluster with 6 workers
cl <- makeCluster(6)
registerDoParallel(cl)

# define a local function
generate<-function(p){
  MIN <- (1-p)*100
  MAX <- (1+p)*100
  
  print(paste(MIN,MAX))
  mat <- matrix(nrow=1,ncol=4)
  
  for (i in seq(1,11,1)){ 
    a <- round(runif(1,MIN,MAX),2)
    b <- round(runif(1,MIN,MAX),2)
    c <- round(runif(1,MIN,MAX),2)
    d <- round(runif(1,MIN,MAX),2)
    mat <- rbind(mat,c(a,b,c,d))
  }
  return(mat[2:length(mat[,1]), ])
}

# run the loop with foreach and call the local function in parallel
abc<-list()
results <- foreach(i = 1:3) %dopar% {
  lstData=generate(0.05)
  abc[[i]]=lstData
  # main code goes here for analysis ......
  # Just added two simple operations for cheeking
  addNum <- 1+2+3
  subNum <- 1-2-3
}

saveRDS(abc, file = "myResults.rds")
stopCluster(cl)

Solution

  • Here is another approach that can be considered :

    library(foreach)
    library(doParallel)
    
    # create a cluster with 6 workers
    cl <- makeCluster(6)
    registerDoParallel(cl)
    
    # define a local function
    generate<-function(p){
      MIN <- (1-p)*100
      MAX <- (1+p)*100
      
      print(paste(MIN,MAX))
      mat <- matrix(nrow=1,ncol=4)
      
      for (i in seq(1,11,1)){ 
        a <- round(runif(1,MIN,MAX),2)
        b <- round(runif(1,MIN,MAX),2)
        c <- round(runif(1,MIN,MAX),2)
        d <- round(runif(1,MIN,MAX),2)
        mat <- rbind(mat,c(a,b,c,d))
      }
      return(mat[2:length(mat[,1]), ])
    }
    
    # run the loop with foreach and call the local function in parallel
    abc <- foreach(i = 1:3) %dopar% {
      lstData <- generate(0.05)
      lstData
    }
    
    saveRDS(abc, file = "myResults.rds")
    stopCluster(cl)
    
    abc
    [[1]]
            [,1]   [,2]   [,3]   [,4]
     [1,] 104.34  98.19  96.11 103.58
     [2,] 101.27 103.05 100.01 104.07
     [3,]  95.80  96.97 104.41 104.56
     [4,]  98.17  98.74  96.02  95.49
     [5,] 101.64 101.55 104.62 100.28
     [6,] 100.49 101.97  95.82  96.43
     [7,] 102.46  98.69 101.47 101.01
     [8,]  96.48  95.38  95.80  95.14
     [9,] 101.41 104.61 103.14  99.86
    [10,] 104.56  97.43 100.10  98.58
    [11,]  97.95 103.36 103.63  98.74
    
    [[2]]
            [,1]   [,2]   [,3]   [,4]
     [1,]  95.78 103.40 103.07 103.83
     [2,] 100.48 104.37 100.59 103.10
     [3,]  99.93 101.82 100.00  98.57
     [4,]  97.94  97.77 101.50 103.82
     [5,]  98.82  96.73  99.26 104.16
     [6,]  97.58 104.66  99.21  99.95
     [7,]  99.02  97.37  98.06 100.84
     [8,]  97.04 104.66 102.65 101.49
     [9,]  97.67  97.18 100.35  97.41
    [10,]  95.84 101.47  96.80  95.71
    [11,]  97.12 104.15 101.98 100.59
    
    [[3]]
            [,1]   [,2]   [,3]   [,4]
     [1,]  95.87 104.25 101.65  96.65
     [2,] 100.64  96.49  98.29 104.78
     [3,]  98.21  98.23  95.01  98.40
     [4,]  96.70 100.93 104.14  96.12
     [5,]  97.64  95.97  96.90  96.59
     [6,] 104.69 102.72 100.10 104.98
     [7,] 101.31  95.62  98.23 105.00
     [8,]  95.52  95.29 103.74  99.05
     [9,]  95.63  95.15 104.47  96.79
    [10,] 101.12  98.20  97.86  97.84
    [11,] 102.02 104.59 103.97  96.12