在从 Shiny 应用程序下载的 r markdown 报告中使用图像

Using image in r markdown report downloaded from Shiny app

我创建了一个非常大的闪亮应用程序,其中包含可下载的 pdf 报告。客户要求在 pdf 每一页的 header 中显示他们的徽标。当 pdf 本身(不是较大的闪亮应用程序的一部分)时,我可以在 pdf 上获得徽标,但是当我尝试从闪亮的应用程序中下载完全相同的报告时,pandoc 找不到图像。下面是一个最小的工作示例和我尝试过但未能开始工作的事情列表。 smiley.png在app.R所在的文件夹中,可以用任何图片替换。smiley.png与我完整使用的图片不同app,所以与原图无关

编织 rmarkdown 本身效果很好,包括 header。尝试从闪亮的应用程序中下载会导致问题。
我试过:

我最好的猜测是,当应用程序运行时,它会以某种方式在目录中移动,而 .rmd 无法找到图像。那么我需要参考什么才能找到图像?我可以把它放在一个特定的文件夹中吗?我尝试了很多不同的事情并做了很多研究,但很难找到一个类似的例子。我在闪亮的应用程序中使用了我用于图像的 www 文件夹(下面不包括),添加新文件夹,将图像放在与 .rmd 相同的文件夹中......这是一个非常漫长的研究,试验过程,和没有成功的错误。

应用程序:

library(shiny)
ui<-shinyUI(fluidPage(
  titlePanel("Hello Shiny!"),
  sidebarPanel(
    downloadButton('downloadReport',label="Download Report")
              ),
  mainPanel(
      p("Hello")
           )
  ))

server<-shinyServer(function(input, output) {
   output$downloadReport <- downloadHandler(
      filename = function() {
           paste0('Report_.pdf')
                            },
      content = function(file) {
           src <- normalizePath('report.rmd')
           owd <- setwd(tempdir())
           on.exit(setwd(owd))
           file.copy(src, 'report.rmd')
           library(rmarkdown)
           out <- render('report.rmd',pdf_document())
           file.rename(out, file)
                               }
   )
 })

shinyApp(ui, server)#Runs the app

R 降价 report.rmd:

---
title: "Test"
date: "Friday, March 04, 2016"
output: pdf_document
header-includes: \usepackage{fancyhdr}
---

\addtolength{\headheight}{1.0cm}
\pagestyle{fancyplain}
\lhead{\includegraphics[height=1.2cm]{smiley.png}}
\renewcommand{\headrulewidth}{0pt}

```{r, echo=FALSE}
plot(cars)
```
```{r, echo=FALSE}
plot(cars)
```

错误:

  C:/Apps/RStudio/bin/pandoc/pandoc report.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output report.pdf --template C:\Apps\R-3.1.1\library\rmarkdown\rmd\latex\default.tex --highlight-style tango --latex-engine pdflatex --variable geometry:margin=1in  
 pandoc.exe: Error producing PDF from TeX source.  
 ! Package pdftex.def Error: File `smiley.png' not found.  

 See the pdftex.def package documentation for explanation.  
 Type  H <return>  for immediate help.  
 ...                                              

l.88 \end{document}  

Warning: running command 'C:/Apps/RStudio/bin/pandoc/pandoc report.utf8.md --to latex --from  markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output report.pdf --template C:\Apps\R-3.1.1\library\rmarkdown\rmd\latex\default.tex --highlight-style tango --latex-engine pdflatex --variable geometry:margin=1in' had status 43  
Error : pandoc document conversion failed with error 43  
In addition: Warning message:  
package ‘shiny’ was built under R version 3.1.3   
Warning: Error in : pandoc document conversion failed with error 43  
Stack trace (innermost first):  
   55: pandoc_convert  
   54: render  
   53: download$func [C:/Data/Documents/Technomic/Testing images/app.R#25]  
    5: <Anonymous>  
    4: do.call  
    3: print.shiny.appobj  
    2: print  
    1: source  

谢谢!我希望有人有一些想法。我断断续续研究和尝试了好几天。

编辑:修复了输出格式。

终于找到了问题 - 它根本不在 rmarkdown 中。我正在将 report.pdf 复制到一个临时目录以确保我可以写入它,这意味着报告无法再找到该图像。我所要做的就是将图像也复制到临时目录。添加了两行,现在可以完美运行了!

希望这对其他人有帮助!我知道我花了很长时间寻找这个解决方案,但找不到其他人试图在可下载的报告中包含图像。

library(shiny)

# Define UI for application that draws a histogram
ui<-shinyUI(fluidPage(
  titlePanel("Hello Shiny!"),
    sidebarPanel(
      downloadButton('downloadReport',label="Download Report")
    ),
    mainPanel(
      p("Hello")
    )
))

server<-shinyServer(function(input, output) {
  output$downloadReport <- downloadHandler(
    filename = function() {
      paste0('Report_.pdf')
    },
    content = function(file) {
      src <- normalizePath('report.rmd')
      src2 <- normalizePath('smiley.png') #NEW 
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report.rmd')
      file.copy(src2, 'smiley.png') #NEW
      library(rmarkdown)
      out <- render('report.rmd',pdf_document())
      file.rename(out, file)
    }
  )
})

shinyApp(ui, server)#Runs the app