Skip to content

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...)

julia
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:

julia
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:

julia
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:

julia
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
julia
cols_label!(tbl::StyledTable, args::Pair...) -> StyledTable

Rename one or more columns in the rendered output.

Arguments

  • tbl: the StyledTable to modify.

  • args: any number of col => label pairs. col must be a Symbol matching a column name; label can be a plain String or any value accepted by SummaryTables.Cell, including Multiline for multi-line headers.

Returns

tbl (modified in place).

Notes

The underlying DataFrame is unchanged.

See also: cols_align!, cols_hide!.

Examples

julia
tbl = StyledTable(df)
cols_label!(tbl, :bmi => "BMI (kg/m²)", :sbp => "Systolic BP")
render(tbl)
source
StyledTables.cols_label! Method
julia
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}}}
) -> StyledTable

Rename columns using a dict or vector of pairs.

Arguments

  • tbl: the StyledTable to modify.

  • d: a Dict or vector of col => label pairs specifying columns and their labels.

Returns

tbl (modified in place).

See also: cols_align!, cols_hide!.

Examples

julia
label_dict = Dict(:bmi => "BMI (kg/m²)", :sbp => "Systolic BP")
tbl = StyledTable(df)
cols_label!(tbl, label_dict)
render(tbl)
source
StyledTables.cols_label! Method
julia
cols_label!(
    f::Function,
    tbl::StyledTable,
    columns::AbstractVector{Symbol}
) -> StyledTable

Relabel 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 name String to a label value.

  • tbl: the StyledTable to modify.

  • columns: optional column selector. Pass a Symbol, String, or a Vector of either to restrict which columns are relabeled; omit to apply f to all columns.

Returns

tbl (modified in place).

See also: cols_align!, cols_hide!.

Examples

julia
tbl = StyledTable(df)
cols_label!(uppercase, tbl)
render(tbl)
source

cols_align!

Set horizontal alignment for one or more columns. Valid values: :left, :center, :right.

Signatures:

julia
cols_align!(tbl, (cols => halign)::Pair...)
cols_align!(tbl, d::AbstractDict)
cols_align!(tbl, d::AbstractVector{<:Pair})
cols_align!(tbl, halign)
julia
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:

julia
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:

julia
tbl = StyledTable(df)
cols_align!(tbl, :center)
render(tbl)
bmi sbp
22.1 118
27.4 135
31 142

Align only Int columns:

julia
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
julia
cols_align!(tbl::StyledTable, args::Pair...) -> StyledTable

Set 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: the StyledTable to modify.

  • args: one or more col => halign pairs.

Returns

tbl (modified in place).

Examples

julia
tbl = StyledTable(df)
cols_align!(tbl, :x => :right, :y => :center)
render(tbl)
source
StyledTables.cols_align! Method
julia
cols_align!(tbl::StyledTable, args::Pair...) -> StyledTable

Set 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: the StyledTable to modify.

  • args: one or more col => halign pairs.

Returns

tbl (modified in place).

Examples

julia
tbl = StyledTable(df)
cols_align!(tbl, :x => :right, :y => :center)
render(tbl)
source
StyledTables.cols_align! Method
julia
cols_align!(tbl::StyledTable, halign::Symbol) -> StyledTable

Set the same horizontal alignment for all columns.

Arguments

  • tbl: the StyledTable to modify.

  • halign: one of :left, :center, or :right.

Returns

tbl (modified in place).

Examples

julia
tbl = StyledTable(df)
cols_align!(tbl, :center)
render(tbl)
source

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...)

julia
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
julia
cols_hide!(tbl::StyledTable, cols::Symbol...) -> StyledTable

Remove 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: the StyledTable to modify.

  • cols: one or more column names to hide.

Returns

tbl (modified in place).

See also: tab_rowgroup!.

Examples

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