R中的异常处理

有人在R中有异常处理的示例/教程吗?官方文档非常简洁。



肥皂起泡泡
浏览 718回答 3
3回答

牛魔王的故事

除了Shane的答案可以将您引向其他StackOverflow讨论之外,您还可以尝试使用代码搜索功能。此原始答案指向Google的代码搜索,此后已停止使用,但您可以尝试Github搜索,例如在此查询中以language = R 搜索tryCatch;Ohloh / Blackduck代码搜索,例如此查询R文件中的tryCatch在整个Debian档案库中的Debian代码搜索引擎只是为了记录在案,也有try,但tryCatch可能是可取的。我在Google代码搜索中尝试了一下快速计数,但是尝试为该动词本身获取了太多的误报-但它似乎tryCatch得到了更广泛的使用。

开满天机

基本上,您想使用该tryCatch()功能。查看help(“ tryCatch”)了解更多详细信息。这是一个简单的示例(请记住,您可以执行任何您想做的错误操作):vari <- 1tryCatch(print("passes"), error = function(e) print(vari), finally=print("finished"))&nbsp;tryCatch(stop("fails"), error = function(e) print(vari), finally=print("finished"))&nbsp;

翻翻过去那场雪

该功能trycatch()相当简单,并且有很多很好的教程。在Hadley Wickham的书Advanced-R中可以找到R中错误处理的出色解释,其后的内容是一个非常基本的介绍,withCallingHandlers()并且withRestarts()用尽可能少的词:假设低级程序员编写了一个函数来计算绝对值。他不确定如何计算,但知道如何构造错误并努力传达自己的天真:low_level_ABS <- function(x){&nbsp; &nbsp; if(x<0){&nbsp; &nbsp; &nbsp; &nbsp; #construct an error&nbsp; &nbsp; &nbsp; &nbsp; negative_value_error <- structure(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # with class `negative_value`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class = c("negative_value","error", "condition"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list(message = "Not Sure what to with a negative value",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;call = sys.call(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# and include the offending parameter in the error object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x=x))&nbsp; &nbsp; &nbsp; &nbsp; # raise the error&nbsp; &nbsp; &nbsp; &nbsp; stop(negative_value_error)&nbsp; &nbsp; }&nbsp; &nbsp; cat("Returning from low_level_ABS()\n")&nbsp; &nbsp; return(x)}一个中级程序员还编写了一个函数,使用可悲的不完整low_level_ABS函数来计算绝对值。他知道,negative_value 当的值为x负时,低级代码将引发错误,并通过建立允许用户控制错误恢复(或不恢复)的方式来建议解决问题的方法。restartmid_level_ABSmid_level_ABSnegative_valuemid_level_ABS <- function(y){&nbsp; &nbsp; abs_y <- withRestarts(low_level_ABS(y),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # establish a restart called 'negative_value'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # which returns the negative of it's argument&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; negative_value_restart=function(z){-z})&nbsp;&nbsp; &nbsp; cat("Returning from mid_level_ABS()\n")&nbsp; &nbsp; return(abs_y)}最终,高级程序员使用该mid_level_ABS函数来计算绝对值,并建立条件处理程序,该条件处理程序通过使用重新启动处理程序来告诉 错误mid_level_ABS从negative_value错误中恢复。high_level_ABS <- function(z){&nbsp; &nbsp; abs_z <- withCallingHandlers(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # call this function&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mid_level_ABS(z) ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # and if an `error` occurres&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error = function(err){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # and the `error` is a `negative_value` error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(inherits(err,"negative_value")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # invoke the restart called 'negative_value_restart'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invokeRestart('negative_value_restart',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# and invoke it with this parameter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;err$x)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # otherwise re-raise the error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stop(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; cat("Returning from high_level_ABS()\n")&nbsp; &nbsp; 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)。
打开App,查看更多内容
随时随地看视频慕课网APP