rstudio / rstudio/rmarkdown

kableExtra in rmarkdown (pdf-output) with shiny app: ! Undefined control sequence

Open
#2,467 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
3.1k
Forks
990
Avg merge
5h 43m
Merged PRs (30d)
11

Description

I have followed this issue in order to render a rmarkdown to pdf while using kableextra, but It fails when I'm using the shiny app. If I use the Knit button from Rstudio IDE, it will compile, but not from the shiny app !
I have prepared a minimal example as well :

library(shiny)
library(dplyr)
library(kableExtra)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      numericInput("n_rows", label = "Number of rows to display", value = 10),
      downloadButton('report',"Generate Report")
    ),
    mainPanel(
      tableOutput("mtcars_table")
    )
  )
)


server <- function(input, output,session) {
  output$mtcars_table <- renderTable({
    head(mtcars, n = input$n_rows)
  })
  
  output$report <- downloadHandler(
    # For PDF output, change this to "report.pdf"
    filename = "report.pdf",
    content = function(file) {
      withProgress(message = 'Calculation in progress',{
        incProgress(session = session)
        tempReport <- file.path(tempdir(), "report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)
        
        params <- list(df = mtcars[input$n_rows,])
        
        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
        incProgress(session=session)
      }, session = session)
    }
  )
}


shinyApp(ui, server)  

And the report.rmd :

---
output:
  pdf_document:
    toc: false
    keep_tex: true
    number_sections: true
  documentclass: scrartcl
params:
  df: NA
header-includes:
  \usepackage[]{graphicx}
  \usepackage[]{xcolor}
  \usepackage{helvet}
  \renewcommand{\familydefault}{\sfdefault}
  \usepackage[T1]{fontenc}
  \geometry{verbose,tmargin=4.2cm,bmargin=2.5cm,lmargin=2.5cm,rmargin=2cm,headheight=1.6cm,headsep=1.2cm,footskip=1cm}
  \pagestyle{fancy}
  \setlength{\parskip}{\medskipamount}
  \setlength{\parindent}{0pt}
  \usepackage{textcomp}
  \usepackage{babel}
  \usepackage{pdflscape}
  \usepackage{tabularx}
  \usepackage{color}
  \usepackage{array}
  \usepackage{hyperref}
  \usepackage{ifthen}
  \usepackage{float}
  \usepackage{fancyhdr}
  \usepackage{framed}
  \usepackage{alltt}
  \usepackage{booktabs}
  \usepackage{makecell}
  \usepackage{longtable}
  \usepackage{multirow}
  \usepackage{wrapfig}
  \usepackage{colortbl}
  \usepackage{tabu}
  \usepackage{threeparttable}
  \usepackage{threeparttablex}
  \usepackage[normalem]{ulem}
  \usepackage{xcolor}
---

# Chapter title

```{r  echo=FALSE, message=FALSE, warning=FALSE,fig.pos = 'H'}
library(kableExtra)
df = params$df
kbl(df, booktabs = T, linesep = "") %>%
kable_styling(latex_options = "striped", stripe_index = c(1,2, 5:6))

Section title

\lipsum[1-10]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided shinyApp code and report.Rmd, then compare PDF rendering through the RStudio Knit button with rendering through the shiny downloadHandler. Inspect the Undefined control sequence output and the generated LaTeX around the kableExtra table. Done means the same report renders successfully as a PDF from the shiny app.

Written by the indexing model from the issue text.

Assessment

Tech stack
latex, markdown, r
Domain
documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.