JackieMium / my_blog

在 Issues 中建立的个人博客
GNU General Public License v3.0
31 stars 12 forks source link

让 R 在完成任务时发送通知或者叮一声 #10

Open JackieMium opened 6 years ago

JackieMium commented 6 years ago
2017-06-22

让 R 在完成任务时发送通知或者叮一声

Is there a way to make R beep/play a sound at the end of a script?

  1. Throw a Beep

    install.packages("beepr")
    library(beepr)
    beep()

    The package is developped at GitHub.

    Usage

    beep(sound = 1, expr = NULL)

    Arguments

    sound character string or number specifying what sound to be played by either specifying one of the built in sounds or specifying the path to a wav file. The default is 1. Possible sounds are:

    1. random

    2. "ping"

    3. "coin"

    4. "fanfare"

    5. "complete"

    6. "treasure"

    7. "ready"

    8. "shotgun"

    9. "mario"

    10. "wilhelm"

    11. "facebook"

    12. "sword"

  2. To get a message, you can use notify-send command:

    system("notify-send \"R script finished running\"")
JackieMium commented 6 years ago

~/.Rprofile 里添加:

notify <- function(){
    cmd <- "notify-send"
    system2(cmd, args="-i emblem-default 'R Message' 'Mission Complete!'")
}

beep <- function(){beepr::beep()}

这样以后执行需要长时间跑的命令时,如果你打算去打开网页看其他东西,直接在末尾放上 notify() 任务完成时桌面会弹出通知,如果你打算玩手机,beep() 会在任务完成是发出声音(在自习室或图书馆请插上耳机谢谢)。