停止R程序没有错误 [英] Stop an R program without error
问题描述
有没有办法停止R程序没有错误?
例如,我有一个很大的来源,定义了几个函数,之后有一些调用函数。这发生在我编辑一些函数,并希望在R环境中更新函数定义,但实际上并没有被调用。
我定义了一个变量 justUpdate
,当它是 TRUE
想在函数定义之后停止程序。
ReadInput< - function(...)...
pre>
分析< - function(...)...
WriteOutput< - function(...)...
if(justUpdate)
stop )
#主体
x< - ReadInput()
y< - Analyze(x)
WriteOutput(y)
我已经调用了
stop()
函数,但问题是它打印错误消息。 p>
ctrl + c 是另一个选项,但我想停止特定行的源。
q()
或quit()
的问题是它终止R会话,但我想让R会话仍然打开。
由于@JoshuaUlrich提出
browser()
可以是另一个选择,但仍然不完美,因为源终止于一个新的环境(即R提示将更改为浏览器[1]>
而不是>
)。仍然我们可以按Q
退出,但我正在寻找直截了当的方式。
另一个选择是使用
if(!justUpdate){main body}
但是它解决了问题,而不是解决它。
是否任何更好的选择?
解决方案您正在寻找功能
浏览器
。Is there any way to stop an R program without error?
For example I have a big source, defining several functions and after it there are some calls to the functions. It happens that I edit some function, and want the function definitions to be updated in R environment, but they are not actually called.
I defined a variable
justUpdate
and when it isTRUE
want to stop the program just after function definitions.ReadInput <- function(...) ... Analyze <- function(...) ... WriteOutput <- function(...) ... if (justUpdate) stop() # main body x <- ReadInput() y <- Analyze(x) WriteOutput(y)
I have called
stop()
function, but the problem is that it prints an error message.ctrl+c is another option, but I want to stop the source in specific line.
The problem with
q()
orquit()
is that it terminates R session, but I would like to have the R session still open.As @JoshuaUlrich proposed
browser()
can be another option, but still not perfect, because the source terminates in a new environment (i.e. the R prompt will change toBrowser[1]>
rather than>
). Still we can pressQ
to quit it, but I am looking for the straightforward way.Another option is to use
if (! justUpdate) { main body }
but it's clearing the problem, not solving it.Is there any better option?
解决方案You're looking for the function
browser
.这篇关于停止R程序没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!