使用R中的另一个函数停止功能评估

Mur*_*rta 4 evaluation r

return在R中使用嵌套函数进行了测试,但没有成功.我来自Mathematica,这段代码效果很好.这是一个玩具代码:

fstop <- function(x){
  if(x>0) return(return("Positive Number"))
}

f <- function(x){
  fstop(x)
  "Negative or Zero Number"
}
Run Code Online (Sandbox Code Playgroud)

如果我评估f(1),我得到:

[1] "Negative or Zero Number"
Run Code Online (Sandbox Code Playgroud)

当我期待的时候:

[1] "Positive Number"
Run Code Online (Sandbox Code Playgroud)

问题是:我可以做一些非标准的评估fstop,所以我可以得到fstop结果,没有改变f功能?

PS:我知道我可以把if直接放在里面f,但在我的实际情况下,结构并不那么简单,这种结构会使我的代码更简单.

Spa*_*man 7

要伸出我的脖子说...

没有.

使函数不返回其调用者但返回其调用者的调用者将涉及更改其执行上下文.这就是return源代码中实现类似事物和其他控制流程的方式.看到:

https://github.com/wch/r-source/blob/trunk/src/main/context.c

现在,我不认为R级代码可以访问这样的执行上下文.也许你可以写一些可以做到的C级代码,但不清楚.你总是可以写do_return_return在样式功能do_returneval.c和构建R的定制版本...它不值得.

所以答案很可能是"不".