library(tinytable)
options(tinytable_tt_digits = 3)
options(tinytable_theme_placement_latex_float = "H")
<- mtcars[1:4, 1:5] x
Groups and labels
The group_tt()
function can label groups of rows (i
) or columns (j
).
Rows
The i
argument accepts a named list of integers. The numbers identify the positions where row group labels are to be inserted. The names includes the text that should be inserted:
<- mtcars[1:9, 1:8]
dat
tt(dat) |>
group_tt(i = list(
"I like (fake) hamburgers" = 3,
"She prefers halloumi" = 4,
"They love tofu" = 7))
mpg | cyl | disp | hp | drat | wt | qsec | vs |
---|---|---|---|---|---|---|---|
21 | 6 | 160 | 110 | 3.9 | 2.62 | 16.5 | 0 |
21 | 6 | 160 | 110 | 3.9 | 2.88 | 17 | 0 |
22.8 | 4 | 108 | 93 | 3.85 | 2.32 | 18.6 | 1 |
21.4 | 6 | 258 | 110 | 3.08 | 3.21 | 19.4 | 1 |
18.7 | 8 | 360 | 175 | 3.15 | 3.44 | 17 | 0 |
18.1 | 6 | 225 | 105 | 2.76 | 3.46 | 20.2 | 1 |
14.3 | 8 | 360 | 245 | 3.21 | 3.57 | 15.8 | 0 |
24.4 | 4 | 147 | 62 | 3.69 | 3.19 | 20 | 1 |
22.8 | 4 | 141 | 95 | 3.92 | 3.15 | 22.9 | 1 |
We can style group rows in the same way as regular rows:
tt(dat) |>
group_tt(
i = list(
"I like (fake) hamburgers" = 3,
"She prefers halloumi" = 4,
"They love tofu" = 7)) |>
style_tt(
i = c(3, 5, 9),
align = "c",
color = "white",
background = "gray",
bold = TRUE)
mpg | cyl | disp | hp | drat | wt | qsec | vs |
---|---|---|---|---|---|---|---|
21 | 6 | 160 | 110 | 3.9 | 2.62 | 16.5 | 0 |
21 | 6 | 160 | 110 | 3.9 | 2.88 | 17 | 0 |
22.8 | 4 | 108 | 93 | 3.85 | 2.32 | 18.6 | 1 |
21.4 | 6 | 258 | 110 | 3.08 | 3.21 | 19.4 | 1 |
18.7 | 8 | 360 | 175 | 3.15 | 3.44 | 17 | 0 |
18.1 | 6 | 225 | 105 | 2.76 | 3.46 | 20.2 | 1 |
14.3 | 8 | 360 | 245 | 3.21 | 3.57 | 15.8 | 0 |
24.4 | 4 | 147 | 62 | 3.69 | 3.19 | 20 | 1 |
22.8 | 4 | 141 | 95 | 3.92 | 3.15 | 22.9 | 1 |
Columns
The syntax for column groups is very similar, but we use the j
argument instead. The named list specifies the labels to appear in column-spanning labels, and the values must be a vector of consecutive and non-overlapping integers that indicate which columns are associated to which labels:
tt(dat) |>
group_tt(
j = list(
"Hamburgers" = 1:3,
"Halloumi" = 4:5,
"Tofu" = 7))
Hamburgers | Halloumi | Tofu | |||||
---|---|---|---|---|---|---|---|
mpg | cyl | disp | hp | drat | wt | qsec | vs |
21 | 6 | 160 | 110 | 3.9 | 2.62 | 16.5 | 0 |
21 | 6 | 160 | 110 | 3.9 | 2.88 | 17 | 0 |
22.8 | 4 | 108 | 93 | 3.85 | 2.32 | 18.6 | 1 |
21.4 | 6 | 258 | 110 | 3.08 | 3.21 | 19.4 | 1 |
18.7 | 8 | 360 | 175 | 3.15 | 3.44 | 17 | 0 |
18.1 | 6 | 225 | 105 | 2.76 | 3.46 | 20.2 | 1 |
14.3 | 8 | 360 | 245 | 3.21 | 3.57 | 15.8 | 0 |
24.4 | 4 | 147 | 62 | 3.69 | 3.19 | 20 | 1 |
22.8 | 4 | 141 | 95 | 3.92 | 3.15 | 22.9 | 1 |
Here is a table with both row and column headers, as well as some styling:
<- mtcars[1:9, 1:8]
dat tt(dat) |>
group_tt(
i = list("I like (fake) hamburgers" = 3,
"She prefers halloumi" = 4,
"They love tofu" = 7),
j = list("Hamburgers" = 1:3,
"Halloumi" = 4:5,
"Tofu" = 7)) |>
style_tt(
i = c(3, 5, 9),
align = "c",
background = "teal",
color = "white") |>
style_tt(i = -1, color = "teal")
Hamburgers | Halloumi | Tofu | |||||
---|---|---|---|---|---|---|---|
mpg | cyl | disp | hp | drat | wt | qsec | vs |
21 | 6 | 160 | 110 | 3.9 | 2.62 | 16.5 | 0 |
21 | 6 | 160 | 110 | 3.9 | 2.88 | 17 | 0 |
22.8 | 4 | 108 | 93 | 3.85 | 2.32 | 18.6 | 1 |
21.4 | 6 | 258 | 110 | 3.08 | 3.21 | 19.4 | 1 |
18.7 | 8 | 360 | 175 | 3.15 | 3.44 | 17 | 0 |
18.1 | 6 | 225 | 105 | 2.76 | 3.46 | 20.2 | 1 |
14.3 | 8 | 360 | 245 | 3.21 | 3.57 | 15.8 | 0 |
24.4 | 4 | 147 | 62 | 3.69 | 3.19 | 20 | 1 |
22.8 | 4 | 141 | 95 | 3.92 | 3.15 | 22.9 | 1 |
We can also stack several extra headers on top of one another:
tt(x) |>
group_tt(j = list("Foo" = 2:3, "Bar" = 5)) |>
group_tt(j = list("Hello" = 1:2, "World" = 4:5))
Hello | World | |||
---|---|---|---|---|
Foo | Bar | |||
mpg | cyl | disp | hp | drat |
21 | 6 | 160 | 110 | 3.9 |
21 | 6 | 160 | 110 | 3.9 |
22.8 | 4 | 108 | 93 | 3.85 |
21.4 | 6 | 258 | 110 | 3.08 |