Column Modifiers
These functions control column display names, alignment, and visibility.
cols_label!
Rename one or more columns for display. The underlying DataFrame is unchanged.
Signature: cols_label!(tbl, (col => label)::Pair...)
using StyledTables, DataFrames
df = DataFrame(bmi = [22.1, 27.4, 31.0], sbp = [118, 135, 142])
tbl = StyledTable(df)
cols_label!(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)
cols_label!(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)
cols_label!(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)
cols_label!(tbl, :bmi_score) do col
titlecase(replace(col, "_" => " "))
end
render(tbl)| Bmi Score | sbp_mmhg |
| 22.1 | 118 |
| 27.4 | 135 |
StyledTables.cols_label! Method
cols_label!(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.
See also: cols_align!, cols_hide!.
Examples
tbl = StyledTable(df)
cols_label!(tbl, :bmi => "BMI (kg/m²)", :sbp => "Systolic BP")
render(tbl)StyledTables.cols_label! Method
cols_label!(
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).
See also: cols_align!, cols_hide!.
Examples
label_dict = Dict(:bmi => "BMI (kg/m²)", :sbp => "Systolic BP")
tbl = StyledTable(df)
cols_label!(tbl, label_dict)
render(tbl)StyledTables.cols_label! Method
cols_label!(
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).
See also: cols_align!, cols_hide!.
Examples
tbl = StyledTable(df)
cols_label!(uppercase, tbl)
render(tbl)cols_align!
Set horizontal alignment for one or more columns. Valid values: :left, :center, :right.
Signatures:
cols_align!(tbl, (cols => halign)::Pair...)
cols_align!(tbl, d::AbstractDict)
cols_align!(tbl, d::AbstractVector{<:Pair})
cols_align!(tbl, halign)tbl = StyledTable(df)
cols_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)
cols_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)
cols_align!(tbl, :center)
render(tbl)| bmi | sbp |
| 22.1 | 118 |
| 27.4 | 135 |
| 31 | 142 |
Align only Int columns:
tbl = StyledTable(df)
cols_align!(tbl, names(df, Int) => :center)
render(tbl)| bmi | sbp |
| 22.1 | 118 |
| 27.4 | 135 |
| 31 | 142 |
StyledTables.cols_align! Method
cols_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)
cols_align!(tbl, :x => :right, :y => :center)
render(tbl)StyledTables.cols_align! Method
cols_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)
cols_align!(tbl, :x => :right, :y => :center)
render(tbl)StyledTables.cols_align! Method
cols_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)
cols_align!(tbl, :center)
render(tbl)cols_hide!
Remove columns from the rendered table without dropping them from the data. Use this when a column drives grouping (via tab_rowgroup!) but should not appear.
Signature: cols_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)
tab_rowgroup!(tbl, :group)
cols_hide!(tbl, :group)
cols_label!(tbl, :subject => "Subject", :score => "Score", :pct_score => "Score (%)")
fmt_percent!(tbl, :pct_score; digits = 0)
render(tbl)| Subject | Score | Score (%) |
| A | ||
| S1 | 88 | 88% |
| S2 | 92 | 92% |
| B | ||
| S3 | 75 | 75% |
| S4 | 84 | 84% |
StyledTables.cols_hide! Function
cols_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 tab_rowgroup!.
Arguments
tbl: theStyledTableto modify.cols: one or more column names to hide.
Returns
tbl (modified in place).
See also: tab_rowgroup!.
Examples
tbl = StyledTable(df)
tab_rowgroup!(tbl, :group)
cols_hide!(tbl, :group)
render(tbl)