无错误地停止 R 程序 [英] Stop an R program without error
问题描述
有什么办法可以不出错地停止 R 程序吗?
Is there any way to stop an R program without error?
例如,我有一个很大的源,定义了几个函数,然后有一些对函数的调用.碰巧我编辑了一些函数,并希望在 R 环境中更新函数定义,但实际上并没有调用它们.
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.
我定义了一个变量 justUpdate
并且当它是 TRUE
想在函数定义后立即停止程序.
I defined a variable justUpdate
and when it is TRUE
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)
我已经调用了 stop()
函数,但问题是它打印了一条错误消息.
I have called stop()
function, but the problem is that it prints an error message.
ctrl+c 是另一种选择,但我想在特定行中停止源.
ctrl+c is another option, but I want to stop the source in specific line.
q()
或 quit()
的问题在于它终止了 R 会话,但我希望 R 会话仍然打开.
The problem with q()
or quit()
is that it terminates R session, but I would like to have the R session still open.
正如@JoshuaUlrich 建议的 browser()
可以是另一种选择,但仍然不完美,因为源在新环境中终止(即 R 提示将更改为 Browser[1]>
而不是 >
).我们仍然可以按 Q
退出它,但我正在寻找直接的方法.
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 to Browser[1]>
rather than >
). Still we can press Q
to quit it, but I am looking for the straightforward way.
另一种选择是使用 if (!justUpdate) { main body }
但它是在清除问题,而不是解决问题.
Another option is to use if (! justUpdate) { main body }
but it's clearing the problem, not solving it.
还有更好的选择吗?
推荐答案
您正在寻找 browser
功能.
这篇关于无错误地停止 R 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!