Skip to content

Table Structure

These functions control high-level layout: column spanners, the stub (row-label) column, and row groups.

tab_spanner!

Add a spanning header above a group of columns.

Signatures:

julia
tab_spanner!(tbl, (label => colgroup)::Pair...)
tab_spanner!(tbl, d::AbstractDict)
tab_spanner!(tbl, d::AbstractVector{<:Pair})
julia
using StyledTables, SummaryTables, DataFrames

df = DataFrame(
    drug = ["Aspirin", "Ibuprofen", "Naproxen"],
    dose_mg = [100, 200, 250],
    efficacy = [0.72, 0.81, 0.78],
    safety = [0.91, 0.84, 0.88],
)

tbl = StyledTable(df)
tab_spanner!(tbl, "Outcomes" => [:efficacy, :safety])
cols_label!(tbl,
    :drug => "Drug",
    :dose_mg => "Dose (mg)",
    :efficacy => "Efficacy",
    :safety => "Safety",
)
fmt_percent!(tbl, [:efficacy, :safety]; digits = 1)
render(tbl)
Outcomes
Drug Dose (mg) Efficacy Safety
Aspirin 100 72.0% 91.0%
Ibuprofen 200 81.0% 84.0%
Naproxen 250 78.0% 88.0%

Multiple spanners can be added at once:

julia
tbl = StyledTable(df)
tab_spanner!(tbl, "Dosing" => [:dose_mg], "Outcomes" => [:efficacy, :safety])
render(tbl)
Dosing Outcomes
drug dose_mg efficacy safety
Aspirin 100 0.72 0.91
Ibuprofen 200 0.81 0.84
Naproxen 250 0.78 0.88

Multi-line spanner header using Multiline:

julia
tbl = StyledTable(df)
tab_spanner!(tbl, Multiline("Outcomes", "(primary)") => [:efficacy, :safety])
render(tbl)
Outcomes
(primary)
drug dose_mg efficacy safety
Aspirin 100 0.72 0.91
Ibuprofen 200 0.81 0.84
Naproxen 250 0.78 0.88
StyledTables.tab_spanner! Method
julia
tab_spanner!(
    tbl::StyledTable,
    args::Pair...;
    level
) -> StyledTable

Add one or more spanning header labels above groups of columns.

Each spanner is given as a label => columns pair, where label is the spanner text (a String, SummaryTables.Multiline, or any value accepted by SummaryTables.Cell) and columns is a Vector{Symbol} of column names to span.

Use the level keyword to stack spanners in multiple rows: level = 1 (the default) is the bottom-most row, closest to the column labels; level = 2 sits above it, and so on. A higher-level spanner's column set must fully contain every lower-level spanner it overlaps.

Arguments

  • tbl: the StyledTable to modify.

  • args: one or more label => column(s) pairs.

  • level: the spanner row (default 1).

Returns

tbl (modified in place).

See also: tab_header!, tab_stub!.

Examples

julia
tbl = StyledTable(df)
tab_spanner!(tbl, "Outcomes" => [:efficacy, :safety])
render(tbl)

tbl = StyledTable(df)
tab_spanner!(tbl, "Length (mm)" => [:bill_len, :bill_depth, :flipper_len])
tab_spanner!(tbl, "Physical measurements" => [:bill_len, :bill_depth, :flipper_len, :body_mass]; level = 2)
render(tbl)

using SummaryTables: Multiline
tbl = StyledTable(df)
tab_spanner!(tbl, Multiline("Treatment", "(N=50)") => [:dose, :response])
render(tbl)
source
StyledTables.tab_spanner! Method
julia
tab_spanner!(
    tbl::StyledTable,
    d::Union{AbstractDict{Symbol, Vector{Symbol}}, AbstractDict{Symbol, Symbol}, AbstractDict{Multiline, Symbol}, AbstractDict{<:AbstractString, <:AbstractVector{<:AbstractString}}, AbstractDict{Symbol, <:AbstractVector{<:AbstractString}}, AbstractDict{<:AbstractString, Vector{Symbol}}, AbstractDict{<:AbstractString, <:AbstractString}, AbstractDict{Symbol, <:AbstractString}, AbstractDict{<:AbstractString, Symbol}, AbstractDict{Multiline, <:AbstractString}, AbstractVector{<:Pair{<:AbstractString, <:AbstractVector{<:AbstractString}}}, AbstractVector{<:Pair{Symbol, Vector{Symbol}}}, AbstractVector{<:Pair{Symbol, <:AbstractVector{<:AbstractString}}}, AbstractVector{<:Pair{<:AbstractString, Vector{Symbol}}}, AbstractVector{<:Pair{<:AbstractString, <:AbstractString}}, AbstractVector{<:Pair{Symbol, <:AbstractString}}, AbstractVector{<:Pair{<:AbstractString, Symbol}}, AbstractVector{<:Pair{Symbol, Symbol}}, AbstractVector{<:Pair{Multiline, Symbol}}, AbstractVector{<:Pair{Multiline, <:AbstractString}}};
    level
) -> StyledTable

Add spanning headers from a dict or vector of pairs.

Arguments

  • tbl: the StyledTable to modify.

  • d: an AbstractDict or AbstractVector pairing spanner labels to column names.

  • level: the spanner row applied to every entry in d (default 1).

Returns

tbl (modified in place).

See also: tab_spanner!, tab_header!, tab_stub!.

Examples

julia
tbl = StyledTable(df)
tab_spanner!(tbl, Dict(
    "Outcomes"     => [:efficacy, :safety],
    "Demographics" => [:age, :sex])
)
render(tbl)
source

tab_stub!

Mark one column as the stub: a row-label column rendered apart from data columns. By default the stub header is not bolded; it gains a bold label if tab_stubhead! is called.

Signature: tab_stub!(tbl, col::Symbol)

julia
tbl = StyledTable(df)
tab_stub!(tbl, :drug)
cols_label!(tbl, :dose_mg => "Dose (mg)", :efficacy => "Efficacy", :safety => "Safety")
render(tbl)
Dose (mg) Efficacy Safety
Aspirin 100 0.72 0.91
Ibuprofen 200 0.81 0.84
Naproxen 250 0.78 0.88
StyledTables.tab_stub! Function
julia
tab_stub!(tbl::StyledTable, col::Symbol) -> StyledTable

Mark a column as the stub (row-label column).

The stub header is not bolded. Use tab_stubhead! to label it.

Arguments

  • tbl: the StyledTable to modify.

  • col: name of the column to use as the stub.

Returns

tbl (modified in place).

See also: tab_stubhead!, tab_rowgroup!.

Examples

julia
tbl = StyledTable(df)
tab_stub!(tbl, :drug)
render(tbl)
source

tab_stubhead!

Label the stub column header. Has no effect without a prior call to tab_stub!.

Signature: tab_stubhead!(tbl, label)

julia
tbl = StyledTable(df)
tab_stub!(tbl, :drug)
tab_stubhead!(tbl, "Drug Name")
render(tbl)
Drug Name dose_mg efficacy safety
Aspirin 100 0.72 0.91
Ibuprofen 200 0.81 0.84
Naproxen 250 0.78 0.88
StyledTables.tab_stubhead! Function
julia
tab_stubhead!(tbl::StyledTable, label) -> StyledTable

Set the stub column header label.

Requires a prior call to tab_stub!.

Arguments

  • tbl: the StyledTable to modify.

  • label: display text for the stub header cell.

Returns

tbl (modified in place).

See also: tab_stub!.

Examples

julia
tbl = StyledTable(df)
tab_stub!(tbl, :drug)
tab_stubhead!(tbl, "Drug Name")
render(tbl)
source

tab_rowgroup!

Group rows by distinct values in a column. A bold group-label row precedes each new group. Data rows are indented.

Signature: tab_rowgroup!(tbl, col::Symbol; indent_pt = 12)

julia
df = DataFrame(
    category = ["Analgesic", "Analgesic", "NSAID", "NSAID"],
    drug = ["Aspirin", "Paracetamol", "Ibuprofen", "Naproxen"],
    dose_mg = [100, 500, 200, 250],
)

tbl = StyledTable(df)
tab_rowgroup!(tbl, :category)
cols_hide!(tbl, :category)
cols_label!(tbl, :drug => "Drug", :dose_mg => "Dose (mg)")
render(tbl)
Drug Dose (mg)
Analgesic
Aspirin 100
Paracetamol 500
NSAID
Ibuprofen 200
Naproxen 250

To increase indentation:

julia
tbl = StyledTable(df)
tab_rowgroup!(tbl, :category; indent_pt = 20)
cols_hide!(tbl, :category)
render(tbl)
drug dose_mg
Analgesic
Aspirin 100
Paracetamol 500
NSAID
Ibuprofen 200
Naproxen 250
StyledTables.tab_rowgroup! Function
julia
tab_rowgroup!(
    tbl::StyledTable,
    col::Symbol;
    indent_pt,
    full_width
) -> StyledTable

Group rows by distinct values in a column.

A bold group-label row precedes each new group value. Data rows are indented by indent_pt points. Hide the grouping column afterwards with cols_hide!.

Arguments

  • tbl: the StyledTable to modify.

  • col: column whose distinct values define the groups.

Keywords

  • indent_pt: left indent for data rows within a group (default 12).

  • full_width: whether group-label rows span the entire table width (default false).

Returns

tbl (modified in place).

See also: cols_hide!, tab_stub!.

Examples

julia
tbl = StyledTable(df)
tab_rowgroup!(tbl, :category)
cols_hide!(tbl, :category)
render(tbl)
source