1

I want to know how many loops have been done and save the results of each finished loop, but assign and cat function are not working.

library(doParallel)
library(foreach)
library(Matrix)

rm(list=ls())


cl=makeCluster(2)
registerDoParallel(cl)

  sink("report.txt")
result=foreach (n=1:10,.packages="Matrix" )%dopar%{
  variable <-sparseMatrix(dims = c(100,150), i={1}, j={1},x=0)


  cat(sprintf("tastk %d is complete \n",n),append=TRUE)
  assign(paste("variable",n,sep=""),variable)
  return(variable)

} 

sink()
LauC
  • 55
  • 5
  • 2
    Possible duplicate of [How can I \`print\` or \`cat\` when using parallel](https://stackoverflow.com/questions/16717461/how-can-i-print-or-cat-when-using-parallel) – F. Privé Oct 02 '18 at 16:22
  • I have tried but, after implementing cl=makeCluster(20,outfile="report.txt") When I open the report during the loop, any line is written. – LauC Oct 02 '18 at 17:04
  • Does my answer work for you? – F. Privé Oct 05 '18 at 09:02
  • not really, the variable is not created and it writes in the text file when it wants. I think is something related to the ram memory, but I don´t know how to manage it. – LauC Oct 05 '18 at 13:46

1 Answers1

1

This works perfectly fine on my Linux computer:

library(doParallel)
library(foreach)
library(Matrix)

rm(list=ls())


cl=makeCluster(2, outfile = "report.txt")
registerDoParallel(cl)

result=foreach (n=1:10,.packages="Matrix" )%dopar%{

  variable <-sparseMatrix(dims = c(100,150), i={1}, j={1},x=0)      

  cat(sprintf("task %d is complete \n",n))
  assign(paste("variable",n,sep=""),variable)
  return(variable)

} 

stopCluster(cl)
F. Privé
  • 11,423
  • 2
  • 27
  • 78