show_table()
Show a table in a Quarto page or Jupyter notebook
Makes a Markdown pipe table from a data frame, which Quarto turns into a real table in every output format (HTML, PDF, Word), styled like the page, so it stays small in a page with embed-resources. In a Quarto or R Markdown chunk it is shown as the table; in Jupyter (IRkernel) it is shown as Markdown; at the console it prints its Markdown. framework-py's show_table() gives the same Markdown for the same data.
Usage
show_table(data, caption = NULL, digits = NULL, big_mark = "", na = "")
Arguments
| Name | Default | Description |
|---|---|---|
| data |
A data frame (a tibble works too) or a matrix. |
|
| caption | NULL |
Character or NULL. A caption below the table, written as
Pandoc's |
| digits | NULL |
A whole number of at least 0, or NULL. Decimal places for
doubles, kept even when they are zero ( |
| big_mark | "" |
Character. The thousands separator for numbers, such as
|
| na | "" |
Character. What a missing value shows. Default: an empty cell. |
Value
The Markdown table, from knitr::asis_output(), so a Quarto or R Markdown chunk shows it as a table. It also has class framework_table, which prints its Markdown at the console.
Details
Numeric columns are right-aligned and others left-aligned. Every row is shown, so summarize first. Cells follow these rules, in order: a missing value shows na; TRUE and FALSE show as R prints them; integers get big_mark between groups of three digits; doubles show Inf or -Inf when infinite, digits decimals when digits is given (1.50) and the column has a number that isn't whole, and otherwise a whole number without decimals (1) and other numbers as format(x, digits = 15) prints them, with big_mark grouping the whole part (a number printed in exponent form, such as 1e-07, is left as is); anything else shows its text. Then | becomes \| and each run of whitespace, newlines included, becomes one space.
Row names are not shown: R tables, like tibbles, have no index. To show them, make them a column first, as in show_table(cbind(name = rownames(df), df)).
Examples
if (requireNamespace("knitr", quietly = TRUE)) {
counts <- data.frame(arm = c("a", "b"), n = c(1234L, 56L), rate = c(0.5, 0.25))
show_table(counts, caption = "Counts by arm", digits = 2, big_mark = ",")
}