在 R Markdown 中将 SVG 代码转换为图形

Convert SVG code to graphic in 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 blob 的 SVG。

编辑:Stéphane Laurent 的解决方案(如下)有效。我没有解释这个 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()

Limey 所述,您可以使用 rsvg 包转换为 png .但是你可以简单地在 Rmarkdown 文件中粘贴 svg 标签,质量优于 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>

注意: 代码不要缩进,否则会渲染成代码块