Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading