如何通过for循环在Rmarkdown中显示绘图图像? [英] How to display plot images in Rmarkdown through a for loop?

查看:49
本文介绍了如何通过for循环在Rmarkdown中显示绘图图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理要打印到 html 文档的 svg 列表.我正在使用 magick 包,认为我愿意使用其他包/解决方案.下面的代码是我尝试呈现我的 html 文档.但是,它不会将 svg 呈现到 html 文件,而是简单地将元数据打印到文档中.有没有办法克服这种行为?

I am handling a list of svg to print to a html document. I am using the magick package thought I am open to using other packages/solutions. The below code is my attempt to render my html document. However, instead of rendering the svg to the html file, it simply prints the metadeta into the document. Is there a way to overcome this behavior?

---
title: "My Test Report"
author: "Nicholas Hayden"
date: "6/12/2018"
output: html_document
---

```{r setup, set-options, include=FALSE, warning=FALSE}
library(rsvg)
library(magick)

paths <- c(
"~/path/to/my1.svg",
"~/path/to/my2.svg")

for (i in 1:length(paths)) {
  print(image_read_svg(paths[i]))
}
```

Rmd 命令:

![]("~/path/to/my1.svg") 失败并产生...

File "/Users/nicholashayden/Desktop/path/to/my1.svg" not found in resource path
Error: pandoc document conversion failed with error 99

正如@mikeck 指出的那样.这可能是路径问题.但是,在没有 ~ 的情况下尝试会产生相同的错误.

As @mikeck pointed out. This may be a pathing issue. However, trying without the ~ produces the same error.

<img src="/Users/nicholahayden/path/to/my1.svg">的html纯文本将图片放入html文档中,而~ 版本失败,错误与前一个相同.

The html plain text of <img src="/Users/nicholashayden/path/to/my1.svg"> places the image in the html document while the ~ version fails with the same error as the previous.

knitr::include_graphics() 方法产生带有绝对路径或带有 ~

The knitr::include_graphics() method produces whitespace with either the absolute path or the path with the ~

推荐答案

我想你想使用 include_graphics:

```{r}
for (i in 1:length(paths)) {
  print(knitr::include_graphics(paths[i]))
}
```

这篇关于如何通过for循环在Rmarkdown中显示绘图图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆