The way that we add components like the Table Header and source notes in the Table Footer is to use the tab_*() family of methods. A Table Header is easy to add so let's see how the previous table looks with a title and a subtitle. We can add this component using the GT.tab_header() method:
import tabelline, gtdata
let islandsMini = islands().head(10)
# Make a display table with the `islands_tbl` table;# put a heading just above the column labelslet withHeader = tabella(islandsMini):
tabHeader title = "Large Landmasses of the World",
subtitle = "The top ten largest are presented"
nbTable withHeader
Large Landmasses of the World
The top ten largest are presented
name
size
Africa
11506
Antarctica
5500
Asia
16988
Australia
2968
Axel Heiberg
16
Baffin
184
Banks
23
Borneo
280
Britain
84
Celebes
73
from great_tables import GT, md, html
from great_tables.data import islands
islands_mini = islands.head(10)
# Make a display table with the `islands_tbl` table;# put a heading just above the column labels
(
GT(islands_mini)
.tab_header(
title = "Large Landmasses of the World",
subtitle = "The top ten largest are presented"
)
)
Large Landmasses of the World
The top ten largest are presented
name
size
Africa
11506
Antarctica
5500
Asia
16988
Australia
2968
Axel Heiberg
16
Baffin
184
Banks
23
Borneo
280
Britain
84
Celebes
73
library(gt)
library(dplyr)
islands_tbl <- tibble(name = names(islands), size = islands)
islands_mini <- islands_tbl |> slice_head(n = 10)
# Make a display table with the `islands_tbl` table;# put a heading just above the column labels
gt(islands_mini) |>
tab_header(
title = "Large Landmasses of the World",
subtitle = "The top ten largest are presented"
)
Large Landmasses of the World
The top ten largest are presented
name
size
Africa
11506
Antarctica
5500
Asia
16988
Australia
2968
Axel Heiberg
16
Baffin
184
Banks
23
Borneo
280
Britain
84
Celebes
73
The Header table component provides an opportunity to describe the data that's presented. Using subtitle= allows us to insert a subtitle, which is an optional part of the Header. We may also style the title= and subtitle= using Markdown! We do this by wrapping the values passed to title= or subtitle= with the md() helper function (we may also use html() in a similar fashion). Here is an example with the table data truncated for brevity:
# Make a display table with the `islands_tbl` table;# put a heading just above the column labelslet gtTbl = tabella(islands().head(2)):
tabHeader title = md("Large Landmasses of the *World* 🌐"),
subtitle = md("The top **ten** largest are presented")
nbTable gtTbl
Large Landmasses of the World 🌐
The top ten largest are presented
name
size
Africa
11506
Antarctica
5500
# Make a display table with the `islands_tbl` table;# put a heading just above the column labels
gt_tbl = (
GT(islands.head(2))
.tab_header(
title = md("Large Landmasses of the *World* 🌐"),
subtitle = md("The top **ten** largest are presented")
)
)
gt_tbl
Large Landmasses of the World 🌐
The top ten largest are presented
name
size
Africa
11506
Antarctica
5500
# Make a display table with the `islands_tbl` table;# put a heading just above the column labels
gt_tbl <- gt(islands_tbl |> slice_head(n = 2)) |>
tab_header(
title = md("Large Landmasses of the *World* 🌐"),
subtitle = md("The top **ten** largest are presented")
)
gt_tbl
Large Landmasses of the World
The top ten largest are presented
name
size
Africa
11506
Antarctica
5500
A source note can be added to the table's Footer through use of the GT.tab_source_note() method. It works in the same way as GT.tab_header() (it also allows for Markdown inputs) except it can be called multiple times---each invocation results in the addition of a source note.
# Display the `islands_tbl` data with a heading and two source noteslet withNotes = tabella(gtTbl):
tabSourceNote "Source: The World Almanac and Book of Facts, 1975, page 406."
tabSourceNote md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
nbTable withNotes
Large Landmasses of the World 🌐
The top ten largest are presented
name
size
Africa
11506
Antarctica
5500
Source: The World Almanac and Book of Facts, 1975, page 406.
Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley.
# Display the `islands_tbl` data with a heading and two source notes
(
gt_tbl
.tab_source_note(
source_note = "Source: The World Almanac and Book of Facts, 1975, page 406."
)
.tab_source_note(
source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
)
)
Large Landmasses of the World 🌐
The top ten largest are presented
name
size
Africa
11506
Antarctica
5500
Source: The World Almanac and Book of Facts, 1975, page 406.
Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley.
# Display the `islands_tbl` data with a heading and two source notes
gt_tbl |>
tab_source_note(
source_note = "Source: The World Almanac and Book of Facts, 1975, page 406."
) |>
tab_source_note(
source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
)
Large Landmasses of the World
The top ten largest are presented
name
size
Africa
11506
Antarctica
5500
Source: The World Almanac and Book of Facts, 1975, page 406.
Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley.
import tabbook
bookInit()
nbText: "# Header and Footer"
attribution gtGuide("Header and Footer", "basic-header", "")
nbText: """
The way that we add components like the **Table Header** and *source notes* in the **Table Footer** is to use the `tab_*()` family of methods. A **Table Header** is easy to add so let's see how the previous table looks with a *title* and a *subtitle*. We can add this component using the `GT.tab_header()` method:
"""
tabs """
from great_tables import GT, md, html
from great_tables.data import islands
islands_mini = islands.head(10)
# Make a display table with the `islands_tbl` table;
# put a heading just above the column labels
(
GT(islands_mini)
.tab_header(
title = "Large Landmasses of the World",
subtitle = "The top ten largest are presented"
)
)
""", """
library(gt)
library(dplyr)
islands_tbl <- tibble(name = names(islands), size = islands)
islands_mini <- islands_tbl |> slice_head(n = 10)
# Make a display table with the `islands_tbl` table;
# put a heading just above the column labels
gt(islands_mini) |>
tab_header(
title = "Large Landmasses of the World",
subtitle = "The top ten largest are presented"
)
""":
nbCode:
import tabelline, gtdata
let islandsMini = islands().head(10)
# Make a display table with the `islands_tbl` table;# put a heading just above the column labelslet withHeader = tabella(islandsMini):
tabHeader title = "Large Landmasses of the World",
subtitle = "The top ten largest are presented"
nbTable withHeader
nbText: """
The **Header** table component provides an opportunity to describe the data that's presented. Using `subtitle=` allows us to insert a subtitle, which is an optional part of the **Header**. We may also style the `title=` and `subtitle=` using Markdown! We do this by wrapping the values passed to `title=` or `subtitle=` with the `md()` helper function (we may also use `html()` in a similar fashion). Here is an example with the table data truncated for brevity:
"""
tabs """
# Make a display table with the `islands_tbl` table;
# put a heading just above the column labels
gt_tbl = (
GT(islands.head(2))
.tab_header(
title = md("Large Landmasses of the *World* 🌐"),
subtitle = md("The top **ten** largest are presented")
)
)
gt_tbl
""", """
# Make a display table with the `islands_tbl` table;
# put a heading just above the column labels
gt_tbl <- gt(islands_tbl |> slice_head(n = 2)) |>
tab_header(
title = md("Large Landmasses of the *World* 🌐"),
subtitle = md("The top **ten** largest are presented")
)
gt_tbl
""":
nbCode:
# Make a display table with the `islands_tbl` table;# put a heading just above the column labelslet gtTbl = tabella(islands().head(2)):
tabHeader title = md("Large Landmasses of the *World* 🌐"),
subtitle = md("The top **ten** largest are presented")
nbTable gtTbl
tabelline """
tabelline's `md()` is the Markdown of a table's texts: emphasis (`*` and `_`), code, links and
`<https://...>`, `~~struck~~`, `~sub~`, `^sup^`, HTML entities such as the globe's `🌐`,
paragraphs and lists. Nim strings are UTF-8, so the globe may also go in as it is, 🌐. Headings,
tables and the rest of CommonMark are not for a cell.
"""
nbText: """
A *source note* can be added to the table's **Footer** through use of the `GT.tab_source_note()` method. It works in the same way as `GT.tab_header()` (it also allows for Markdown inputs) except it can be called multiple times---each invocation results in the addition of a source note.
"""
tabs """
# Display the `islands_tbl` data with a heading and two source notes
(
gt_tbl
.tab_source_note(
source_note = "Source: The World Almanac and Book of Facts, 1975, page 406."
)
.tab_source_note(
source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
)
)
""", """
# Display the `islands_tbl` data with a heading and two source notes
gt_tbl |>
tab_source_note(
source_note = "Source: The World Almanac and Book of Facts, 1975, page 406."
) |>
tab_source_note(
source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
)
""":
nbCode:
# Display the `islands_tbl` data with a heading and two source noteslet withNotes = tabella(gtTbl):
tabSourceNote "Source: The World Almanac and Book of Facts, 1975, page 406."
tabSourceNote md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
nbTable withNotes
bookSave()