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:
tab_spanner!(tbl, (label => colgroup)::Pair...)
tab_spanner!(tbl, d::AbstractDict)
tab_spanner!(tbl, d::AbstractVector{<:Pair})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:
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:
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
tab_spanner!(
tbl::StyledTable,
args::Pair...;
level
) -> StyledTableAdd 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: theStyledTableto modify.args: one or morelabel => column(s)pairs.level: the spanner row (default1).
Returns
tbl (modified in place).
See also: tab_header!, tab_stub!.
Examples
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)StyledTables.tab_spanner! Method
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
) -> StyledTableAdd spanning headers from a dict or vector of pairs.
Arguments
tbl: theStyledTableto modify.d: anAbstractDictorAbstractVectorpairing spanner labels to column names.level: the spanner row applied to every entry ind(default1).
Returns
tbl (modified in place).
See also: tab_spanner!, tab_header!, tab_stub!.
Examples
tbl = StyledTable(df)
tab_spanner!(tbl, Dict(
"Outcomes" => [:efficacy, :safety],
"Demographics" => [:age, :sex])
)
render(tbl)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)
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
tab_stub!(tbl::StyledTable, col::Symbol) -> StyledTableMark a column as the stub (row-label column).
The stub header is not bolded. Use tab_stubhead! to label it.
Arguments
tbl: theStyledTableto modify.col: name of the column to use as the stub.
Returns
tbl (modified in place).
See also: tab_stubhead!, tab_rowgroup!.
Examples
tbl = StyledTable(df)
tab_stub!(tbl, :drug)
render(tbl)tab_stubhead!
Label the stub column header. Has no effect without a prior call to tab_stub!.
Signature: tab_stubhead!(tbl, label)
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
tab_stubhead!(tbl::StyledTable, label) -> StyledTableSet the stub column header label.
Requires a prior call to tab_stub!.
Arguments
tbl: theStyledTableto modify.label: display text for the stub header cell.
Returns
tbl (modified in place).
See also: tab_stub!.
Examples
tbl = StyledTable(df)
tab_stub!(tbl, :drug)
tab_stubhead!(tbl, "Drug Name")
render(tbl)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)
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:
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
tab_rowgroup!(
tbl::StyledTable,
col::Symbol;
indent_pt,
full_width
) -> StyledTableGroup 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: theStyledTableto modify.col: column whose distinct values define the groups.
Keywords
indent_pt: left indent for data rows within a group (default12).full_width: whether group-label rows span the entire table width (defaultfalse).
Returns
tbl (modified in place).
See also: cols_hide!, tab_stub!.
Examples
tbl = StyledTable(df)
tab_rowgroup!(tbl, :category)
cols_hide!(tbl, :category)
render(tbl)