Search code examples
rrscriptmacos-high-sierra

no sound from beepr in Rscript


I am trying to use beepr in my Rscripts to tell me when they are done.

But the beep never comes.

here is an example script saved as test.R:

#!/usr/bin/env Rscript

library(beepr)

beep(1)
cat('got here')

Then from the terminal I run

Rscript test.R

I get the printed output, but no sound!

I am running OSX with High Sierra.

If i run with alarm() instead of beep(), I do get a noise (but not the cool sounds beepr has)

Thanks.


Solution

  • The reason for this behaviour is because the sound played by beep() does not block the session, but is played by the session, and as soon as the session is over the sound stops. If you run this in an R session then the session continues, and so the sound is played, but if you run this with Rscript the session exits thus immediately cutting the sound short.

    A workaround is to delay the closing of the session allowing the sound to finish playing:

    #!/usr/bin/env Rscript
    beepr::beep()
    Sys.sleep(3) # Waits for 3 seconds
    cat('got here')