我正在嘗試將 a 編織rmd到html包含一個svg檔案并保留其可點擊鏈接(xlink/ <a>ref),但似乎將 aknitr轉換或嵌入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>
uj5u.com熱心網友回復:
您可以svg使用以下方式包含您的檔案htmltools::includeHTML:
---
output: html_document
---
```{r}
htmltools::includeHTML("testsvglink.svg")
```

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/410412.html
標籤:
