0

I have a Shiny app which allows to create a PDF report via downloadHandler(). I include an external image file in my .Rmd file. When I knit my .Rmd file alone, this image is displayed. When I generate my PDF report via the Shiny app (which calls the .Rmd file), the image is not found and the report cannot be generated. What am I doing wrong?

The relevant part of the Shiny app:

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

And here is the relevant part of my .Rmd file:

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

The "knitr::include_graphics("www/logo.jpg")" is the part which is working when I compile the document directly, but not working when I call the pdf creation via the Shiny app. When I want to download the report via Shiny app, it takes some seconds before I get redirected to a "file not found" page indicating that the code crashed.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Requin
  • 467
  • 4
  • 16

1 Answers1

1

I found the solution on my own, thanks to Using image in r markdown report downloaded from Shiny app :

You have to copy all included external images to the temp path as well, so that they are found! Simply add the following to your app.R:

tempReport2 <- file.path(tempdir(), "logo.jpg")
      file.copy("logo.jpg", tempReport2, overwrite = TRUE)
Requin
  • 467
  • 4
  • 16