I love the setting .progress = \'text\' in plyr\'s llply. However, it causes my much anxiety to not know how far along an mclapp
Based on the answer of @fotNelson, using a progress bar instead of line by line printing and calling an external function with mclapply.
library('utils')
library('multicore')
prog.indic <- local({ #evaluates in local environment only
f <- fifo(tempfile(), open="w+b", blocking=T) # open fifo connection
assign(x='f',value=f,envir=.GlobalEnv)
pb <- txtProgressBar(min=1, max=MC,style=3)
if (inherits(fork(), "masterProcess")) { #progress tracker
# Child
progress <- 0.0
while (progress < MC && !isIncomplete(f)){
msg <- readBin(f, "double")
progress <- progress + as.numeric(msg)
# Updating the progress bar.
setTxtProgressBar(pb,progress)
}
exit()
}
MC <- 100
result <- mclapply(1:MC, .mcfunc)
cat('\n')
assign(x='result',value=result,envir=.GlobalEnv)
close(f)
})
.mcfunc<-function(i,...){
writeBin(1, f)
return(i)
}
Assigning the fifo connection to the .GlobalEnv is necessary to use it from a function outside of the mclapply call. Thanks for the questions and the previous replies, I had been wondering how to do this for a while.