Not able to get an image into word/pdf from URL in RMarkdown

Hi,

I am trying to get an image from an URL in RMarkdown. The code which I have used is:

![](URL)

But I am getting this error:

Error: pandoc document conversion failed with error 61

Did anyone come across this error? If no, can anyone let me know if there is any other way to get a picture into pdf from an URL using RMarkdown?

I am able to get the image in html after using "self_contained: no" (see below) in the output.

title: "Test Document"
output: 
  html_document:
    self_contained: no

But I am not able to get the image in word, it's showing blank for the below output.

title: "Test Document"
output: word_document

Does anyone know how to resolve this? or is there any other alternative way?

Can you please share the code you're using to add the image?

1 Like

Hi @bharath222! This is a really good example of why it’s important to properly format code you post here (How? See: FAQ: How to format your code).

Your code was invisible because this forum also uses Markdown to format posts, so the forum software was actually trying to run your code! I edited the original post to properly format the code, so now it shows up.

If you’re still getting an error when you try to knit to word_document, it might help to include more of the output from the R Markdown console (formatted as code, please!)

3 Likes
---
title: "test"
author: "Bharath Sepuri"
date: "10/7/2018"
output: 
  html_document:
    self_contained: no
---

```{r, echo=FALSE}
# Define variable containing url
url <- "http://www.online-image-editor.com//styles/2014/images/example_image.png"
```

![](`r url`)

I have also tried in another way (see below) but still not able to get the output in word/pdf. This is the error which I am getting:

Error: Could not fetch resource 'http://www.online-image-editor.com//styles/2014/images/example_image.png': PandocHttpError

title: "test"
author: "Bharath Sepuri"
date: "10/7/2018"
output: 
  html_document:
    self_contained: no
---

```{r, echo=FALSE}

```

![](http://www.online-image-editor.com//styles/2014/images/example_image.png)


Hi,

Thank you very much for letting me know about formatting. It was very helpful.

1 Like

So I can sort of confirm this, but I get a slightly different result from what you report.

Here's the source of my test Rmd file:

---
title: "Foo"
output:
  pdf_document: default
  word_document: default
  html_document: default
---

![](http://www.online-image-editor.com//styles/2014/images/example_image.png)

```{r}
# Define variable containing url
url <- "http://www.online-image-editor.com//styles/2014/images/example_image.png"
```

![](`r url`)

Since knitting to PDF can involve some tricky external system dependencies (i.e., LaTeX), I tested this on RStudio Cloud so that at least the setup isn't idiosyncratic to my specific machine and its checkered past! (I got the same results on my own machine though). Here's a link to a shared project, if you want to play with it there: https://rstudio.cloud/project/103903

I can knit to word_document and default (self-contained) html_document just fine with http://-linked images (both outputs contain two adorable kittens :heart_eyes_cat:). You can inspect/download the successfully output HTML and Word files from the Files pane of the Cloud project.

Knitting to pdf_document fails with the error:

! Package pdftex.def Error: File `http://www.online-image-editor.com//styles/20
14/images/example_image.png' not found.

I've been searching around a bit, and I can't really confirm that it's supposed to be possible to use http:// URLs in image links when the output is PDF — the pandoc documentation only has a local path example. I'm reaching the end of my understanding of the intricacies of the handoff between rmarkdown, knitr, and pandoc here, so I hope somebody else will come along and shed some more light.

1 Like

I know this post is late, but as of July 2019 this doesn't seem to have a nice conclusion (here or stack exchange).

Below is an approach that I found to work for pdf, html, and word documents.

---
title: "bar"
output:
  html_document: default
  pdf_document: default
  word_document: default
---

```{r, echo=FALSE, message=F, warning=F, fig.align="center"}
# Define variable containing url
url <- "http://www.online-image-editor.com//styles/2014/images/example_image.png"
library(png)
library(RCurl)
url_cont <- getURLContent(url)
img <- readPNG(url_cont)
rimg <- as.raster(img) # raster multilayer object
r <- nrow(rimg) / ncol(rimg) # image ratio
plot(rimg)
```

Also, just for people googling this problem - this was a necessary approach for me when I was using the assignr package.

1 Like