将SVG代码转换为R Markdown中的图形
无论如何,是否可以使用以下代码,例如产生一个简单的圆圈,并在编织到HTML时产生结果图像?
<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="#757575" /></svg>
对于为什么我有代码而不是图像的原因,此SVG是从JSON BLOB作为文本提取的。如果我将其保存到带有.svg扩展名的文本文件中,则可以打开罚款。该代码的最终结果将是一个闪亮的应用程序,可显示各种JSON BLOBS的SVG。
编辑:StéphaneLaurent的解决方案(下)工作。我没有解释说,该SVG将在数据框架内,这在渲染中提出了另一个挑战。但是,他们的回答使我做到了:
```{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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 limey 所提到的那样,您可以使用 rsvg 软件包将其转换为 png 。但是您可以简单地将 svg 标记粘贴到rmarkDown文件中,质量比 png :
: 不要缩进代码,否则将其作为代码块渲染。
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:
Note: Do not indent the code, otherwise it will be rendered as a code block.