4

I would love to use the kableExtra package features (as documented here) in the production of a PDF report from a Shiny app. Unfortunately, while I'm able to replicate the examples from the document linked above outside of a Shiny environment, I get the following error when I attempt it within Shiny:

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS report.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc2f0831607c9a.pdf --template "C:\Users\PStraforelli\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" ! Undefined control sequence. l.160 \toprule

pandoc.exe: Error producing PDF Warning: running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS report.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc2f0831607c9a.pdf --template "C:\Users\phil\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43 Warning: Error in : pandoc document conversion failed with error 43 Stack trace (innermost first): 53: pandoc_convert 52: convert 51: rmarkdown::render 50: download$func [C:\Users\phil\Desktop\app/app.R#16] 1: shiny::runApp Error : pandoc document conversion failed with error 43

Here is a reproducible example:

The app.R file:

#From here: http://shiny.rstudio-staging.com/articles/generating-reports.html
shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 32, 10),
    downloadButton("report", "Generate report")
  ),
  server = function(input, output) {
    output$report <- downloadHandler(

      filename = "report.pdf",
      content = function(file) {
                tempReport <- file.path(tempdir(), "report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)

        params <- list(n = input$slider)

        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

The report.Rmd file:

---
title: "Dynamic report"
output: pdf_document
params:
  n: NA
---

```{r}
#From here: http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
library(magrittr)
library(knitr)
library(kableExtra)
dt <- mtcars[1:params$n, 1:6]

kable(dt, format = "latex", booktabs = T) %>% 
  kable_styling(latex_options = "striped")
```

When I launch the app.R file onto an Internet browser, and click on "Generate report", I get the error quoted above. If I remove the format = "latex" argument within knitr::kable() in the report.Rmd file (and the kable_styling() function), than everything works fine.

Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

10

You need to put library(kableExtra) in your rmarkdown. It will automatically load necessary LaTeX packages for you.


Update: rmarkdown render has some issues on clear up metadata. To ensure those packages are loaded, you can follow the direction in the "LaTeX packages used in this package" section of the vignette and put that header-includesin your yaml header.

See Why does rendering a pdf from rmarkdown require closing rstudio between renders? for details.

Hao
  • 7,476
  • 1
  • 38
  • 59
  • 2
    Hi Hao, thanks for very much for the prompt response, and I first want to say that the `kableExtra` package is awesome. I'm still getting an error even if I call the needed packages using `library()` (I updated my initial question accordingly), although it is somewhat different from my previous error message. It now starts with `! LaTeX Error: Unknown float option H'. See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ... l.139 \begin{table}[H]`. – Phil Dec 28 '17 at 21:21
  • 3
    Hmm... I restarted the R session, and ran again, and it now works fine. Thanks! – Phil Dec 28 '17 at 21:41
  • You reminded me something so I updated my answer. :) – Hao Dec 28 '17 at 21:59
  • Restarting the session worked for me as well – Ljupcho Naumov Dec 16 '21 at 16:38