我写了一个R脚本,它将消息(进度报告)写入文本文件.我修改了该error选项,以便在发生错误时,错误消息也会写入该文件:
options(error = function() {
cat(geterrmessage(),file = normalizePath("logs/messages.txt"),append = TRUE)
stop()
})
Run Code Online (Sandbox Code Playgroud)
它可以工作,但是当发生错误时我在控制台/终端窗口中收到此消息:
Error during wrapup:
Execution halted
Run Code Online (Sandbox Code Playgroud)
所以我认为有一种更好的方法来中断脚本的执行......或者在那里?
我刚刚在R源代码中找到了这个:
if (inError) {
/* fail-safe handler for recursive errors */
if(inError == 3) {
/* Can REprintf generate an error? If so we should guard for it */
REprintf(_("Error during wrapup: "));
/* this does NOT try to print the call since that could
cause a cascade of error calls */
Rvsnprintf(errbuf, sizeof(errbuf), format, ap);
REprintf("%s\n", errbuf);
}
Run Code Online (Sandbox Code Playgroud)
stop()导致错误处理程序被执行.如果stop()调用发生在错误处理程序中,则R显示该Error during wrapup:消息并阻止您发生否则将发生的无限递归.
不要stop()从你的内心打电话options$error.
q(save="no", status=1, runLast=FALSE)相反,应该使用默认错误处理程序执行非交互式使用的操作.见?options的意义options$error和?stop有关错误处理的详细信息.