Play a sound in RMarkdown notebook?

I am interested in making a Rmarkdown document knit to html that can play a sound when an image is clicked (could be a link but I do not want an external software to open). After plying the web, I am not sure it is possible but thought that I would ask in this friendly community.

Are you including the image as a static resource, or are you trying to generate the image with R?

I have not tried this (so apply grains of salt!), but part of the essential nature of Markdown is that it’s always legal (encouraged, even) to drop into HTML for features that Markdown itself can’t handle.

So if you can write HTML (+ CSS + JavaScript) that does what you want, you ought to be able to include that in the Markdown part of your R Markdown and have it work as expected when you knit to HTML. A few leads on the HTML/CSS/JavaScript front:

Note that sound generation in general is tricky in HTML and you are more likely to be successful if you are OK with only supporting very recent web browsers.


I had to play audio recently in a Shiny app, but it also works in RMarkdown. It uses the audio element linked by @jcblum above. I think its easier to play audio in a browser than looking for a way to play audio on all systems.

The pertinent code, adapted from here:

audio_player <- function(audio = "output.wav",
                         html = "player.html"){

    <audio controls autoplay>
                     <source src="%s">



Watch out for browser caching, as it will not update the audio file if you change it, to get around that I call the audio file a different name each iteration.

@jcblum that is what I suspected, unfortunately, I am not fluent in HTML, CSS, or JavaScript ( I am a biologist and R is really the only language that I program in).

I am planning to use R to generate the jpgs showing a spectrogram of the sound (~100 files) and have a caption describing each image generated from a data.frame and then when you click on the image/caption/play bar/button, it would play the sound.

@MarkeD This seems really close to what I am looking for! Thank you. Once I have an example that works I will post it here.