RMarkdown :!软件包pdftex.def错误;找不到在线图片?

时间:2018-07-11 14:11:23

标签: r r-markdown knitr

尝试在R Markdown PDF文档中包含基于Web的图像时遇到问题。

最小示例

---
title: "Random"
output: pdf_document
---

![Benjamin Bannekat](https://octodex.github.com/images/bannekat.png)

编织以上内容会导致错误:

  

!软件包pdftex.def错误:文件`https://octodex.github.com/images/bannekat.pn       找不到g。

但是,如果我使用以下代码,则会显示该图像:

---
title: "Random"
output:
  html_document: default
  html_notebook: default
---

![Benjamin Bannekat](https://octodex.github.com/images/bannekat.png)

当输出为HTML时,相同的代码可以正常工作。

如何使图像显示在PDF文档中?

1 个答案:

答案 0 :(得分:2)

如果要编织为PDF,则文件必须通过LaTeX运行。 LaTeX没有内置功能来显示网络上托管的文件,如突出显示的in this question

最好的解决方法是将网络图像下载到本地目录,然后将其指定为文件路径。

以下代码使用download.file函数下载图像,然后使用include_graphics显示。包含图形的好处在于,它允许您指定输出文档中图像的宽度,我将其设置为40像素。

---
title: "Random"
output: pdf_document
---


```{r results = 'asis', out.width="40px"}
download.file(url = "https://octodex.github.com/images/bannekat.png",
          destfile = "image.png",
          mode = 'wb')
knitr::include_graphics(path = "image.png")
```

enter image description here

  

找出为什么应使用include_graphics在R Markdown文档中包括图形的更多原因。查看my other answer here,了解更多原因。

相关问题