【问题标题】:r markdown preserve xlink in svgr markdown 在 svg 中保留 xlink
【发布时间】: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">
      &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>

【问题讨论】:

    标签: html svg r-markdown knitr xlink


    【解决方案1】:

    您可以使用htmltools::includeHTML 包含您的svg 文件:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 2017-03-25
      相关资源
      最近更新 更多