The graphics format SVG is essentially XML. You can generate SVG plots easily
with the knitr chunk option dev = 'svg' (or 'svglite' or other SVG
devices). However, they are embedded in the HTML output document with the
<img> tag, which means you cannot manipulate them even if they are XML. I have
wished to embed the raw XML for years, so that SVG plots can be manipulated. I
didn’t have a strong enough motivation, although I felt this could be
potentially useful.
In July last year, JooYoung Seo filed a feature request to knitr, and I finally got the motivation! For people who are visually impaired or blind, they could use JavaScript to interact with an SVG plot so that they can hear the information in the plot. I don’t really have much experience in this regard, but I’m sharing a simple example in this post to reveal the potential.
The key is to set the option options(knitr.svg.object = TRUE) to let knitr
embed SVG plots as raw XML in your HTML output document. Once an SVG plot is an
HTML node, you can select this node and its child nodes. Then you can use any
JavaScript technology to manipulate them.
Below is an R Markdown example that shows you how to raise a bar in a barplot and beep at the same time when you move your mouse over the bar.
---
title: "Manipulate SVG plots"
---
```{r setup, include=FALSE}
options(knitr.svg.object = TRUE)
```
Draw a barplot with an ID `my-plot` and the SVG format.
```{r dev='svg', out.extra='id="my-plot"'}
barplot(1:10)
```
Add `mouseover` events on all bars.
```{js}
document.querySelectorAll('#my-plot #surface4 path').forEach(el => {
// beep for 500 milliseconds
// code adapted from https://stackoverflow.com/a/29641185/559676
function beep() {
const ctx = new AudioContext();
var osc = ctx.createOscillator();
osc.type = 'sine';
osc.frequency.value = 800;
osc.connect(ctx.destination);
osc.start();
setTimeout(() => { osc.stop(); }, 100);
}
el.addEventListener('mouseover', e => {
this.event.target.style.transform = 'rotateX(15deg)';
beep();
});
el.addEventListener('mouseout', e => {
this.event.target.style.transform = '';
});
});
```
You can knit this document to HTML and move your mouse over each bar to see the effect, or just visit a published demo of mine. To hear the sound, you may need to click on the page at least once (because browsers may not allow for playing sounds without user interaction).
The JavaScript code document.querySelectorAll('#my-plot #surface4 path')
selects all bars in the plot. For each bar, the mouseover event will trigger a
beep and a CSS transform to raise the bar a little bit (rotateX(15deg)). For
the mouseout event, the transform is undone.
This feature of embedding SVG as XML in HTML output documents was first introduced in knitr 1.40 (released on Aug 24, 2022). I hope you will find it useful and fun. Special thanks to JooYoung for the motivation!
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).