Skip to main content

Module expr

Module expr 

Source
Expand description

A tiny arithmetic expression evaluator for computed aesthetics — so aes can map e.g. "pop / 1e6", "log(gdp)", or "a * b + 1" instead of a bare column name. Anything that isn’t an existing column is parsed and evaluated per row against the data’s numeric columns.

Grammar (standard precedence, ^ right-associative): expr := term (('+'|'-') term)*, term := factor (('*'|'/'|'%') factor)*, factor := unary ('^' factor)?, unary := ('-'|'+') unary | primary, primary := number | ident | ident '(' expr ')' | '(' expr ')'. Functions: ln/log, log10, log2, sqrt, exp, abs, sin, cos, tan, floor, ceil, round, sign. Aggregate functions reduce their argument over all rows to a scalar (broadcast to every row): sum, mean (avg), max, min, count, median, prod — enabling normalized after_stat mappings such as "count / sum(count)".

Functions§

eval_expression
Evaluate expr over every row of data, producing one Value per row (non-finite results become Value::Na). Returns None if the string is not a valid expression or references no existing column (so a plain unknown column name / typo is left for the caller to handle, not silently computed).