停止从 R 中的子项执行父函数
Stopping Execution Of A Parent Function From Child In R
我正在使用 R 中的 stop
函数来执行一些故障排除措施。例如,当数据输入错误时抛出错误。我想要一个可以调用的通用函数来执行此检查,然后我可以调用父代码的几个不同点。我可以很好地抛出一个错误,但是,我注意到父函数在抛出错误后继续执行,只有我的 "error function" 如果你将停止执行。当发生 stop
的错误时,有没有办法让 R 全局停止执行?
谢谢。
我的问题的编码示例:
StopCode <- function() {
try(if(TRUE) stop("This try/if statement should stop the code from executing."))
}
StopCode() # This function should stop the code!
print("I should not see this statement if the try/if statement stopped the code in the way I desired.")
控制台:
Error in try(if (TRUE) stop("This try/if statement should stop the code from executing.")) :
This try/if statement should stop the code from executing.
[1] "I should not see this statement if the try/if statement stopped the code in the way I desired."
期望的结果应该是 StopCode 函数完全停止代码,而不仅仅是函数的执行。当您获取此代码时,它会停止该功能,发出警告,然后继续 运行 打印功能。我希望这种情况不会发生,并希望函数停止以全局终止执行。
基本上,您想将选项(错误=)设置为您选择的某些功能。我将在此处粘贴文档 ?options:
error:
either a function or an expression governing the handling of non-catastrophic
errors such as those generated by stop as well as by signals and internally
detected errors. If the option is a function, a call to that function, with
no arguments, is generated as the expression. The default value is NULL: see
stop for the behaviour in that case. The functions dump.frames and recover
provide alternatives that allow post-mortem debugging. Note that these need
to specified as e.g. options(error = utils::recover) in startup files such
as ‘.Rprofile’.
默认情况下,stop
完全按照您的意愿执行:它会跳出每个 运行 函数,直到它位于顶层 — 如果脚本是 运行 非交互,然后停止脚本。
但是,在您发布的代码中,您通过 try
捕获错误,明确告诉 R not 停止执行调用函数。不清楚您为什么在这里使用 try
,因为它的唯一目的是防止您似乎想要的行为。
解决方案就是从您的代码中删除 try
。或者,您可能想要捕获错误,处理它,然后重新抛出它:
result = try(‹some operation that may fail›)
if (inherits(result, 'try-error')) {
‹handle error›
# Re-throw error:
stop(attr(result, 'condition'))
}
我正在使用 R 中的 stop
函数来执行一些故障排除措施。例如,当数据输入错误时抛出错误。我想要一个可以调用的通用函数来执行此检查,然后我可以调用父代码的几个不同点。我可以很好地抛出一个错误,但是,我注意到父函数在抛出错误后继续执行,只有我的 "error function" 如果你将停止执行。当发生 stop
的错误时,有没有办法让 R 全局停止执行?
谢谢。
我的问题的编码示例:
StopCode <- function() {
try(if(TRUE) stop("This try/if statement should stop the code from executing."))
}
StopCode() # This function should stop the code!
print("I should not see this statement if the try/if statement stopped the code in the way I desired.")
控制台:
Error in try(if (TRUE) stop("This try/if statement should stop the code from executing.")) :
This try/if statement should stop the code from executing.
[1] "I should not see this statement if the try/if statement stopped the code in the way I desired."
期望的结果应该是 StopCode 函数完全停止代码,而不仅仅是函数的执行。当您获取此代码时,它会停止该功能,发出警告,然后继续 运行 打印功能。我希望这种情况不会发生,并希望函数停止以全局终止执行。
基本上,您想将选项(错误=)设置为您选择的某些功能。我将在此处粘贴文档 ?options:
error:
either a function or an expression governing the handling of non-catastrophic
errors such as those generated by stop as well as by signals and internally
detected errors. If the option is a function, a call to that function, with
no arguments, is generated as the expression. The default value is NULL: see
stop for the behaviour in that case. The functions dump.frames and recover
provide alternatives that allow post-mortem debugging. Note that these need
to specified as e.g. options(error = utils::recover) in startup files such
as ‘.Rprofile’.
默认情况下,stop
完全按照您的意愿执行:它会跳出每个 运行 函数,直到它位于顶层 — 如果脚本是 运行 非交互,然后停止脚本。
但是,在您发布的代码中,您通过 try
捕获错误,明确告诉 R not 停止执行调用函数。不清楚您为什么在这里使用 try
,因为它的唯一目的是防止您似乎想要的行为。
解决方案就是从您的代码中删除 try
。或者,您可能想要捕获错误,处理它,然后重新抛出它:
result = try(‹some operation that may fail›)
if (inherits(result, 'try-error')) {
‹handle error›
# Re-throw error:
stop(attr(result, 'condition'))
}