kableExtra
kableExtra copied to clipboard
LaTeX packages omitted in preamble if kable_styling used in a loop
Hello,
I'm running into a problem when trying to generate parameterized reports while using kableExtra. This requires a template Rmd file:
---
params:
client: "Unknown client"
newdata: mtcars
title: "Report for `r params$client`"
author: "C. Chaplin"
date: "16/11/2020"
output: pdf_document
---
```{r setup, include=FALSE}
library(knitr)
library(kableExtra)
```
# Section 1
Here is the report for my client `r params$client` :
```{r cars, echo=FALSE}
kable(params$newdata) %>%
kable_styling(latex_options = c("striped"),font_size=6)
```
In this example below, it should generate 3 simple reports using clients
and datList
:
#Make reports for a set of clients using a for-loop
library(rmarkdown)
clients <- c('Larry','Moe','Curly Joe') #Names of clients
datList <- list(mtcars,CO2,iris) #Dataset for each client
for(i in 1:length(clients)){
pdfName <- paste0(clients[i],' report.pdf') #pdf filename
render(input='template2.Rmd',output_file=pdfName,params=list(client=clients[i],newdata=datList[[i]]))
}
However, this is not what happens. It successfully makes the first report, but fails on the second one:
#> processing file: template.Rmd
#> output file: template.knit.md
#> /usr/bin/pandoc +RTS -K512m -RTS template.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc9dcf1e93ea9e.tex --lua-filter /home/samuel/R/x86_64-pc-linux-gnu-library/3.6/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /home/samuel/R/x86_64-pc-linux-gnu-library/3.6/rmarkdown/rmarkdown/lua/latex-div.lua --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable 'geometry:margin=1in' --include-in-header /tmp/RtmpLnpKUX/rmarkdown-str9dcf2a47bab2.html
#>
#> Output created: Larry report.pdf
#> processing file: template.Rmd
#> output file: template.knit.md
#> /usr/bin/pandoc +RTS -K512m -RTS template.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc9dcf1e61dca6.tex --lua-filter /home/samuel/R/x86_64-pc-linux-gnu-library/3.6/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /home/samuel/R/x86_64-pc-linux-gnu-library/3.6/rmarkdown/rmarkdown/lua/latex-div.lua --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable 'geometry:margin=1in'
#> ! LaTeX Error: Unknown float option `H'.
#> Error: LaTeX failed to compile Moe report.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See Moe report.log for more info.
After inspecting the .tex files left in the directory after the error, it appears that during the second pass through the loop, render
is not knitting the appropriate LaTeX packages into the preamble before compiling: there is no \usepackage{float}
or \usepackage{xcolor}
in the preamble. This problem disappears if kable_styling
is omitted from the template file.
Many thanks!
Check out the kbl
function in the latest version of this package. I think it will solve this issue. Let me know if it works. Otherwise, you can just load those packages in header_includes.
I tried kbl
instead of kable
, and I encountered the same problem.
These reports all compile fine as long as they are run first in the loop, which makes me think that something about the knitting environment changes on each pass. However, this problem remains even when I specify envir=env.new()
in render
, so there's something else going on that I don't understand.
Loading the necessary packages using header_includes
fixes the problem, although it's a matter of trial and error to figure out which ones are required.