LaTeX PDF with images from web in shinyapp

爱⌒轻易说出口 提交于 2020-01-14 08:22:28

问题


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)
    }
)

回答1:


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")


来源:https://stackoverflow.com/questions/52652934/latex-pdf-with-images-from-web-in-shinyapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!