Play a sound at the end of R script in Windows command line

In my post about favorite RStudio tips and tricks, I mentioned sounds that could be played during R script execution. It works great but doesn’t make a sound after running R script from the Windows command line. Here is how to do that by using PowerShell and adding additional lines in the same batch file.

Play beep with the help of PowerShell

With the help of two parameters in the brackets, modify the frequency and duration of the beep sound.

PowerShell [System.Console]::Beep(300, 1000)

Try to combine beeps and compose some sort of melody like this.

PowerShell [System.Console]::Beep(300, 500); [System.Console]::Beep(300, 300); [System.Console]::Beep(300, 900)

There is possible that after R script execution, you can play .wav files with System.Media.SoundPlayer.

PowerShell -C (New-Object System.Media.SoundPlayer "C:\Windows\Media\tada.wav").PlaySync()

Readout loud the text after R script is executed

That can be done with PowerShell by using System.Speech.Synthesis.SpeechSynthesizer.

PowerShell -C Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('R script test.R execution finished!')

It is interesting how speech synthesizer works. Only if I write R file name without capital letter at the beginning, it is pronounced properly.

Choose one of these sound solutions and add next to the R script execution in the Windows batch file or command line.

Play sound or speech after R script execution in RStudio

If you want to run R script in RStudio and play sound or speech from there, you can do that by running PowerShell script in a separate batch file. Check out this post to learn how.





Leave a Reply

Your email address will not be published. Required fields are marked *