When using doParallel in R, how can I get the line number where the error was selected?

I am trying to figure out how to determine the line where the error occurs when using foreach and doParallel. Here is an example:

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

So, inside% do% there is an error in which I add a character to a numeric one. (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 say from this where in the loop I had error line 3 - the foreach line and not the violation line. When I run the 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))

Please note that in frame 2 the whole cycle is usually indicated, so I can not find the line in which the error was actually made.

If instead I run this without foreach, I get useful information:

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 brings me to a line in the code that throws an exception.

, excpetion foreach? .

+4
1

foreach, .verbose = TRUE

z <- foreach (i = 1:2, .verbose = T) %do% ...
+2

Source: https://habr.com/ru/post/1548413/


All Articles