Tips and Tricks

Markdown

style_tt() does not apply to row headers

This is an important limitation, but it is difficult to get around. See this issue for discussion: https://github.com/vincentarelbundock/tinytable/issues/125

Users can use markdown styling directly in group_tt() to circumvent this. This is documented in the tutorial.

rowspan and colspan

These arguments are already implemented in the form of “pseudo-spans”, meaning that we flush the content of adjacent cells, but do not modify the row or column borders. This is probably adequate for most needs.

One alternative would be to remove line segments in finalize_grid(). I tried this but it is tricky and the results were brittle, so I rolled it back. I’m open to considering a PR if someone wants to contribute code, but please discuss the feature design in an issue with me before working on this.

Removing elements with theme_empty()

In some cases, it is useful to remove elements of an existing tinytable object. For example, packages like modelsummary often return tables with default styling—such as borders and lines in specific position. If the user adds group labels manually, the original lines and borders will be misaligned.

The code below produces a regression table with group labels but misaligned horizontal rule.

#! warning: false
library(modelsummary)
library(tinytable)

mod <- lm(mpg ~ factor(cyl) + hp + wt - 1, data = mtcars)

modelsummary(mod) |>
    group_tt(
        i = list(
            "Cylinders" = 1,
            "Others" = 7
        )
    )
(1)
Cylinders Cylinders
factor(cyl)4 35.846
(2.041)
factor(cyl)6 32.487
(2.811)
factor(cyl)8 32.660
(3.835)
Others Others
hp -0.023
(0.012)
wt -3.181
(0.720)
Num.Obs. 32
R2 0.989
R2 Adj. 0.986
AIC 154.5
BIC 163.3
Log.Lik. -71.235
RMSE 2.24

To fix this, we can strip the lines and add them back in the correct position.

modelsummary(mod) |>
    theme_empty() |>
    group_tt(
        i = list(
            "Cylinders" = 1,
            "Others" = 7
        )
    ) |>
    style_tt(i = 12, line = "b", line_width = .05)
(1)
Cylinders Cylinders
factor(cyl)4 35.846
(2.041)
factor(cyl)6 32.487
(2.811)
factor(cyl)8 32.660
(3.835)
Others Others
hp -0.023
(0.012)
wt -3.181
(0.720)
Num.Obs. 32
R2 0.989
R2 Adj. 0.986
AIC 154.5
BIC 163.3
Log.Lik. -71.235
RMSE 2.24