RMarkdown: ! Package pdftex.def Error; online image not found?

邮差的信 提交于 2019-12-18 09:16:17

问题


I am running into a problem when trying to include a web-based image within a R Markdown PDF document.

Minimal Example:

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

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

Knitting the above results in the error:

! Package pdftex.def Error: File `https://octodex.github.com/images/bannekat.pn g' not found.

However, if I use the following code, the image shows up:

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

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

The same code works fine when output is HTML.

How can I make the image show up in a PDF document?


回答1:


If you are knitting to PDF, the file has to run through LaTeX. LaTeX has no built-in capacity to display files hosted on the web, as highlighted in this question.

The best workaround for this is to download the web image to your local directory, and then specify this as a file path.

The following code downloads the image using the download.file function, and then displays in using include_graphics. The benefit of include graphics is that it allows you to specify the width of the image in the output document, which I set to 40 pixels.

---
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")
```

Find out more reasons why include_graphics should be used to include graphics in R Markdown documents. Check out my other answer here for more details why.



来源:https://stackoverflow.com/questions/51287734/rmarkdown-package-pdftex-def-error-online-image-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!