R - Rstudio - 如果产生警告/错误,使 R 播放声音 [英] R - Rstudio - make R play a sound if warning/error generated
问题描述
我正在运行一个循环遍历股票对组合列表的脚本......偶尔脚本会因配对组合之间不同数据长度产生的错误而停止运行,我只是从考虑中删除不匹配的股票):>
I am running a script that loops over a list of stock pair combinations... occasionally the script stops running due to an error generated by differing data lengths between pair combo and I simply remove the mismatched stock from consideration):
Error in model.frame.default(formula = stckY ~ stckX + 0, drop.unused.levels = TRUE) :
variable lengths differ (found for 'stckX')
有什么方法可以让 R/Rstudio 在出现错误消息时播放声音,以便在脚本循环播放时无需将眼睛一直盯着屏幕就可以收到警报?
Is there any way I can make R / Rstudio play a sound when the error message occurs so that I can be alerted without having to keep my eyes on the screen while the script is looping along?
我可以使用以下方法线性生成声音:
I can generate sounds linearly using:
beep <- function(n = 3){
for(i in seq(n)){
system("rundll32 user32.dll,MessageBeep -1")
Sys.sleep(.5)
}
}
beep()
但是我如何以错误消息为条件执行此操作?
but how can I do this conditional on an error message?
推荐答案
基于@frankc 的回答和@hrbrmstr 的评论,一种方法:
Building on @frankc answer and @hrbrmstr comment, a way to do this:
install.packages("beepr")
library(beepr)
options(error = beep)
这篇关于R - Rstudio - 如果产生警告/错误,使 R 播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!