【问题标题】:Shiny / RMarkdown: External picture not found [duplicate]Shiny / RMarkdown:找不到外部图片[重复]
【发布时间】:2018-12-04 11:28:21
【问题描述】:

我有一个 Shiny 应用程序,它允许通过 downloadHandler() 创建 PDF 报告。我在 .Rmd 文件中包含一个外部图像文件。当我单独编织 .Rmd 文件时,会显示此图像。当我通过 Shiny 应用程序(调用 .Rmd 文件)生成 PDF 报告时,找不到图像并且无法生成报告。我做错了什么?

Shiny 应用的相关部分:

  output$report <- downloadHandler(
    filename = "report.pdf",
    content = function(file) {
      # Copy the report file to a temporary directory before processing it, in
      # case we don't have write permissions to the current working dir (which
      # can happen when deployed).
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)

      abs_kontr <- "test variable"

      # Set up parameters to pass to Rmd document
      params <- list(abs_kontr = abs_kontr)

      # Knit the document, passing in the `params` list, and eval it in a
      # child of the global environment (this isolates the code in the document
      # from the code in this app).
      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )

这是我的 .Rmd 文件的相关部分:

---
output: 
  pdf_document:
    latex_engine: xelatex
    fig_caption: yes
    number_sections: true
params:
  abs_kontr: NA
mainfont: Calibri
fontsize: 12pt
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[]{}
- \fancyfoot[LO,LE]{`r format(Sys.time(), "%B %Y")`}
- \renewcommand{\footrulewidth}{0.4pt}
- \renewcommand{\headrulewidth}{0pt}
- \usepackage{sectsty}
- \setsansfont[Ligatures=TeX]{Arial Narrow}
- \sectionfont{\sffamily\Huge}
- \subsectionfont{\sffamily\huge}
- \subsectionfont{\sffamily\LARGE}

---
\thispagestyle{empty}
\linespread{1}
\begin{flushright}

```{r echo=FALSE, out.width="100%", fig.align='right'}
knitr::include_graphics("www/logo.jpg")
```

“knitr::include_graphics("www/logo.jpg")”是我直接编译文档时工作的部分,但当我通过 Shiny 应用程序调用 pdf 创建时不工作。当我想通过 Shiny 应用程序下载报告时,需要几秒钟才能重定向到指示代码崩溃的“找不到文件”页面。

【问题讨论】:

    标签: r shiny r-markdown


    【解决方案1】:

    感谢Using image in r markdown report downloaded from Shiny app,我自己找到了解决方案:

    您还必须将所有包含的外部图像复制到临时路径,以便找到它们!只需将以下内容添加到您的 app.R:

    tempReport2 <- file.path(tempdir(), "logo.jpg")
          file.copy("logo.jpg", tempReport2, overwrite = TRUE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-12
      • 2017-10-29
      • 2021-07-19
      • 1970-01-01
      • 2012-04-19
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多