中断/退出脚本 [英] break/exit script
问题描述
我有一个程序,该程序进行一些数据分析,并且有几百行.
I have a program that does some data analysis and is a few hundred lines long.
在程序的早期,我想进行一些质量控制,如果没有足够的数据,我希望程序终止并返回到R控制台.否则,我希望其余代码执行.
Very early on in the program, I want to do some quality control and if there is not enough data, I want the program to terminate and return to the R console. Otherwise, I want the rest of the code to execute.
我尝试了 break
, browser
和 quit
,但它们都没有阻止程序其余部分的执行(并且 quit
会停止执行并完全退出R,这是我不希望发生的事情.我的最后一招是创建一个 if-else
语句,如下所示:
I've tried break
,browser
, and quit
and none of them stop the execution of the rest of the program (and quit
stops the execution as well as completely quitting R, which is not something I want to happen). My last resort is creating an if-else
statement as below:
if(n < 500){}
else{*insert rest of program here*}
但这似乎是不好的编码习惯.我想念什么吗?
but that seems like bad coding practice. Am I missing something?
推荐答案
如果希望程序产生错误,则可以使用 stopifnot()
函数:
You could use the stopifnot()
function if you want the program to produce an error:
foo <- function(x) {
stopifnot(x > 500)
# rest of program
}
这篇关于中断/退出脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!