Convert SVG code to graphic in R Markdown

Is there anyway to take code such as the following, which produces a simple circle, and produce the resulting image when knitted to HTML?

<svg height="100" width="100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="#757575" /></svg>

For reference as to why I have code rather than images, this SVG is extract from JSON blobs as text. If I save it to a text file with .svg extension, it can open fine. The end result of this code would be a shiny app that displays the SVG from various JSON blobs.

>Solution :

As mentionned by Limey, you can use the rsvg package to convert to png. But you can simply paste the svg tag in the Rmarkdown file, quality is better than png:

---
title: "Untitled"
author: "St├ęphane Laurent"
date: '2022-05-20'
output: html_document
---

<svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="#757575" /></svg>

Note: Do not indent the code, otherwise it will be rendered as a code block.

enter image description here

Leave a Reply