【发布时间】:2022-01-13 18:40:19
【问题描述】:
我正在尝试将 rmd 编织到 html,其中包含一个 svg 文件并保留其可点击链接 (xlink / <a> ref),但似乎 knitr 转换或嵌入svg 作为<img>,丢失或停用链接。我尝试了三种方式加载 svg:
- 直接复制svg代码
- 使用 markdown 图片加载语法
- 使用
knitr::include_graphics
没有按预期工作。首选方法是 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">
<circle>
</text>
</a>
</svg>
# Use markdown to load SVG places svg inside <img>

# 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">
<circle>
</text>
</a>
</svg>
【问题讨论】:
标签: html svg r-markdown knitr xlink