7

I used to be able to include images from URLs in PDF reports generated from shiny apps doing ![](url.com). A few markdown versions later I get the following error: ! Unable to load picture or PDF file https://i0.wp.com/wptavern.com/wp-content/uploads/2016/07/stack-overflow.png?ssl=1 for the same code. Adding pandoc_args: ["--extract-media", "."] to the YAML downloads the imaged file locally but only works in local r-markdown files.

  • How does shinyapp store local files and how to get the extract-media workaround to function?
  • How to include web images in PDF's in shinyapps?

r-markdown example


title: "Test"
header-includes:
    - \usepackage{graphicx}
    - \usepackage{hyperref}
output:
  pdf_document:
    latex_engine: xelatex
    pandoc_args: ["--extract-media","."]
    number_sections: yes
    keep_tex: yes
classoption: article
papersize: A4
fontsize: 10pt
geometry: margin=0.9in
linestretch: 1.15
---
## R Markdown
![click](https://i0.wp.com/wptavern.com/wp-content/uploads/2016/07/stack-overflow.png?ssl=1)

server.R chunk triggering report generation

## img report
output$downloadImgReport <- downloadHandler(
    filename = function() {
        paste0(format(Sys.time(), '%Y%m%d'),'-WS-CM-image-report-',docounts()$count, '.pdf')
    },
    content = function(file) {
        src <- normalizePath('Untitled.Rmd')
        src1 <- normalizePath('logo.png')
        owd <- setwd(tempdir())
        on.exit(setwd(owd))
        file.copy(src, 'Untitled.Rmd', overwrite = TRUE)
        file.copy(src1,'logo.png')
        library(rmarkdown)
        out <- render('Untitled.Rmd', output_format=pdf_document(latex_engine = "xelatex"))
        writetolog(1,session$token)
        file.rename(out, file)
    }
)
Community
  • 1
  • 1
Philipp R
  • 598
  • 6
  • 22
  • The error appears to come from XeLaTeX, maybe try to update it? At least the following works for me, though: `echo '![](https://i0.wp.com/wptavern.com/wp-content/uploads/2016/07/stack-overflow.png)' | pandoc --pdf-engine=xelatex -o foo.pdf` – mb21 Oct 05 '18 at 10:31
  • I updated all packages including Latex without any success. – Philipp R Oct 08 '18 at 16:10
  • but calling pandoc directly works? like I posted above? – mb21 Oct 08 '18 at 16:32
  • Thanks, so in a shiny environment pandoc is not called directly? Is there a way to force this? – Philipp R Oct 08 '18 at 16:35
  • I don't really know shiny... what I meant is: what happens if you execute `echo '![](https://i0.wp.com/wptavern.com/wp-content/uploads/2016/07/stack-overflow.png)' | pandoc --pdf-engine=xelatex -o foo.pdf` on the commandline? – mb21 Oct 08 '18 at 16:56
  • OK, this returns a pdf with the image from the URL. – Philipp R Oct 08 '18 at 20:27
  • 1
    With the latest version of rmarkdown, you have to download such images (e.g. via `download.file()`) and use local image paths when generating PDF. – Yihui Xie Oct 09 '18 at 04:15
  • Thanks @YihuiXie! What is the last version of rmarkdown that automatically downloads the file? – Philipp R Oct 09 '18 at 08:17
  • 1
    I'm sure 1.8 will do it. 1.9 might do it, but I'm not entirely sure (need to check the git log). – Yihui Xie Oct 09 '18 at 15:19

1 Answers1

6

The latest version of rmarkdown requires images to be downloaded locally. Adding pandoc_args: ["--extract-media","."] to the YAML header works for local rmarkdown files but not in a shiny app environment.

Downgrading rmarkdown below version 1.9 will enable images to be automatically downloaded.

Alternatively, files can be downloaded locally using download.file() and reference with an absolute path.

title: "Test"
header-includes:
    - \usepackage{graphicx}
    - \usepackage{hyperref}
output:
  pdf_document:
    latex_engine: xelatex
    pandoc_args: ["--extract-media","."]
    number_sections: yes
    keep_tex: yes
classoption: article
papersize: A4
fontsize: 10pt
geometry: margin=0.9in
linestretch: 1.15
---
## R Markdown
download.file(url = "https://i0.wp.com/wptavern.com/wp-content/uploads/2016/07/stack-overflow.png?ssl=1"), destfile = "stack-overflow.png")
![click]("stack-overflow.png")
Philipp R
  • 598
  • 6
  • 22