5

I have been trying to get some output displayed from the foreach loop R. A reproducible example is

cl <- makeCluster(2)
registerDoParallel(cl)


ptm1 <- proc.time()
foreach (i = 1:50, .packages = c("MASS"), .combine='+') %dopar% {
  ginv(matrix(rexp(1000000, rate=.001), ncol=1000))
  if (i >49){
    cat("Time taken", proc.time() - ptm1)
  }
}

I expect the time taken to be displayed. But, this does not display anything. Can you please suggest ways of capturing the messages in the foreach loop and displaying at the end of the loop.

1

1 Answer 1

2

I'm not sure if there's a way to output to the screen, but you can easily output to a log file using the sink function like so

ptm1 <- proc.time()
foreach (i = 1:50, .packages = c("MASS"), .combine='+') %dopar% {
  ginv(matrix(rexp(1000000, rate=.001), ncol=1000))
  if (i >49){

    sink("Report.txt", append=TRUE) #open sink file and add output

    cat("Time taken", proc.time() - ptm1)

  }
}

EDIT : As @Roland points out, this can be dangerous if you want to capture output from every iteration and not just the final one, because you don't want the workers to clobber each other. He links to a better alternative for this scenario in his comment.

Sign up to request clarification or add additional context in comments.

5 Comments

I think this is dangerous. If you follow this approach, you should create a separate log for each worker, similar to this
That's a good point. However in this case the OP is only calling sink once on the final process. I agree that if you want the output from each process multiple logs is the way to go.
"calling sink once on the final process" No, they want output from each iteration, which means calling sink within the loop and thus on the workers (as you indeed show in your code).
I interpreted their inclusion of if (i > 49) { cat(...)} to mean that they only desired output from the final iteration.
Ah, I see. That seems strange (and useless).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.