有人在R中有异常处理的示例/教程吗?官方文档非常简洁。
有人在R中有异常处理的示例/教程吗?官方文档非常简洁。
Answers:
除了Shane的答案可以将您引向其他StackOverflow讨论之外,您还可以尝试使用代码搜索功能。此原始答案指向Google的代码搜索,此后已停止使用,但您可以尝试
只是为了记录在案,也有try
,但tryCatch
可能是可取的。我在Google代码搜索中尝试了一下快速计数,但是尝试对动词本身获取了太多的误报-但它似乎tryCatch
得到了更广泛的使用。
基本上,您想使用该tryCatch()
功能。查看help(“ 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"))
看一下这些相关的问题:
谷歌相关搜索的结果对我有所帮助:http : //biocodenv.com/wordpress/?p=15。
for(i in 1:16){
result <- try(nonlinear_modeling(i));
if(class(result) == "try-error") next;
}
该函数trycatch()
相当简单,并且有很多很好的教程。可以在Hadley Wickham的Advanced-R书中找到有关R中错误处理的出色解释,并且以下内容是一个非常基本的介绍,withCallingHandlers()
并withRestarts()
用尽可能少的单词介绍了这些内容:
可以说一个低级的程序员编写了一个函数来计算绝对值。他不确定如何计算,但知道如何构造错误并努力传达自己的天真:
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()
and withCallingHandlers()
,该函数
high_level_ABS
能够告诉您mid_level_ABS
如何从错误引起的low_level_ABS
错误中恢复而不会停止执行
mid_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
)。