当我尝试在 R Markdown PDF 文档中包含基于 Web 的图像时遇到问题。
最小示例:
---
title: "Random"
output: pdf_document
---
![Benjamin Bannekat](https://octodex.github.com/images/bannekat.png)
编织上述结果会出现错误:
! Package pdftex.def Error: File `https://octodex.github.com/images/bannekat.pn g' not found.
但是,如果我使用以下代码,则会显示图像:
---
title: "Random"
output:
html_document: default
html_notebook: default
---
![Benjamin Bannekat](https://octodex.github.com/images/bannekat.png)
当输出为 HTML 时,相同的代码可以正常工作。
如何让图像显示在 PDF 文档中?
最佳答案
如果您要编织为 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")
```
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.
关于RMarkdown : ! 包 pdftex.def 错误;找不到在线图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51287734/