R:在foreach%dopar%中显示错误和警告消息 [英] R: show error and warning messages in foreach %dopar%

查看:163
本文介绍了R:在foreach%dopar%中显示错误和警告消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是不熟悉使用foreach()%dopar%进行并行处理的新手,我对它如何处理错误或警告有一些疑问。

I'm new to using foreach() %dopar% for paralleling, and I have some problems about how it handles errors or warnings.


  1. 当我将try()与foreach()%dopar%中的自定义错误消息一起使用时,本地错误消息没有显示:

  1. when I use try() with my customized error message within foreach() %dopar%, the "native" error message doesn't show up:

test <- function(x) {
  if (x==2) "a"/2
}

foreach(i=1:3) %dopar% {
  tryout <- try(test(i))
  if (class(tryout)=="try-error") print("Error!")
}

在这种情况下,本地错误消息: a / 2中的错误:二进制运算符的非数字参数不会显示,并且只有try( Error! )错误捕获将被打印。但是,当不使用foreach()%dopar%时,将同时打印这两个错误消息。那么如何使两个错误消息都显示出来?

In this case the "native" error message: Error in "a"/2 : non-numeric argument to binary operator doesn't show up, and only the Error! from try() error catching will be printed. However both error messages will be printed when not using foreach() %dopar%. So how to make both error messages show up?

在上述情况下,当有警告(无论是否有错误)时,警告消息都不会例如,使用与上面相同的foreach()块和下面的 test()打印:

In the above case, when there are warnings, whether additional to errors or not, the warning messages are not printed, for example with the same foreach() block as above and the test() below:

test <- function(x) {
  if (x==2) warning("Warning!")
}

那么如何显示警告?

ps我发现,如果仅在%dopar%中使用try(test(i)),则将显示本机错误消息和警告,但我确实希望在实际情况中包括自己的错误消息。我还尝试使用 tryCatch()代替 try(),但是并不能解决问题。

p.s. I found that if I simply use try(test(i)) within %dopar% then the "native" error messages and the warnings will be printed, but I do want to include my own error message in real-life situations. I also tried using tryCatch() instead of try(), but it didn't solve the problem.

谢谢!

推荐答案

我认为问题出在您的错误处理和对dopar有点误解。首先,将dopar更像是一个函数。默认情况下,它必须将一个对象作为列表返回给用户(它收集了每个工作程序的所有输出,而foreach函数收集了这些输出并返回使用,请参见下面的 output_list)。

I think the issue is around your errorhandling and a bit of a misunderstanding of dopar. First, think of dopar more like a function. It must return an object back to the user by default as a list (it gathers all the output from each worker and the foreach function gathers these and returns them to use, see 'output_list' below).

您还可以设置.errorhandling ='pass',它将错误返回给输出对象(请注意,仅当.combine ='list'时才有效)。 .errorhandling的工作方式如下:

You can also set .errorhandling = 'pass', it will return the error back to the output object (note probably only works if .combine ='list'). Here is how .errorhandling works:


.errorhandling指定应如何处理任务评估错误。
如果值为 stop,则在发生错误时,将通过stop
函数停止执行。如果值为 remove,则该任务的
结果将不返回或传递给.combine函数。如果
是通过,则任务评估生成的错误对象将
包含在其余结果中。假定
组合函数(如果指定)将能够处理错误的
对象。默认值为 stop。

.errorhandling specifies how a task evaluation error should be handled. If the value is "stop", then execution will be stopped via the stop function if an error occurs. If the value is "remove", the result for that task will not be returned, or passed to the .combine function. If it is "pass", then the error object generated by task evaluation will be included with the rest of the results. It is assumed that the combine function (if specified) will be able to deal with the error object. The default value is "stop".

这里是一个如何在foreach内部设置tryCatch的示例。请注意,错误现在存储在output_list中。

Here is an example of how to set up a tryCatch inside of a foreach. Note that errors are now stored in output_list.

output_list = foreach(i=1:3, .errorhandling='pass') %dopar% {
result <- tryCatch({
    object_that_doesnt_exist[i]}, 
     warning = function(war) {
         return('a warning')}, 
     error = function(err) {
         return('an error')}, 
     finally = {
         return('other things')
     }) # END tryCatch

  return(result)  # return your result to outputlist
}

output_list

这篇关于R:在foreach%dopar%中显示错误和警告消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆