R 如何防止在出错后执行进一步的代码步骤?

R 如何防止在出错后执行进一步的代码步骤?,r,R,如何确保在“捕获”错误并将其记录下来之后,不会执行进一步的代码步骤(我不想使用q()) 我的使用场景如下: -做一些计算 -如果发生错误,记录它 -停止执行代码中的任何进一步步骤 我尝试使用下面的代码示例解决这个问题(使用print而不是true日志函数): handleMySimpleError handleMySimpleError >打印(“开始执行…”) [1] “开始执行…” >tryCatch( +停止(“我的简单错误”), +error=function(e){handleMySi

如何确保在“捕获”错误并将其记录下来之后,不会执行进一步的代码步骤(我不想使用q())

我的使用场景如下: -做一些计算 -如果发生错误,记录它 -停止执行代码中的任何进一步步骤

我尝试使用下面的代码示例解决这个问题(使用print而不是true日志函数):

handleMySimpleError handleMySimpleError
>打印(“开始执行…”)
[1] “开始执行…”
>tryCatch(
+停止(“我的简单错误”),
+error=function(e){handleMySimpleError(e,“由于“)”而无法完成),finnaly=NULL
+ )
[1] 无法完成,原因是:doTryCatch(返回(expr)、名称、parentenv、处理程序)中的错误:我的简单错误。\n
handleMySimpleError中的错误(e,“由于以下原因无法完成”):
现在,停下来。来真的
>打印(“已成功结束执行…”)
[1] “已成功结束执行…”

如何防止执行打印(“成功结束执行…”)?在错误处理函数中记录错误后,停止代码处理的正确策略是什么?

只需用大括号括起来即可

>     {
+       handleMySimpleError<-function(e, text) {
+           # Let's log the error
+           print(paste0(text, ": ", e))
+           # This should stop execution of any further steps but it doesn't
+           stop("Now, stop. For real.")
+       }
+       print("Starting execution...")
+       tryCatch(
+           stop("My simple error."),
+           error=function(e) {handleMySimpleError(e, "could not finish due to")}, finally=NULL
+       )
+       print("Successfully ended execution...") 
+     }
[1] "Starting execution..."
[1] "could not finish due to: Error in doTryCatch(return(expr), name, parentenv, handler): My simple error.\n"
Error in handleMySimpleError(e, "could not finish due to") : 
  Now, stop. For real.
>{

+HandleMySimpleError您是如何执行该代码的?寻找脚本?我现在通过StatEt Eclipse插件使用Ctrl+R+R对其进行测试。我想在“生产”中使用“脚本”比如:/usr/bin/R--vanilla--quiet停止后,您会收到一条错误消息,然后执行下一行。如果用大括号括起来,您实际上是在告诉R,“立即运行整个块,而不是逐行运行。”现在,当它出现错误时,它会退出代码块。我现在看到了。谢谢。这更有意义。我假设处理是以与SAS中类似的方式完成的。。。
> handleMySimpleError<-function(e, text) {
+   # Let's log the error
+   print(paste0(text, ": ", e))
+   # This should stop execution of any further steps but it doesn't
+   stop("Now, stop. For real.")
+ }
>  
> print("Starting execution...")
[1] "Starting execution..."
> tryCatch(
+   stop("My simple error."),
+   error=function(e) {handleMySimpleError(e, "could not finish due to")}, finnaly=NULL
+ )
[1] "could not finish due to: Error in doTryCatch(return(expr), name, parentenv, handler): My simple error.\n"
Error in handleMySimpleError(e, "could not finish due to") : 
Now, stop. For real.
> print("Successfully ended execution...")
[1] "Successfully ended execution..."
>     {
+       handleMySimpleError<-function(e, text) {
+           # Let's log the error
+           print(paste0(text, ": ", e))
+           # This should stop execution of any further steps but it doesn't
+           stop("Now, stop. For real.")
+       }
+       print("Starting execution...")
+       tryCatch(
+           stop("My simple error."),
+           error=function(e) {handleMySimpleError(e, "could not finish due to")}, finally=NULL
+       )
+       print("Successfully ended execution...") 
+     }
[1] "Starting execution..."
[1] "could not finish due to: Error in doTryCatch(return(expr), name, parentenv, handler): My simple error.\n"
Error in handleMySimpleError(e, "could not finish due to") : 
  Now, stop. For real.