rmultithreadingparallel-processingoutputsnow

Capturing output from new threads in R


I am running a multithreaded R script but I am having trouble with generating output from the cluster.

outFun <- function()
{
  cat(sample(0:9,1));
}
require(snow)
clust <- makeCluster(4)
clusterExport(clust,"outFun")
clustFun <- function(i){outFun()}
clusterApplyLB(clust,1:8,clustFun)

I understand that I don't see any of the output from outFun() because it's in a new R thread, but I was hoping that there would be some way to forward this output back to the master thread so it was visible when printed.

EDIT: This question answers this for a Linux machine, but the solution does not work for Windows. The workaround given is to simply use file output, but I am curious if anyone knows a solution to be able to actually send output back to the master thread in Windows.


Solution

  • The makeCluster outfile="" option doesn't work with Rgui on Windows because Rgui doesn't send output from child processes to the display window. However, outfile="" does work with the Rterm program because it's a simple console program:

    C:\Program Files\R\R-3.0.2\bin\i386> rterm -q
    > library(parallel)
    > clust <- makeCluster(4, outfile="")
    starting worker pid=1596 on localhost:11862 at 09:13:30.005
    starting worker pid=1192 on localhost:11862 at 09:13:30.342
    starting worker pid=1616 on localhost:11862 at 09:13:30.679
    

    The "starting worker" messages are coming from the worker processes just before they execute the slaveLoop function. You should also see output from the workers from executing tasks:

    > clusterEvalQ(clust, message("hello"))
    hello
    hello
    hello
    hello
    [[1]]
    NULL
    
    [[2]]
    NULL
    
    [[3]]
    NULL
    
    [[4]]
    NULL