我想知道在R中是否有任何关键字跳出函数而不执行其余的.在C,Java或Matlab中,有关键字'return'.但是R中的'return'关键字与那些语言中的'return'关键字不同.这是一个例子,
myfunc = function() {
if (TRUE) {
return # hopefully, jump out of the function
}
print('the rest of the function is still executed!')
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,"返回"时,遇到了像Java语言将不会执行"休息",而在R"回报"只是的范围是否仍执行的语句和功能的其余部分.在这个特定的例子中,我可以添加一个'else'块来实现它,但我想知道是否有任何关键字会产生与Java等相似的行为.谢谢.
什么告诉你其实在语法上是有效的R代码里面......但你的错误不提供值来return.所以这是一个更正版本:
R> myfunc <- function() {
if (TRUE) {
return(NULL) # hopefully, jump out of the function
}
print('the rest of the function is still executed!')
}
myfunc <- function() {
+ if (TRUE) {
+ return(NULL) # hopefully, jump out of the function
+ }
+ print('the rest of the function is still executed!')
+ }
R> myfunc()
NULL
R>
Run Code Online (Sandbox Code Playgroud)