title: Using image in r markdown report downloaded from Shiny app
body: <p>I have created a very large shiny app that has a downloadable pdf report.  The client has requested their logo in the header of every page of the pdf.  I can get a logo on the pdf when the pdf is by itself (not part of the larger shiny app) but pandoc cannot find the image when I try to download the exact same report from within the shiny app.  Below is a minimum working example and a list of things I have tried and failed to get to work.  <strong>smiley.png is in the folder with app.R and can be replaced by any image.</strong>  smiley.png is a different image from the one I used in the full app, so it has nothing to do with the original image.</p>
  
  <p>Knitting the rmarkdown by itself works perfectly and includes the header.  Trying to download from within the shiny app causes the problem.<br>
  I've tried:</p>
  
  <ul>
  <li>moving the image into a folder called "images" and referencing images\smiley.png rather than smiley.png.  That fails as well with the same error.</li>
  <li>referencing /srv/shiny-server/AppName/smiley.png when the app was uploaded to shinyapps.io.  Failed with unable to find the image (same type of error).</li>
  <li>using plain <code>![Logo](smiley.png)</code> syntax rather than the four header lines.  Also failed with the same error of unable to find smiley.png  </li>
  <li><code>&lt;img src="smiley.png" /&gt;</code> with smiley.png in my www folder does not work.  I'm knitting a pdf, not an html.  pdf knits but does not include an image.  It just removes the html.  </li>
  <li>using plain <code>![Logo](smiley.png)</code> syntax with smiley.png in my www folder does not work.  Same error; can't find smiley.png.  </li>
  </ul>
  
  <p>My best guess is that when the app runs, it moves around the directories somehow and the .rmd cannot locate the image.  So what do I need to reference in order to find the image?  Can I put it in a particular folder?  I've tried so many different things and done a lot of research, but have had trouble finding a single similar example.  I've used the www folder I use for images in the shiny app (not included below), adding new folders, putting the image in the same folder as the .rmd...  It's been a very long process of research, trial, and error with no success. </p>
  
  <p>The app:</p>
  
  <pre><code>library(shiny)
  ui&lt;-shinyUI(fluidPage(
    titlePanel("Hello Shiny!"),
    sidebarPanel(
      downloadButton('downloadReport',label="Download Report")
                ),
    mainPanel(
        p("Hello")
             )
    ))
  
  server&lt;-shinyServer(function(input, output) {
     output$downloadReport &lt;- downloadHandler(
        filename = function() {
             paste0('Report_.pdf')
                              },
        content = function(file) {
             src &lt;- normalizePath('report.rmd')
             owd &lt;- setwd(tempdir())
             on.exit(setwd(owd))
             file.copy(src, 'report.rmd')
             library(rmarkdown)
             out &lt;- render('report.rmd',pdf_document())
             file.rename(out, file)
                                 }
     )
   })
  
  shinyApp(ui, server)#Runs the app
  </code></pre>
  
  <p>R markdown report.rmd:  </p>
  
  <pre><code>---
  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)
  ```
  </code></pre>
  
  <p>The error:  </p>
  
  <pre><code>  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 &lt;return&gt;  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: &lt;Anonymous&gt;  
      4: do.call  
      3: print.shiny.appobj  
      2: print  
      1: source  
  </code></pre>
  
  <p>Thank you!  I hope someone has some ideas.  I've been researching and trying things for days on and off.</p>
  
  <p>Edit: Fixed the output formatting.</p>