2
votes

I am using the bookdown package to produce a large document with tables using the knitr::kable function. I can get the document to produce tables in HTML perfectly, but in PDF, the table contents just appear as a list of numbers. This is also true for the output to Word. The compiled LaTeX just appears to be a list of numbers as well.

This is also true of output from other packages like sjplot.

The same problem also appears when the sample code from the bookdown book.


knitr::kable(
  list(
    head(iris[, 1:2], 3),
    head(mtcars[, 1:3], 5)
  ),
  caption = 'A Tale of Two Tables.', booktabs = TRUE
)

Produces the expected output in HTML:

HTML Output

But, produces the following in PDF:

enter image description here

The YAML header in index.rmd are:

#output ~~~~~~~~~~~ [see _output.yml] 
site: bookdown::bookdown_site
output: [bookdown::gitbook, bookdown::pdf_book, bookdown::word_document2, bookdown::html_document2]
documentclass: book

The output settings in the _output.yml are:

bookdown::pdf_book:
    keep_tex: true
    latex_engine: xelatex
    includes:
      in_header: tex/biblio.tex
    number_sections: yes
    pandoc_args: ["--top-level-division=chapter"]
1
What chunk options are you using?monarque13
You can make this problem appear even if you use the "new bookdown project" starter code. As I specified in my answer, it appears that simply loading the "kableExtra" package seemed to interfere with the PDF rendering of tables, even though the HTML rendering works just fine.HoneyBuddha

1 Answers

4
votes

It turns out the problem arises from some interaction with the kableExtra package

So, even if you specify (as I did in my sample code) the knitr namespace (knitr::kable), there is still some downstream interference if kableExtra is loaded. So, this will not work when rendering to PDF:

library(kableExtra, warn.conflicts = TRUE)
knitr::kable(head(iris, 20), caption = 'Here is a nice table!',  booktabs = TRUE)

While everything works for the HTML output, the rendering to PDF does not work for the tables and you just end up with a list of the table cell values (for both PDF and Word) - as shown in the image above.

To solve, remove any library statements

Then, ensure you unload the package:

detach("package:kableExtra", unload = TRUE)

And, finally, for me, I found that I needed to "Restart R and Clear All Outputs" or "Terminate R" from the R-Studio "Session" menu.