我有一个程序可以进行一些数据分析,并且只有几百行.
在程序的早期,我想做一些质量控制,如果没有足够的数据,我希望程序终止并返回到R控制台.否则,我希望其余的代码执行.
我已经尝试了break
,browser
并且quit
它们都没有停止执行程序的其余部分(并且quit
停止执行以及完全退出R,这不是我想要发生的事情).我的最后一招是创建如下if-else
声明:
if(n < 500){}
else{*insert rest of program here*}
Run Code Online (Sandbox Code Playgroud)
但这似乎是糟糕的编码实践.我错过了什么吗?
Mic*_*ick 56
stopifnot()
如果希望程序产生错误,可以使用该函数:
foo <- function(x) {
stopifnot(x > 500)
# rest of program
}
Run Code Online (Sandbox Code Playgroud)
Tho*_*mas 12
反转你的if-else结构:
if(n >= 500) {
# do stuff
}
# no need for else
Run Code Online (Sandbox Code Playgroud)
joc*_*hen 10
不漂亮,但这里是一种exit()
在R中实现命令的方法,对我有用.
exit <- function() {
.Internal(.invokeRestart(list(NULL, NULL), NULL))
}
print("this is the last message")
exit()
print("you should not see this")
Run Code Online (Sandbox Code Playgroud)
只是轻度测试,但是当我运行它时,我看到this is the last message
然后脚本中止而没有任何错误消息.
编辑:似乎OP正在运行一个长脚本,在这种情况下,只需要在质量控制之后包装脚本的一部分
if (n >= 500) {
.... long running code here
}
Run Code Online (Sandbox Code Playgroud)
如果突破某个函数,您可能只需要return()
显式或隐式.
例如,显式双返回
foo <- function(x) {
if(x < 10) {
return(NA)
} else {
xx <- seq_len(x)
xx <- cumsum(xx)
}
xx ## return(xx) is implied here
}
> foo(5)
[1] 0
> foo(10)
[1] 1 3 6 10 15 21 28 36 45 55
Run Code Online (Sandbox Code Playgroud)
通过return()
暗示,我的意思是最后一行就好像你已经完成了return(xx)
,但是稍微高效一点就不用了return()
.
有些人认为使用多种退货方式不好; 在长函数中,跟踪函数退出的位置可能变得困难或容易出错.因此,另一种方法是使用单个返回点,但使用该if () else ()
子句更改返回对象.这样的修改foo()
就可以了
foo <- function(x) {
## out is NA or cumsum(xx) depending on x
out <- if(x < 10) {
NA
} else {
xx <- seq_len(x)
cumsum(xx)
}
out ## return(out) is implied here
}
> foo(5)
[1] NA
> foo(10)
[1] 1 3 6 10 15 21 28 36 45 55
Run Code Online (Sandbox Code Playgroud)
这是一个老问题,但还没有一个干净的解决方案。这可能没有回答这个具体问题,但那些寻找“如何优雅地退出 R 脚本”答案的人可能会在这里找到答案。R 开发人员似乎忘记实现 exit() 函数。无论如何,我发现的技巧是:
continue <- TRUE
tryCatch({
# You do something here that needs to exit gracefully without error.
...
# We now say bye-bye
stop("exit")
}, error = function(e) {
if (e$message != "exit") {
# Your error message goes here. E.g.
stop(e)
}
continue <<-FALSE
})
if (continue) {
# Your code continues here
...
}
cat("done.\n")
Run Code Online (Sandbox Code Playgroud)
基本上,您使用标志来指示指定代码块是否继续。然后,您使用该stop()
函数将自定义消息传递给函数的错误处理程序tryCatch()
。如果错误处理程序收到您要正常退出的消息,那么它只会忽略该错误并将继续标志设置为FALSE
。
也许您只是想在某个时候停止执行长脚本。即。就像您想用C或Python硬编码一个exit()一样。
print("this is the last message")
stop()
print("you should not see this")
Run Code Online (Sandbox Code Playgroud)