Spanning labels to identify groups of rows or columns

Description

Spanning labels to identify groups of rows or columns

Usage

group_tt(
  x,
  i = getOption("tinytable_group_i", default = NULL),
  j = getOption("tinytable_group_j", default = NULL),
  ...
)

Arguments

x A data frame or data table to be rendered as a table.
i

Character vector, named list, or integer vector

  • A character vector of labels with length equal to the number of rows in x

  • A named list of row indices to group. The names of the list will be used as labels. The indices represent the position where labels should be inserted in the original table. For example,

    • i=list(“Hello”=5): insert the "Hello" label after the 4th row in the original table.

    • i=list(“Hello”=2, “World”=2): insert the two labels consecutively after the 1st row in the original table.

    • i=list(“Foo Bar”=0): insert the label in the first row after the header.

  • Vector of positive integers: For matrix insertion: i specifies row positions and j must be a character matrix to insert in the table (see below for details).

j

String, named list, or character matrix

  • Named list of column indices to group, ex: j=list(“A”=1:2,“B”=3:6). The names of the list will be used as labels. See below for more examples. Note: empty labels must be a space: " ".

  • A single string when column names include the group name as a prefix, ex: group1_column1, group1_column2, etc.

  • Character matrix for inserting rows at positions specified by i. The matrix must have the same number of columns as the table, or be a single column with a number of elements that is a multiple of the table’s column count (which will be automatically reshaped). Each row of the matrix matches an element

Other arguments are ignored.

Details

Warning: The style_tt() can normally be used to style the group headers, as expected, but that feature is not available for Markdown and Word tables.

Value

An object of class tt representing the table.

Word and Markdown limitations

Markdown and Word tables only support these styles: italic, bold, strikeout. The width argument is also unavailable Moreover, the style_tt() function cannot be used to style headers inserted by the group_tt() function; instead, you should style the headers directly in the header definition using markdown syntax: group_tt(i = list(“italic header” = 2)). These limitations are due to the fact that there is no markdown syntax for the other options, and that we create Word documents by converting a markdown table to .docx via the Pandoc software.

Examples

library("tinytable")


# vector of row labels
dat <- data.frame(
  label = c("a", "a", "a", "b", "b", "c", "a", "a"),
  x1 = rnorm(8),
  x2 = rnorm(8)
)
tt(dat[, 2:3]) |> group_tt(i = dat$label)
x1 x2
a
0.45383827 0.53840124
-0.41643874 0.65816000
0.05125347 -0.09095432
b
0.50560851 -0.46038538
0.98878528 1.33133117
c
1.24762156 2.85702280
a
0.83076324 0.35658043
-0.25817956 -0.57328988
# named lists of labels
tt(mtcars[1:10, 1:5]) |>
  group_tt(
    i = list(
      "Hello" = 3,
      "World" = 8
    ),
    j = list(
      "Foo" = 2:3,
      "Bar" = 4:5
    )
  )
Foo Bar
mpg cyl disp hp drat
21.0 6 160.0 110 3.90
21.0 6 160.0 110 3.90
Hello
22.8 4 108.0 93 3.85
21.4 6 258.0 110 3.08
18.7 8 360.0 175 3.15
18.1 6 225.0 105 2.76
14.3 8 360.0 245 3.21
World
24.4 4 146.7 62 3.69
22.8 4 140.8 95 3.92
19.2 6 167.6 123 3.92
dat <- mtcars[1:9, 1:8]
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.0 6 160.0 110 3.90 2.620 16.46 0
21.0 6 160.0 110 3.90 2.875 17.02 0
I like (fake) hamburgers
22.8 4 108.0 93 3.85 2.320 18.61 1
She prefers halloumi
21.4 6 258.0 110 3.08 3.215 19.44 1
18.7 8 360.0 175 3.15 3.440 17.02 0
18.1 6 225.0 105 2.76 3.460 20.22 1
They love tofu
14.3 8 360.0 245 3.21 3.570 15.84 0
24.4 4 146.7 62 3.69 3.190 20.00 1
22.8 4 140.8 95 3.92 3.150 22.90 1
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.0 6 160.0 110 3.90 2.620 16.46 0
21.0 6 160.0 110 3.90 2.875 17.02 0
22.8 4 108.0 93 3.85 2.320 18.61 1
21.4 6 258.0 110 3.08 3.215 19.44 1
18.7 8 360.0 175 3.15 3.440 17.02 0
18.1 6 225.0 105 2.76 3.460 20.22 1
14.3 8 360.0 245 3.21 3.570 15.84 0
24.4 4 146.7 62 3.69 3.190 20.00 1
22.8 4 140.8 95 3.92 3.150 22.90 1
x <- mtcars[1:5, 1:6]
tt(x) |>
  group_tt(j = list("Hello" = 1:2, "World" = 3:4, "Hello" = 5:6)) |>
  group_tt(j = list("Foo" = 1:3, "Bar" = 4:6))
Foo Bar
World Hello
mpg cyl disp hp drat wt
21.0 6 160 110 3.90 2.620
21.0 6 160 110 3.90 2.875
22.8 4 108 93 3.85 2.320
21.4 6 258 110 3.08 3.215
18.7 8 360 175 3.15 3.440
# column names with delimiters
dat <- data.frame(
  A_id = 1,
  A_a1 = 2,
  A_a2 = "3",
  B_b1 = 4,
  B_b2 = 5,
  B_C = 6
)
tt(dat) |> group_tt(j = "_")
A B
id a1 a2 b1 b2 C
1 2 3 4 5 6
# matrix insertion
rowmat <- matrix(colnames(iris))
tt(head(iris, 7)) |>
  group_tt(i = c(2, 5), j = rowmat)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
4.6 3.4 1.4 0.3 setosa
rowmat <- matrix(c(
  "a", "b", "c", "d", "e",
  1, 2, 3, 4, 5))
tt(head(iris, 7)) |>
  group_tt(i = 2, j = rowmat) |>
  style_tt(i = "groupi", background = "pink")
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
a b c d e
1 2 3 4 5
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
4.6 3.4 1.4 0.3 setosa