给定一个包含3行的文件script.R:
print('hello') stop('user error') print('world')
我们可以运行它:
$R -f script.R > print('hello') [1] "hello" > stop('user error') Error: user error > print('world') [1] "world"
但在错误发生后仍然如此.我希望它在出错时暂停脚本.这样做:
$R -e "source('script.R')" > source('script.R') [1] "hello" Error in eval(expr,envir,enclos) : user error Calls: source -> withVisible -> eval -> eval
好.它停止在错误上运行脚本.但是shell的返回值是0(成功):
$echo $? 0
反正有没有运行脚本,停止错误,并返回非零到shell?我搜查了一下,找不到答案.
我可以grep输出文件中的文本“错误”,但这有一些需要管理的风险;例如以某种方式greping错误的输出文件并且两次或多次运行写入同一文件.可以管理这些问题,但是将非零返回到shell会更简单,更健壮.在脚本末尾添加一行也是一种解决方法,因为我需要将该行添加到所有脚本中.
感谢@Pascal的评论,结果发现在我的.Rprofile中我有:
options(error=quote(dump.frames()))
当我使用–vanilla运行以防止我的.Rprofile被加载:
$R --vanilla -f script.R > print('hello') [1] "hello" > stop('user error') Error: user error Execution halted $echo $? 1
这正是我想要的并解决问题.谢谢@Pascal!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。