Problems with try() inside foreach() in R

眉间皱痕 提交于 2019-12-20 19:45:10

问题


I am trying to use the try() function to deal with errors that are occurring in my parallelised for loop:

results <- foreach (i = 1:2, .errorhandling = 'remove') %dopar% {
    res <- try(myfun(i), TRUE)
}

with

myfun <- function(i){
    if (i==1) return(rnorm(1))
    else stop('error')
}

I get the following error message

Error in checkForRemoteErrors(val) : 
  one node produced an error: Error in myfun(i) : error

How can I get the foreach "loop" to ignore the error message (or at least deal with it a little more elegantly)?


回答1:


You can use tryCatch and deal with the error appropriately. Here the error is ignored (returning NULL)

results <- foreach (i = 1:2) %dopar% {
    res <- tryCatch({
        myfun(i)
    }, error=function(e) NULL)
}

or just using the builtin .errorhandling='remove' as you have, without the try should remove the errors already.




回答2:


If you want to use the "remove" or "pass" error handling in foreach, you don't need to catch the error yourself. Here are the results when setting the error handling to "remove":

> foreach (i = 1:2, .errorhandling = 'remove') %dopar% {
+     myfun(i)
+ }
[[1]]
[1] 1.314854

Here are the results when using "pass":

> foreach (i = 1:2, .errorhandling = 'pass') %dopar% {
+     myfun(i)
+ }
[[1]]
[1] 0.7247509

[[2]]
<simpleError in myfun(i): error>

By using try, the error is caught and then translated to a "try-error" object by the try function, hiding the error from foreach until the the clusterApplyLB function (which is used to implement the doParallel backend) notices the "try-error" object in the list of results and throws an error in the master process. I would call that a bug in doParallel.

Note that the solution given by @LegalizeIt works because it returns a NULL rather than a "try-error" object, thus avoiding the bug in doParallel.



来源:https://stackoverflow.com/questions/31138257/problems-with-try-inside-foreach-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!