Why do kableExtra and modelsummary interact weirdly?

54 views Asked by At

I am working on a Quarto project and use R for statistical analyses, modelsummary to format the output tables and kableExtra to style those accordingly.

However, when using modelsummary with kableExtra, I am encountering a weird error where the output in the PDF is the raw LaTeX code for the table, but not the (rendered) table itself:

I encounter this problem specifically when calling kbl():

library(kableExtra)
library(modelsummary)

model <- lm(mpg ~ hp, data = mtcars)
modelsummary(model) |> 
    kbl() |> 
    kable_styling(full_width = TRUE)

When I skip kbl(), there is no problem - but I need some functionality of kbl() (e.g., longtable and booktabs). I already tried modifying the code blocks output, i.e. #| output: asis as well as all different combinations of modelsummary's format argument and kbl()'s output argument. Nothing seems to work - any ideas?

1

There are 1 answers

4
Julian On

These two packages work well together, use this code to make it work:

modelsummary(model, output = "kableExtra") |> 
    kable_styling(full_width=TRUE)

for pdf

modelsummary(model, output = "data.frame") |>
  kbl(format = "latex") |> 
  kable_styling(full_width=TRUE)