Skip to main content

build_factor_with_levels

Function build_factor_with_levels 

Source
pub fn build_factor_with_levels(indices: &[i32], level_names: &[&str]) -> SEXP
Expand description

Build a factor SEXP from indices and level names in a single call.

Builds the levels STRSXP via build_levels_sexp and protects it across the build_factor allocation, so callers don’t need to manage the levels protection themselves. The returned factor SEXP is not protected — caller must protect or return it.

This is the recommended path for one-shot factor construction; for repeated calls with the same levels prefer caching via build_levels_sexp_cached (no protection needed because the cached SEXP is on R’s precious list).

See CLAUDE.md “PROTECT discipline against R-devel GC” for why this matters even though build_levels_sexp uses symbol PRINTNAMEs for the per-element CHARSXPs — the container STRSXP itself is freshly allocated and unprotected.