Tables

Tables are a common way to summarize data in reports. Quarto allows you to produce tables directly in Markdown or dynamically from R. Using clear, readable tables improves the presentation of results and makes your documents more professional and accessible.

Text-Based Tables in Markdown

For simple tables, you can write them directly in Markdown using the pipe (|) syntax. This is ideal for small, static tables that do not require dynamic calculations.

Example:

| Group | Mean | SD  |
|-------|------|-----|
| A     | 5.2  | 1.1 |
| B     | 6.8  | 0.9 |
| C     | 4.9  | 1.3 |

Rendered output:

Group Mean SD
A 5.2 1.1
B 6.8 0.9
C 4.9 1.3
  • Use --- under headers for proper alignment.
  • Columns can be aligned left (: on left), right (: on right), or center (: on both sides):
| Group | Mean | SD  |
|:------|----:|:---:|
| A     | 5.2 | 1.1 |
| B     | 6.8 | 0.9 |
| C     | 4.9 | 1.3 |

Results in aligned columns like this:

Group Mean SD
A 5.2 1.1
B 6.8 0.9
C 4.9 1.3

Generating Tables from R with knitr::kable

For tables that come from your R data (e.g., data frames, summaries), Quarto integrates seamlessly with knitr::kable to produce clean, nicely formatted tables.

Example:

When producing tables are raw output, they will look like this:

library(knitr)

data <- data.frame(
  Group = c("A", "B", "C"),
  Mean  = c(5.2, 6.8, 4.9),
  SD    = c(1.1, 0.9, 1.3)
)

data
  Group Mean  SD
1     A  5.2 1.1
2     B  6.8 0.9
3     C  4.9 1.3

When using kable, they can be formatted more neatly:

kable(data, caption = "Summary statistics by group")
Summary statistics by group
Group Mean SD
A 5.2 1.1
B 6.8 0.9
C 4.9 1.3
  • caption adds a descriptive label for the table.
  • kable automatically aligns columns neatly.
  • For larger or more complex tables, you can also use kableExtra to add styling, colors, or column formatting.
library(kableExtra)

mean_score <- mean(data$Mean)

kable(data, caption = paste("Summary statistics (overall mean:", round(mean_score, 2), ")")) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE) %>%
  row_spec(0, bold = TRUE, color = "white", background = "#4CAF50")  # header row styling
Summary statistics (overall mean: 5.63 )
Group Mean SD
A 5.2 1.1
B 6.8 0.9
C 4.9 1.3
  • kable_styling(): Adds table styling options like striped rows, hover effect, and condensed spacing.
  • row_spec(0, ...): Styles the header row (0 = header) with bold text, white font, and a green background.
  • The caption dynamically includes the overall mean calculated from the data.

Rendered in HTML, this produces a professional, readable table that’s visually distinct and easier to interpret than a plain kable() table.

You can also add row colors, column formatting, or footnotes with other kableExtra functions for even more control.

Tips for Readable Tables

  1. Keep them concise: Avoid overcrowding a table with too many columns or unnecessary decimals.
  2. Label clearly: Use meaningful headers and, if needed, a descriptive caption.
  3. Round numbers: Format numeric results for readability.
  4. Consistent styling: For longer reports, stick with a single table style (Markdown or kable) for consistency.
External Resources

For more detail, see the Quarto documentation

Back to top