rjen
rjen

Reputation: 1972

kableExtra error in Markdown when rendering to PDF?

It seems that I am having trouble using kableExtra when trying to create a PDF by rendering an Rmd-file from an R Script. I have had no success in trying to follow the instructions at:

R markdown compile error: https://github.com/rstudio/bookdown/issues/440 https://community.rstudio.com/t/rendering-both-pdf-and-html-is-only-possible-interactively-not-via-script/19520/3 https://github.com/haozhu233/kableExtra/issues/301

I'll access the packages from the script:

library(tidyverse)
library(knitr)
library(rmarkdown)
library(tinytex)

Make the Rmd-file:

---
output: pdf_document 
---
{r, comment = NA, echo = FALSE, message = FALSE, warning = FALSE}

tibble(x = 1, y = 2, z = 3) %>%
    kable()

Render the Rmd-file from the script using:

render('C:/Users/Rasmus/SO/Test.Rmd',
    output_file = "Test.pdf",
    output_format = 'pdf_document',
    output_dir = 'C:/Users/Rasmus/SO')

This gives me a PDF with a table. But if I start out by running library(kableExtra) alongside the others, and then apply the rendering procedure, I get a PDF with:

x
y
z
1
2
3

After running library(kableExtra), I have tried the rendering procedure on the following Rmd-file:

---
output: pdf_document 
---
{r, comment = NA, echo = FALSE, message = FALSE, warning = FALSE}

tibble(x = 1, y = 2, z = 3) %>%
    kable() %>%
    kable_styling(latex_options = 'scale_down')

This returns:

output file: Test.knit.md

Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:

  always_allow_html: true

Note however that the HTML output will not be visible in non-HTML formats.

What is stopping me from using kableExtra?

Upvotes: 4

Views: 9168

Answers (1)

eipi10
eipi10

Reputation: 93791

You need to explicitly add format="latex" within the kable function (for example, kable(format="latex")) when using kableExtra with kable for PDF output. See the kableExtra introduction for additional information and examples.

The kableExtra PDF output Vignette indicates that you don't need to set format="latex" for version 0.9.0 or later, but I've found (using version 1.1.0 currently) that I still need to set format="latex" or I get html output by default.

You can run options(knitr.table.format = "latex") at the beginning of any script or Rmarkdown document to make latex the default output for that R session and avoid having to add format="latex" to each individual kable table.

Upvotes: 15

Related Questions