In order to see the console messages output by a function running in a foreach()
loop I followed the advice of this guy and added a sink()
call like so:
library(foreach)
library(doMC)
cores <- detectCores()
registerDoMC(cores)
X <- foreach(i=1:100) %dopar%{
sink("./out/log.branchpies.txt", append=TRUE)
cat(paste("\n","Starting iteration",i,"\n"), append=TRUE)
myFunction(data, argument1="foo", argument2="bar")
}
However, at iteration 77 I got the error 'sink stack is full'. There are well-answered questions about avoiding this error when using for-loops, but not foreach. What's the best way to write the otherwise-hidden foreach output to a file?
sink
andcat
with a file? – Exosmosisforeach
because it would take forever using afor
loop, or evenmclapply
(I've tried and it's much slower). I'm usingsink
andcat
because the linked page recommended I do, and because it helps keep track of which iteration theforeach
loop is up to. – Rushtonfile
argument ofcat
. – Exosmosismclapply
shouldn't be slower thanforeach
if you set up the cluster correctly. – Exosmosiscat
—that was something I was experimenting with. I mistyped the code. I'll fix it now. – Rushtonmclapply
doesn't do anything. – Waterwaymclapply
resulted in a definite speed increase relative to afor
-loop but it was meagre compared toforeach
. – Rushton