Search code examples
rr-markdownknitrpdflatex

rendering images in parameterized rmarkdown reports causes pdflatex to fail


I was following the guide here and here to generate the same report for a different subset of data. I've gotten it to generate simple PDF printing the different input parameters but when I try to generate pdf's with a ggplot, I get an error. Would anyone know why it's complaining?

The error (with filepaths trimmed):

output file: AllDepartmentsReport.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS AllDepartmentsReport.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc13b0103e5bc9.tex --template "C:..\R\win-library\3.5\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" --variable "compact-title:yes" ! Package pdftex.def Error: File `/autoReports/MATH_Report_files/figure-lat ex/unnamed-chunk-3-1.pdf' not found: using draft setting.

Error: Failed to compile /autoReports/MATH_Report.tex. See MATH_Report.log for more info.

Rscript to feed parameters generateReports.r:

library(knitr)
library(markdown)
library(rmarkdown)
library(tinytex)

departments <- c("MATH", "BIOL")


ReportGenerator <- function(department) {

  rmarkdown::render("./AllDepartmentsReport.Rmd",
                    params = list(department = dpt),
                    output_file = paste0(dpt,"_Report",".pdf"),
                    output_dir = "../autoReports",
                    clean = FALSE
                     )
}

# lapply(departments, ReportGenerator)

for (dpt in unique(departments) ){
  ReportGenerator(dpt)
}

Rmd with the report info AllDepartmentsReport:

---
output: pdf_document
params: 
  department: "MATH"
---

```{r, include = FALSE}
library(knitr)
library(tidyverse)
library(viridis)
library(purrr)
library(scales)
library(stringr)
library(lubridate)
library(markdown)
library(rmarkdown)
```

This is my report about 

```{r}
print(params$department)
paste(params$department)
print("hi")

ggplot(mtcars) + 
  geom_point(aes(x=mpg, y=hp))

```

I downloaded and used miktex. If I knit from the RMD it will generate properly. If I run the script without the ggplot it works. I should also note, that the file it says it cannot find, is actually there. I'm also using Windows 10, 64-bit.

Some of the articles I tried:

Edit:

I've tried to play with the options in render like intermediates_dir = and knit_root_dir but can't seem to get a combination that works. Only been able to compile in the same folder.


Solution

  • I can successfully save the created pdfs to a different folder by specifying the path as part of the output_file name instead of using output_dir. Something like below:

      rmarkdown::render(input = "reportMaker.Rmd",
                        params = list(department = dpt),
                        output_file = paste0("../autoReports/",dpt,"_Report",".pdf"), 
                        output_format = "pdf_document"
    
                         )