描述
我正在处理一些批处理脚本,这些脚本将执行一些R脚本.我希望全局更改R错误的处理方式,以使错误以彩色显示.在这个过程中,我遇到了一个非常奇怪的错误.我使用了options()
命令来更改错误的处理方式.奇怪的是,当文件来源于RStudio时,新的错误表达式会像预期的那样停止脚本的进一步执行,但当从命令提示符调用脚本时,自定义错误doesn't会停止执行.
我有以下R脚本:
# set global error options
options(error =function(){
cat(" \033[97;101m There was an error! \033[0m\n")
})
# apples is undefined so it should return an error
print(apples)
# because the command above produces an error, this shouldn't print:
print("bananas")
当我从R内部编写脚本时,脚本会像预期的那样在print(apples)
停止执行.
然而,当我try 在Windows命令提示符中使用R可执行文件执行脚本时,脚本并没有像它应该执行的那样在print(apples)
停止执行,相反,它继续运行并执行print("bananas")
我认为这与自定义错误有关,因为当我将R脚本更改为使用默认的错误表达式时,命令提示符将如预期的那样在print(apples)
停止执行.以下是修改后的脚本:
# set global error options
options(error = NULL)
# apples is undefined so it should return an error
print(apples)
# because the command above produces an error, this shouldn't print:
print("bananas")
问题
- 为什么会发生这种事?
- 在从命令提示符执行R脚本时,如何确保自定义错误表达式实际停止R脚本的执行?
- 有没有更好的方法使R错误在命令提示符窗口中以彩色显示?也许是一些相当于
colorout
套餐的窗口? - 如果这是一个漏洞,它是谁的错误,以及关于在哪里报告它的任何 idea ?
我不想使用trycatch
,因为我想全局更改所有可能的错误消息的显示 colored颜色 ,而不仅仅是特定的代码块.我希望能够从批处理脚本内部和RStudio内部运行这些R脚本,而不会因为错误而完全结束R会话,只是进一步执行脚本.