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

发布于 2025-01-31 05:45:12 字数 846 浏览 0 评论 0原文

无论如何,是否可以使用以下代码,例如产生一个简单的圆圈,并在编织到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 BLOBS的SVG。

编辑:StéphaneLaurent的解决方案(下)工作。我没有解释说,该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()

Is there anyway to take code such as the following, which produces a simple circle, and produce the resulting image when knitted to HTML?

<svg height="100" width="100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="#757575" /></svg>

For reference as to why I have code rather than images, this SVG is extract from JSON blobs as text. If I save it to a text file with .svg extension, it can open fine. The end result of this code would be a shiny app that displays the SVG from various JSON blobs.

EDIT: Stéphane Laurent's solution (below) worked. I failed to explain that this SVG would be inside a data frame, which presented another challenge in rendering. However, their answer led me to this:

```{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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘人皮目录ツ 2025-02-07 05:45:12

正如 limey 所提到的那样,您可以使用 rsvg 软件包将其转换为 png 。但是您可以简单地将 svg 标记粘贴到rmarkDown文件中,质量比 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>

不要缩进代码,否则将其作为代码块渲染。

As mentionned by Limey, you can use the rsvg package to convert to png. But you can simply paste the svg tag in the Rmarkdown file, quality is better than 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>

Note: Do not indent the code, otherwise it will be rendered as a code block.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文