Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use `format = "latex"` in knitr::kable() within a rmarkdown document generated via shiny

Tags:

r

knitr

shiny

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.

like image 701
Phil Avatar asked Jan 04 '23 00:01

Phil


1 Answers

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.

like image 134
Hao Avatar answered Jan 05 '23 16:01

Hao