I am trying to figure out how to pinpoint the line where an error is thrown
when using foreach and doParallel. Here's an example:

foreach.example <- function() {
    require("doParallel")
    z <- foreach (i = 1:2) %do% {
        x <- i + 'a'
    }
    return(z)
}

So within the %do% there is a bug where i am adding a character to a
numeric. (Everything I say here also applies to %dopar%,). When I run this
I get:

> foreach.example()
Error in { (from test_foreach.R#3) : task 1 failed - "non-numeric argument
to binary operator"

I can't tell from this where in the loop I had the error line 3 is the
foreach line, not the offending line. When I run debugger() I get:

> debugger()
Message:  Error in { (from test_foreach.R#3) : task 1 failed - "non-numeric
argument to binary operator"
Available environments had calls:
1: foreach.example()
2: test_foreach.R#3: foreach(i = 1:2) %do% {
    x <- i + "a"
}
3: e$fun(obj, substitute(ex), parent.frame(), e$data)
4: stop(simpleError(msg, call = expr))

Note that frame 2 will generally indicate the whole loop so I can't find
the line where the error was actually thrown.

If instead I run this without the foreach, I get useful informatin:

regular.example <- function() {
    z <- list()
    for (i in 1:2) {
        x <- i + 'a'
        z <- c(z, list(x))
    }
    return(z)
}

>regular.example()
Error in i + "a" (from test_foreach.R#12) : non-numeric argument to binary
operator

and the debugger takes me to the line in code that threw the exception.

Any ideas on how to identify the line number with the excpetion when using
foreach? Thanks.

 Jeff

	[[alternative HTML version deleted]]

