2

I am trying to include a picture file from the internet in my R Markdown report. I am hoping to print this out on to a pdf file. However, whenever I try to run my code, I get a LaTeX file not found error. The picture exists on the web, I just can't seem to be able to bring it into my report.

My code:

```{r, out.width = "50%"}
include_graphics("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/MC_Drei-Finger-Faultier.jpg/330px-MC_Drei-Finger-Faultier.jpg")
```

My error is this: ! LaTeX Error: File `https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/ MC_Drei-Finger-Faultier.jpg/330px-MC_Drei-Finger-Faultier' not found.

The picture in question: https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/MC_Drei-Finger-Faultier.jpg/330px-MC_Drei-Finger-Faultier.jpg

I am using:

  • knitr v 1.28
  • rmarkdown v 2
  • on a mac
Alokin
  • 461
  • 1
  • 4
  • 22
  • This has happened to me when the image name has hyphens in it. To troubleshoot - does it work with an image path online with no hyphens? – Nova May 29 '20 at 15:12
  • No, I still get the same error when I removed the hyphens. – Alokin May 29 '20 at 15:17
  • haha, I mean find an image without hyphens in the path and try that :) - it won't solve your problem, but will help you figure out if that IS the problem! – Nova May 29 '20 at 15:48
  • I tried it with this link and still got the same error: ```{r, out.width = "50%"} include_graphics("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png") ``` – Alokin May 29 '20 at 15:55
  • According to [this post](https://www.r-bloggers.com/wrapper-of-knitrinclude_graphics-to-handle-urls-pdf-outputs/) `include_graphics` does not work with a URL when knitting to a pdf. – nniloc May 29 '20 at 17:03

1 Answers1

2

To display an image from a URL in a pdf, you have to first download the file. You may need a different mode depending on your OS.

```{r, out.width = "50%"} 
myurl <- "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/MC_Drei-Finger-Faultier.jpg/330px-MC_Drei-Finger-Faultier.jpg"

download.file(url = myurl, destfile = 'temp.jpg', mode = 'wb')

knitr::include_graphics('temp.jpg')
nniloc
  • 4,128
  • 2
  • 11
  • 22