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.
EDIT: Stéphane Laurent's solution (below) worked. I failed to explain that this SVG would be inside a data frame, which presented another challenge in rendering. However, their answer led me to this:
```{r}
tribble(~id, ~svg,
1, '<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="#757575" /></svg>',
2, '<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="#757575" /></svg>') %>%
knitr::kable(escape = F) %>%
kableExtra::kable_styling()
CodePudding user response:
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.