Formatting
Formatters convert raw cell values into display strings before styling is applied. They leave the underlying DataFrame unchanged — they only affect the rendered output.
format!
The entry point for all formatting. Pass a formatter object and the columns to apply it to.
StyledTables.format! Function
format!(formatter, tbl, cols...)
format!(formatter, tbl, cols::AbstractVector)Append formatter to the format stack for each column in cols.
formatter may be any AbstractFormatter or a bare callable (automatically wrapped in FunctionFormatter).
Formatters are applied in call order at render time: the first format! call runs first on the raw value. Stack MissingFormatter last to intercept any missing values that remain after earlier formatters.
Examples
tbl = StyledTable(df)
format!(NumberFormatter(digits = 3), tbl, :x, :y)
format!(MissingFormatter("—"), tbl, :x, :y)
render(tbl)Built-in formatters
AbstractFormatter
StyledTables.AbstractFormatter Type
AbstractFormatterSupertype for all formatters used with format!.
Implement (f::MyFormatter)(x) to define a custom formatter:
struct PrefixFormatter <: AbstractFormatter
prefix::String
end
(f::PrefixFormatter)(x) = ismissing(x) ? x : f.prefix * string(x)NumberFormatter
using StyledTables, DataFrames
df = DataFrame(x = [1.2345, 6.789], y = [100.0, 0.001])
tbl = StyledTable(df)
format!(NumberFormatter(digits = 3), tbl, :x)
format!(NumberFormatter(digits = 2), tbl, :y)
render(tbl)| x | y |
| 1.234 | 100.00 |
| 6.789 | 0.00 |
StyledTables.NumberFormatter Type
NumberFormatter(; digits = 2, trailing_zeros = true)Format numeric values to a fixed number of decimal places.
digits: number of decimal places.trailing_zeros: whenfalse, strip trailing zeros after the decimal point.
PercentFormatter
df = DataFrame(rate = [0.123, 0.456, 0.789])
tbl = StyledTable(df)
format!(PercentFormatter(digits = 1), tbl, :rate)
render(tbl)| rate |
| 12.3% |
| 45.6% |
| 78.9% |
For already-scaled values (e.g., 12.3 stored as 12.3%):
df2 = DataFrame(rate = [12.3, 45.6, 78.9])
tbl = StyledTable(df2)
format!(PercentFormatter(digits = 1, scale = 1), tbl, :rate)
render(tbl)| rate |
| 12.3% |
| 45.6% |
| 78.9% |
StyledTables.PercentFormatter Type
PercentFormatter(; digits = 1, scale = 100, suffix = "%")Multiply a value by scale, format to digits decimal places, and append suffix.
IntegerFormatter
df = DataFrame(count = [12.6, 7.2, 100.9])
tbl = StyledTable(df)
format!(IntegerFormatter(), tbl, :count)
render(tbl)| count |
| 13 |
| 7 |
| 101 |
StyledTables.IntegerFormatter Type
IntegerFormatter()Round numeric values to the nearest integer and format without a decimal point.
sourceMissingFormatter
Stack MissingFormatter last so that earlier numeric formatters run first on non-missing values.
StyledTables.MissingFormatter Type
MissingFormatter(replacement)Return replacement when a value ismissing; otherwise pass it through unchanged. Stack this last so earlier numeric formatters run first.
FunctionFormatter
Pass a bare callable to format! — it is wrapped in a FunctionFormatter automatically. You rarely need to construct FunctionFormatter directly.
df = DataFrame(p_value = [0.032, 0.001, 0.245])
tbl = StyledTable(df)
format!(tbl, :p_value) do pval
pval < 0.05 ? "< 0.05" : "n.s."
end
render(tbl)| p_value |
| < 0.05 |
| < 0.05 |
| n.s. |
StyledTables.FunctionFormatter Type
FunctionFormatter(f)Wraps a bare callable f as an AbstractFormatter. Created automatically when a Function is passed to format!.
Stacking formatters
Each format! call appends to the formatter stack for a column. Formatters run in call order at render time. Each formatter in the stack receives the output of the previous one, not the original raw value. Use this to combine a numeric formatter with a fallback for missing values:
df = DataFrame(x = [1.5, missing, 3.0])
tbl = StyledTable(df)
format!(NumberFormatter(digits = 1), tbl, :x) # runs first on non-missing
format!(MissingFormatter("—"), tbl, :x) # intercepts any remaining missing
render(tbl)| x |
| 1.5 |
| — |
| 3.0 |
Custom formatters
Implement AbstractFormatter to define reusable custom formatters:
struct PrefixFormatter <: AbstractFormatter
prefix::String
end
(f::PrefixFormatter)(x) = ismissing(x) ? x : f.prefix * string(x)Then use it like any built-in formatter:
format!(PrefixFormatter("€"), tbl, :price)