rkableextramodelsummary

Post-process Modelsummary output with kableExtra with latex_tabular


I'm trying to produce a table with modelsummary that has spanning headers. That's why I want to post-process the modelsummary output with kableExtra::add_header_above. Also, I want the resulting table to be in a tabular environment only (no table environment). The following code used to work a few months back, however, it doesn't anymore:

# Load necessary package
library(modelsummary)
library(knitr)
library(kableExtra)

# Create example regression models using built-in mtcars dataset
m1 <- lm(mpg ~ hp, data = mtcars)
m2 <- lm(mpg ~ drat, data = mtcars)
m3 <- lm(mpg ~ wt, data = mtcars)

# Attempt to create a LaTeX table using modelsummary and kableExtra

modelsummary(list(
  "(A)"=m1, 
  "(B)"=m2,
  "(C)"=m3),
  output = "latex_tabular") %>%   
  add_header_above(c("Text"=1, "More Text"=1, "Some Other Text"=2)) 

The error I get is "Error in if (kable_format %in% c("pipe", "markdown")) { : argument is of length zero"

If I change the output in the modelsummary to "kableExtra" it works, but then I can't save it to a tex file using save_kable() anymore. Same error occurs with output = "latex" btw.

Does anyone have an idea what's going on here?


Solution

  • Since version 2.0.0 of modelsummary, the default output format is tinytable, rather than kableExtra. There were good reasons for that shift and I, the modelsummary author, strongly recommend you try the defaults before switching to kableExtra.

    In this example, we use:

    1. group_tt() from tinytable to add spanning column headers.
    2. theme_tt("tabular") to apply a special theme to the LaTeX table that will remove environments other than the simple tabular.
    3. print("latex") to print it. This is only so you can see the raw code on StackOverflow. It would not be needed in a Quarto or Rmarkdown document. Alternatively, you could use save_tt("path/to/file.tex") to save the LaTeX code to a file.

    The tinytable website hosts a billion tutorials and examples: https://vincentarelbundock.github.io/tinytable/

    modelsummary(list(
      "(A)"=m1, 
      "(B)"=m2,
      "(C)"=m3)) |>
      group_tt(j = list("Text" = 1, "More Text" = 2, "Some Other Text" = 3:4)) |>
      theme_tt("tabular") |>
      print("latex")
    
    \begin{tabular}{llll}
    \hline
    Text & More Text & Some Other Text &  \\ \cmidrule(lr]{1-1}\cmidrule[lr]{2-2}\cmidrule[lr){3-4}
    & (A) & (B) & (C) \\ \hline
    (Intercept) & 30.099  & -7.525  & 37.285  \\
    & (1.634) & (5.477) & (1.878) \\
    hp          & -0.068  &         &         \\
    & (0.010) &         &         \\
    drat        &         & 7.678   &         \\
    &         & (1.507) &         \\
    wt          &         &         & -5.344  \\
    &         &         & (0.559) \\
    Num.Obs.    & 32      & 32      & 32      \\
    R2          & 0.602   & 0.464   & 0.753   \\
    R2 Adj.     & 0.589   & 0.446   & 0.745   \\
    AIC         & 181.2   & 190.8   & 166.0   \\
    BIC         & 185.6   & 195.2   & 170.4   \\
    Log.Lik.    & -87.619 & -92.400 & -80.015 \\
    F           & 45.460  & 25.970  & 91.375  \\
    RMSE        & 3.74    & 4.34    & 2.95    \\
    \hline
    \end{tabular}