Table testing

Playing around with tables generated using various table packages.

Markdown tables

Examples from the Quarto tables documentation.

Demonstration of pipe table syntax
Default Left Right Center
12 12 12 12
123 123 123 123
1 1 1 1

Subtables

Table 1: Main Caption

(a) First Table
Col1 Col2 Col3
A B C
E F G
A G G
(b) Second Table
Col1 Col2 Col3
A B C
E F G
A G G

See Table 1 for details, especially Table 1 (b).

Grid tables

Sample grid table.
Fruit Price Advantages
Bananas $1.34
  • built-in wrapper
  • bright color
Oranges $2.10
  • cures scurvy
  • tasty

knitr::kable()

View code
knitr::kable(head(mtcars), "html") %>%
  kable_styling(bootstrap_options = "striped")
Table 2: Basic mtcars table with caption set from chunk options
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

Table with kable() caption

View code
#|: knitr-w-caption
iris2 <- head(iris)
knitr::kable(iris2, caption = "An example table caption.") %>%
  kable_styling(bootstrap_options = "striped")
An example table caption.
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
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

{kableExtra}

Grouped rows and columns

add_header_above()

View code
iris2 <- iris[1:5, c(1, 3, 2, 4, 5)]
names(iris2) <- gsub("[.].+", "", names(iris2))
kable(iris2, booktabs = TRUE, align = "rrrrc") %>%
  kable_styling(bootstrap_options = "striped") %>%
  add_header_above(c("Length" = 2, "Width" = 2, " " = 1)) %>%
  add_header_above(c("Measurements" = 4, "More attributes" = 1))
Measurements
More attributes
Length
Width
Sepal Petal Sepal Petal Species
5.1 1.4 3.5 0.2 setosa
4.9 1.4 3.0 0.2 setosa
4.7 1.3 3.2 0.2 setosa
4.6 1.5 3.1 0.2 setosa
5.0 1.4 3.6 0.2 setosa

pack_rows()

View code
iris3 <- iris[c(1:2, 51:54, 101:103), ]
kable(iris3[, 1:4], booktabs = TRUE) %>%
  kable_styling(bootstrap_options = "striped") %>%
  pack_rows(
    index = c("setosa" = 2, "versicolor" = 4, "virginica" = 3)
  )
Sepal.Length Sepal.Width Petal.Length Petal.Width
setosa
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
versicolor
51 7.0 3.2 4.7 1.4
52 6.4 3.2 4.5 1.5
53 6.9 3.1 4.9 1.5
54 5.5 2.3 4.0 1.3
virginica
101 6.3 3.3 6.0 2.5
102 5.8 2.7 5.1 1.9
103 7.1 3.0 5.9 2.1

DT

With row names

View code
library(DT)
datatable(head(mtcars))

Without row names

View code
datatable(head(mtcars), rownames = FALSE)  # no row names

Custom table container

View code
# a custom table container
sketch = htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th(rowspan = 2, 'Species'),
      th(colspan = 2, 'Sepal'),
      th(colspan = 2, 'Petal')
    ),
    tr(
      lapply(rep(c('Length', 'Width'), 2), th)
    )
  )
))

# use rownames = FALSE here because we did not generate a cell for row names in
# the header, and the header only contains five columns
datatable(iris[1:20, c(5, 1:4)], container = sketch, rownames = FALSE)

HTML

As always, you can include raw HTML in a Quarto document, and it will be rendered. Below is a revised version Table 2, modified to use scope="col" and scope="row" for the column and row <th> cells, as described in the W3C WAI tutorial “Tables with Two Headers.”

HTML table of mtcars
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

More

For examples using {gt}, see Experiments with gt. For examples using the tabulate package, see Python tabulate.