Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaTeX PDF with images from web in shinyapp

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)
    }
)
like image 462
Philipp R Avatar asked Oct 04 '18 18:10

Philipp R


1 Answers

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")
like image 173
Philipp R Avatar answered Sep 18 '22 12:09

Philipp R