Column Modifiers
These functions control column display names, alignment, and visibility.
relabel!
Rename one or more columns for display. The underlying DataFrame is unchanged.
Signature: relabel!(tbl, (col => label)::Pair...)
using StyledTables, DataFrames
df = DataFrame(bmi = [22.1, 27.4, 31.0], sbp = [118, 135, 142])
tbl = StyledTable(df)
relabel!(tbl, :bmi => "BMI (kg/m²)", :sbp => "Systolic BP (mmHg)")
render(tbl)| BMI (kg/m²) | Systolic BP (mmHg) |
| 22.1 | 118 |
| 27.4 | 135 |
| 31 | 142 |
Use Multiline headers via SummaryTables.Multiline:
using SummaryTables: Multiline
tbl = StyledTable(df)
relabel!(tbl,
:bmi => Multiline("BMI", "(kg/m²)"),
:sbp => Multiline("Systolic BP", "(mmHg)"),
)
render(tbl)| BMI (kg/m²) |
Systolic BP (mmHg) |
| 22.1 | 118 |
| 27.4 | 135 |
| 31 | 142 |
Apply a function uniformly to all column names:
df2 = DataFrame(bmi_score = [22.1, 27.4], sbp_mmhg = [118, 135])
tbl = StyledTable(df2)
relabel!(col -> titlecase(replace(col, "_" => " ")), tbl)
render(tbl)| Bmi Score | Sbp Mmhg |
| 22.1 | 118 |
| 27.4 | 135 |
Restrict to a subset of columns with a Symbol, String or a Vector thereof:
tbl = StyledTable(df2)
relabel!(tbl, :bmi_score) do col
titlecase(replace(col, "_" => " "))
end
render(tbl)| Bmi Score | sbp_mmhg |
| 22.1 | 118 |
| 27.4 | 135 |
StyledTables.relabel! Method
relabel!(tbl::StyledTable, args::Pair...) -> StyledTableRename one or more columns in the rendered output.
Arguments
tbl: theStyledTableto modify.args: any number ofcol => labelpairs.colmust be aSymbolmatching a column name;labelcan be a plainStringor any value accepted bySummaryTables.Cell, includingMultilinefor multi-line headers.
Returns
tbl (modified in place).
Notes
The underlying DataFrame is unchanged.
Examples
tbl = StyledTable(df)
relabel!(tbl, :bmi => "BMI (kg/m²)", :sbp => "Systolic BP")
render(tbl)StyledTables.relabel! Method
relabel!(
tbl::StyledTable,
d::Union{AbstractDict{Symbol, Symbol}, AbstractDict{<:AbstractString, <:AbstractString}, AbstractDict{<:AbstractString, Symbol}, AbstractDict{Symbol, <:AbstractString}, AbstractVector{<:Pair{Symbol, Symbol}}, AbstractVector{<:Pair{<:AbstractString, <:AbstractString}}, AbstractVector{<:Pair{<:AbstractString, Symbol}}}
) -> StyledTableRename columns using a dict or vector of pairs.
Arguments
tbl: theStyledTableto modify.d: aDictor vector ofcol => labelpairs specifying columns and their labels.
Returns
tbl (modified in place).
Examples
label_dict = Dict(:bmi => "BMI (kg/m²)", :sbp => "Systolic BP")
tbl = StyledTable(df)
relabel!(tbl, label_dict)
render(tbl)StyledTables.relabel! Method
relabel!(
f::Function,
tbl::StyledTable,
columns::AbstractVector{Symbol}
) -> StyledTableRelabel columns by applying a function to each column name.
f(col::String) -> label receives the column name as a String and returns any value accepted by the pair form: a String, or any Cell-compatible value such as Multiline.
Arguments
f: function mapping a column nameStringto a label value.tbl: theStyledTableto modify.columns: optional column selector. Pass aSymbol,String, or aVectorof either to restrict which columns are relabeled; omit to applyfto all columns.
Returns
tbl (modified in place).
Examples
tbl = StyledTable(df)
relabel!(uppercase, tbl)
render(tbl)align!
Set horizontal alignment for one or more columns. Valid values: :left, :center, :right.
Signatures:
align!(tbl, (cols => halign)::Pair...)
align!(tbl, d::AbstractDict)
align!(tbl, d::AbstractVector{<:Pair})
align!(tbl, halign)tbl = StyledTable(df)
align!(tbl, :bmi => :right, :sbp => :left)
render(tbl)| bmi | sbp |
| 22.1 | 118 |
| 27.4 | 135 |
| 31 | 142 |
Align a group of columns to the same alignment in one call:
tbl = StyledTable(df)
align!(tbl, [:bmi, :sbp] => :right)
render(tbl)| bmi | sbp |
| 22.1 | 118 |
| 27.4 | 135 |
| 31 | 142 |
Align all columns at once:
tbl = StyledTable(df)
align!(tbl, :center)
render(tbl)| bmi | sbp |
| 22.1 | 118 |
| 27.4 | 135 |
| 31 | 142 |
Align only Int columns:
tbl = StyledTable(df)
align!(tbl, names(df, Int) => :center)
render(tbl)| bmi | sbp |
| 22.1 | 118 |
| 27.4 | 135 |
| 31 | 142 |
StyledTables.align! Method
align!(tbl::StyledTable, args::Pair...) -> StyledTableSet horizontal alignment for one or more columns.
Each argument is a col => halign pair, where col is a Symbol matching a column name and halign is one of :left, :center, or :right.
Arguments
tbl: theStyledTableto modify.args: one or morecol => halignpairs.
Returns
tbl (modified in place).
Examples
tbl = StyledTable(df)
align!(tbl, :x => :right, :y => :center)
render(tbl)StyledTables.align! Method
align!(tbl::StyledTable, args::Pair...) -> StyledTableSet horizontal alignment for one or more columns.
Each argument is a col => halign pair, where col is a Symbol matching a column name and halign is one of :left, :center, or :right.
Arguments
tbl: theStyledTableto modify.args: one or morecol => halignpairs.
Returns
tbl (modified in place).
Examples
tbl = StyledTable(df)
align!(tbl, :x => :right, :y => :center)
render(tbl)StyledTables.align! Method
align!(tbl::StyledTable, halign::Symbol) -> StyledTableSet the same horizontal alignment for all columns.
Arguments
tbl: theStyledTableto modify.halign: one of:left,:center, or:right.
Returns
tbl (modified in place).
Examples
tbl = StyledTable(df)
align!(tbl, :center)
render(tbl)hide!
Remove columns from the rendered table without dropping them from the data. Use this when a column drives grouping (via rowgroup!) but should not appear.
Signature: hide!(tbl, cols::Symbol...)
df = DataFrame(
group = ["A", "A", "B", "B"],
subject = ["S1", "S2", "S3", "S4"],
score = [88, 92, 75, 84],
pct_score = [0.88, 0.92, 0.75, 0.84],
)
tbl = StyledTable(df)
rowgroup!(tbl, :group)
hide!(tbl, :group)
relabel!(tbl, :subject => "Subject", :score => "Score", :pct_score => "Score (%)")
format!(PercentFormatter(digits = 0), tbl, :pct_score)
render(tbl)| Subject | Score | Score (%) |
| A | ||
| S1 | 88 | 88% |
| S2 | 92 | 92% |
| B | ||
| S3 | 75 | 75% |
| S4 | 84 | 84% |
StyledTables.hide! Function
hide!(tbl::StyledTable, cols::Symbol...) -> StyledTableRemove columns from the rendered output without modifying the DataFrame.
Hidden columns remain accessible for grouping or formatting, but do not appear in the rendered table. Commonly paired with rowgroup!.
Arguments
tbl: theStyledTableto modify.cols: one or more column names to hide.
Returns
tbl (modified in place).
See also: rowgroup!.
Examples
tbl = StyledTable(df)
rowgroup!(tbl, :group)
hide!(tbl, :group)
render(tbl)