Search code examples
rr-markdownknitrkablekableextra

R-Markdown - kableExtra package - format = 'latex' not working


Using the kableExtra documentation. inside RMardown I am running:

```{r}
library(knitr)
library(kableExtra)

dt <- mtcars[1:5, 1:6]

kable(dt, format = "rmarkdown")

```

this actually outputs a table but I also get the following in the console:

    Error in kable_rmarkdown(x = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710",  : 
  could not find function "kable_rmarkdown"

when I switch to:

```{r}
library(knitr)
library(kableExtra)

dt <- mtcars[1:5, 1:6]

kable(dt, format = "latex")

```

I get no error and no table. Do I need to install latex to use this functionality?


Solution

  • Just to put the comments together for providing a complete answer: The following quote is from the kableExtra vignette:

    Starting from kableExtra 0.9.0, when you load this package (library(kableExtra)), it will automatically set up the global option ’knitr.table.format’ based on your current environment. Unless you are rendering a PDF, kableExtra will try to render a HTML table for you. You no longer need to manually set either the global option or the format option in each kable() function.

    So you can write in both your examples (markdown and LaTeX):

    library(knitr)
    library(kableExtra)
    
    dt <- mtcars[1:5, 1:6]
    
    kable(dt)
    

    Depending on your output format you will get the table rendered in HTML or LaTeX (PDF). And yes: For PDF you will need a LaTeX installation. But this is nowadays quite easy with TinyTeX by Yihui Xie.