1

I used to be able to render images in r-markdown using a URL with the following code ![](https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark) but I get a file not found error ! LaTeX Error: File https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark' not found. Am I missing packages? This code still works on some shiny apps published a few month ago.

Below the a working file r-markdown file:

---
title: "Test"
header-includes:
    - \usepackage{graphicx}
output:
  pdf_document:
    latex_engine: xelatex
    number_sections: yes
    keep_tex: yes
classoption: article
papersize: A4
fontsize: 10pt
geometry: margin=0.9in
linestretch: 1.15
---
## R Markdown
![](https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark)
Philipp R
  • 598
  • 6
  • 22
  • The image URL returns a 404. Append the missing `.png` and you should be golden. – tarleb Sep 28 '18 at 15:46
  • thanks for the quick response. R-markdown does not require the file type. Unfortunately, this does not make a difference. – Philipp R Sep 28 '18 at 16:02
  • I don't have TeX installed, so I can't test with pdf output, but at least with html output, adding the `.png` solves the problem. – divibisan Sep 28 '18 at 16:09
  • @PhilippR Ah yes, I can reproduce this. See for an answer. (Also, technically, it's only the graphicx package that doesn't require the file type.) – tarleb Sep 28 '18 at 17:17
  • Ok, that points me in the right direction. What strikes me is that I have code running that works well with the syntax above. I am looping through the images to download them for the report. Not sure how this will work in the cloud hosted shiny apps. – Philipp R Sep 28 '18 at 19:58

1 Answers1

3

The LaTeX graphicx package does not include an http client, it is therefore not able to pull the image from the internet. However, a lot of the conversion work from Markdown to LaTeX is performed by pandoc, which can get this image. One just needs to tell pandoc to store all images locally by passing the --extract-media option. This allows LaTeX to find the images when it is invoked by RMarkdown.

---
output:
  pdf_document:
    pandoc_args: ["--extract-media", "."]
---

The above will store all images in the same directory as the Rmd file. The files will be named using SHA1 hashes, so you might want to use a separate directory for these files instead.

tarleb
  • 19,863
  • 4
  • 51
  • 80
  • 1
    Thanks, that works great for local files, but does this work in _shinyapps_? Has this been a default setting in previous _pandoc_ installations? I am still trying to understand how my older apps are still functioning without the option in ordinary _rmarkdown->xelatex_. – Philipp R Oct 01 '18 at 21:43
  • I'm not sure, but I suspect that this used to work because pandoc directly created the PDF, while R Markdown will now use pandoc to generate LaTeX and do the final invocations of XeLaTeX by itself. The release notes of R Markdown 1.1 (~ 2016) mention changes to the way `keep_tex` works, which might have caused the change in behavior. – tarleb Oct 03 '18 at 13:43
  • Thanks. I am generating pdf's in `xelatex` and while this works locally I cannot get the images to work in my shiny app. I am using [this approach](https://stackoverflow.com/questions/35800883/using-image-in-r-markdown-report-downloaded-from-shiny-app) to generate the pdf from shiny app data. How to reference the working directory of the `extracted-media` in shiny? – Philipp R Oct 04 '18 at 15:30
  • I know next to nothing about shiny, sorry. Might be worth a new question? – tarleb Oct 04 '18 at 15:44
  • 1
    New question regarding web images in LaTex PDF in Shinyapps posted [here](https://stackoverflow.com/questions/52652934/latex-pdf-with-images-from-web-in-shinyapp). – Philipp R Oct 04 '18 at 18:08