如果遇到错误,如何让 R 发送警报?

How can I make R send an alert if it encounters an error?

我目前喜欢在我的 R 脚本完成时收到电子邮件通知 运行,感谢 mail 包。

但是,我想知道如果脚本因遇到错误而无法完成 运行,是否可以让 R(或 RStudio 或其他软件)通知我?电子邮件将是最好的传送方式。

当我 运行 在远程机器上编写代码,或者当我不在计算机旁时,这将非常有用。

获取推送消息:http://cran.r-project.org/web/packages/RPushbullet/index.html

或给自己发送电子邮件:http://cran.r-project.org/web/packages/mailR/

并使用 tryCatch...

a <- tryCatch({
    source("your_script.R")
}, warning = function(w) {
    warning-handler-code
}, error = function(e) {
    send mail, pushbullet, etc...
}, finally = {
    cleanup
}