Search code examples
rshinyr-markdownkablekableextra

kable and kableExtra not rendering in pdf from markdown document


I am trying to render a table into a pdf using kable in a markdown document generated from a shiny app. It renders OK until I use any functionality from kableExtra (column_spec in the code below) at which point I get the following error:

! LaTeX Error: Illegal character in array arg.

The problem seems pretty basic and I'm not the first to have this issue, but other solutions (such as this and this) haven't helped. The table renders OK as HTML but it seems that kableExtra doesn't seem to know that it is rendering latex. If I change the knitr.table.format to "pandoc" it renders but still errors of the column_spec line. Any ideas?

UI.R

fluidPage(
  title = 'Download a PDF report',
  sidebarLayout(
    sidebarPanel(
      helpText(),
      div("Create a Mark Down Document"),
      radioButtons('format', 'Document format', c('PDF', 'HTML'),
                   inline = TRUE),
      downloadButton('downloadReport')
    ),
    mainPanel(
      plotOutput('regPlot')
    )
  )
)

Server.R

library(rmarkdown)
library(shiny)
library(magrittr)
library(kableExtra)

function(input, output) {

  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html'
      ))
    },

    content = function(file) {
      src <- normalizePath('report.Rmd')

      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report.Rmd', overwrite = TRUE)

      out <- render('report.Rmd', switch(
        input$format,
        PDF = pdf_document(), HTML = html_document()
      ))
      file.rename(out, file)
    }
  )

}

Report.Rmd

 Test kable:
 
```{r}

 auto_set_format <- function() { 
   if (knitr::is_latex_output()) { 
     options(knitr.table.format = "latex") 
   } else { 
     options(knitr.table.format = "html") 
   } 
 }

auto_set_format()

tbl <- knitr::kable(mtcars[1:6, 1:6], caption = 'A subset of mtcars.') 

# THIS LINE CAUSES THE PROBEM WHEN WRITING TO PDF. COMMENT OUT AND IT WORKS OK
tbl <- tbl %>% column_spec(3, color = "#62BD19")

tbl
```

Solution

  • For column_spec() you need to give an additional Latex package to knitr, i.e. change your report to

    ---
    title: "Example Table"
    header-includes:
       - \usepackage{dcolumn}
    ---
     Test kable:
     
    ```{r}
    
     auto_set_format <- function() { 
       if (knitr::is_latex_output()) { 
         options(knitr.table.format = "latex") 
       } else { 
         options(knitr.table.format = "html") 
       } 
     }
    
    auto_set_format()
    
    tbl <- knitr::kable(mtcars[1:6, 1:6], caption = 'A subset of mtcars.') %>% 
                  column_spec(3, color = "#62BD19")
    
    tbl
    ```
    

    and you will be able to produce both reports.