I agree with Roland that the problem is in the way that tclTaskSchedule
evaluates its arguments. My solution isn't pretty, but I got it to work by exporting testfn
using clusterExport
and assigning i
to the worker's global environment in the foreach loop:
testfn <- function() print(paste("hello from", i))
clusterExport(cl, "testfn")
foreach(i=1:n, .packages = c("tcltk", "tcltk2"), .verbose = F) %dopar% {
i <<- i
tclTaskSchedule(1000, testfn(), id = paste0("task", i), redo = 10)
}
I would probably also pass .noexport="testfn"
to foreach, but it isn't really necessary.