r markdown 在 svg 中保留 xlink

r markdown preserve xlink in svg

我正在尝试编织一个 rmdhtml,其中包含一个 svg 文件并保留其可点击的 link (xlink / <a> ref),但似乎 knitrsvg 转换或嵌入为 <img>,丢失或停用了 link。我试过三种加载 svg 的方法:

None 按预期工作。首选方法是 2 或 3(加载 svg 文件)。

这是一个最小的 rmd 示例:

---
title: "Untitled"
output: html_document
---

# directly embed svg tag does not properly display the circle

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <!-- A link around a shape -->
  <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/a">
    <circle cx="50" cy="40" r="35"/>
  </a>

  <!-- A link around a text -->
  <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/a">
    <text x="50" y="90" text-anchor="middle">
      &lt;circle&gt;
    </text>
  </a>
</svg>


# Use markdown to load SVG places svg inside <img>

![](testsvglink.svg)

# use r knitr::include_graphics places svg inside <img>

```{r}
knitr::include_graphics("testsvglink.svg")
```

和文件 testsvglink.svg:

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <!-- A link around a shape -->
  <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/a">
    <circle cx="50" cy="40" r="35"/>
  </a>

  <!-- A link around a text -->
  <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/a">
    <text x="50" y="90" text-anchor="middle">
      &lt;circle&gt;
    </text>
  </a>
</svg>

您可以使用 htmltools::includeHTML:

包含您的 svg 文件
---
output: html_document
---

```{r}
htmltools::includeHTML("testsvglink.svg")
```