有没有人有R中异常处理的例子/教程?官方文件非常简洁。R中的异常处理
R中的异常处理
回答
除了Shane的回答指向其他StackOverflow讨论,您可以尝试代码搜索功能。这种原始的回答指出,谷歌的代码搜索已经被停产,但你可以尝试
- Github上搜索作为例如在this query for tryCatch in language=R;
- Ohloh对/ Blackduck代码搜索如this query for tryCatch in R files
- 的Debian code search engine on top of the whole Debian archive
只是为了记录在案,也有try
但tryCatch
可能是可取的。我在Google代码搜索中尝试过很快计数,但是尝试为动词本身获取太多误报 - 但看起来tryCatch
更为广泛。
基本上你想使用tryCatch()
函数。查看帮助(“tryCatch”)了解更多详情。
这里有一个简单的例子(记住,你可以做任何你想要的一个错误):
vari <- 1
tryCatch(print("passes"), error = function(e) print(vari), finally=print("finished"))
tryCatch(stop("fails"), error = function(e) print(vari), finally=print("finished"))
看一看这些相关的问题:
第三个链接不是链接。 – Marek 2010-04-13 12:04:51
非Shane的错 - 可以说是正则表达式中的一个错误,用于确定如何标记SO上的内容。 – 2010-04-13 12:18:39
来自相关谷歌搜索的这个结果帮助了我:http://biocodenv.com/wordpress/?p=15。
for(i in 1:16){
result <- try(nonlinear_modeling(i));
if(class(result) == "try-error") next;
}
重新启动功能在从Lisp继承的R中非常重要。如果你想在循环体中调用一些函数,并且你只是希望程序在函数调用崩溃的时候继续,那么这很有用。试试这个代码:
for (i in 1:20) withRestarts(tryCatch(
if((a <- runif(1))>0.5) print(a) else stop(a),
finally = print("loop body finished!")),
abort = function(){})
函数trycatch()
是相当直接的,并且有很多很好的教程。错误R中处理的优异的解释可在哈德利韦翰的书Advanced-R被找到,接下来就是非常基本介绍到withCallingHandlers()
和withRestarts()
在为几句话越好:
比方说一个低水平程序员编写一个函数来计算绝对值 的值。他不知道如何来计算的话,但是他知道how to construct an error和 努力传达自己的幼稚:
low_level_ABS <- function(x){
if(x<0){
#construct an error
negative_value_error <- structure(
# with class `negative_value`
class = c("negative_value","error", "condition"),
list(message = "Not Sure what to with a negative value",
call = sys.call(),
# and include the offending parameter in the error object
x=x))
# raise the error
stop(negative_value_error)
}
cat("Returning from low_level_ABS()\n")
return(x)
}
一个中等水平的程序员也写一个函数来计算绝对值,利用了远远不完全low_level_ABS
功能。他知道,低级代码抛出一个negative_value
错误时的x
值为负并表明一个解决问题的办法,通过 建立restart
,其允许mid_level_ABS
用户控制 方式,其中mid_level_ABS
复苏(或不)从negative_value
错误。
mid_level_ABS <- function(y){
abs_y <- withRestarts(low_level_ABS(y),
# establish a restart called 'negative_value'
# which returns the negative of it's argument
negative_value_restart=function(z){-z})
cat("Returning from mid_level_ABS()\n")
return(abs_y)
}
最后,高电平编程器使用mid_level_ABS
函数来计算 绝对值,并建立了一个条件处理程序,它告诉 mid_level_ABS
通过使用重启 处理程序从negative_value
错误恢复。
high_level_ABS <- function(z){
abs_z <- withCallingHandlers(
# call this function
mid_level_ABS(z) ,
# and if an `error` occurres
error = function(err){
# and the `error` is a `negative_value` error
if(inherits(err,"negative_value")){
# invoke the restart called 'negative_value_restart'
invokeRestart('negative_value_restart',
# and invoke it with this parameter
err$x)
}else{
# otherwise re-raise the error
stop(err)
}
})
cat("Returning from high_level_ABS()\n")
return(abs_z)
}
所有这一切的一点是,通过使用withRestarts()
和withCallingHandlers()
,功能 high_level_ABS
能告诉mid_level_ABS
如何从错误中恢复 不停止的 mid_level_ABS
执行,这是每个人所low_level_ABS
错误引发你不能做tryCatch()
:
> high_level_ABS(3)
Returning from low_level_ABS()
Returning from mid_level_ABS()
Returning from high_level_ABS()
[1] 3
> high_level_ABS(-3)
Returning from mid_level_ABS()
Returning from high_level_ABS()
[1] 3
在实践中,low_level_ABS
代表一个函数,mid_level_ABS
调用 很多(甚至可能是数百万次),对此,错误 的正确处理方法可能因情况而异,并且如何处理特定错误的选择是 留给较高级别函数(high_level_ABS
)。
- 1. R中的异常处理
- 2. 如何处理异常处理代码中的异常?
- 3. 异常处理
- 4. 处理异常
- 5. 异常处理
- 6. 异常处理
- 7. 异常处理
- 8. 异常处理
- 9. 处理异常
- 10. 异常处理
- 11. 异常处理
- 12. 异常处理
- 13. 异常处理
- 14. 异常处理?
- 15. 异常处理
- 16. 异常处理
- 17. 异常处理
- 18. 异常处理
- 19. MVVMLight中的异常处理
- 20. Erlang中的异常处理
- 21. PLSQL中的异常处理
- 22. TPL中的异常处理
- 23. REST中的异常处理
- 24. MFC中的异常处理
- 25. ThreadPools中的异常处理
- 26. Java中的异常处理
- 27. iphone中的异常处理?
- 28. FitNesse中的异常处理
- 29. Iterable中的异常处理
- 30. Java中的异常处理
这也是一个很好的例子:http://stackoverflow.com/q/12193779/2026975。 – imriss 2013-08-14 17:44:35
我发现这篇博文很有用:[http://mazamascience.com/WorkingWithData/?p=912](http://mazamascience.com/WorkingWithData/?p=912) – 2015-06-02 13:05:32
我请求了SO文档主题对于这个问题。 – Leonid 2017-06-01 05:41:40