Skip to main content

Module factor

Module factor 

Source
Expand description

Factor support for enum ↔ R factor conversions.

Provides the RFactor trait for converting Rust enums to/from R factors. Use #[derive(RFactor)] on C-style enums to auto-generate the implementation.

§Example

use miniextendr_api::RFactor;

#[derive(Copy, Clone, RFactor)]
enum Color { Red, Green, Blue }

#[miniextendr]
fn color_name(c: Color) -> &'static str {
    match c {
        Color::Red => "red",
        Color::Green => "green",
        Color::Blue => "blue",
    }
}

Factor support for enum ↔ R factor conversions.

R factors are integer vectors with a levels attribute (character vector) and a class attribute set to "factor". The integer payload uses 1-based indexing into the levels, with NA_INTEGER for missing values.

§Usage

use miniextendr_api::RFactor;

#[derive(Copy, Clone, RFactor)]
enum Color { Red, Green, Blue }

// Enum values convert to/from R factors automatically
#[miniextendr]
fn describe(c: Color) -> &'static str {
    match c {
        Color::Red => "red",
        Color::Green => "green",
        Color::Blue => "blue",
    }
}

Structs§

Factor
A borrowed view into an R factor’s integer indices.
FactorMut
A mutable borrowed view into an R factor’s integer indices.
FactorOptionVec
Wrapper for Vec<Option<T: RFactor>> with NA support.
FactorVec
Wrapper for Vec<T: RFactor> enabling IntoR/TryFromSexp.

Statics§

FACTOR_CLASS 🔒

Traits§

RFactor
Trait for mapping Rust enums to R factors.

Functions§

build_factor
Build a factor SEXP from indices and a levels STRSXP.
build_levels_sexp
Build a levels STRSXP using symbol PRINTNAMEs for permanent CHARSXP protection.
build_levels_sexp_cached
Build a levels STRSXP and preserve it permanently (for caching).
factor_class_sexp 🔒
factor_from_sexp
Convert an R factor SEXP to a single enum value.
factor_option_vec_from_sexp 🔒
Convert an R factor SEXP to a Vec of Option enum values (NA → None).
factor_vec_from_sexp 🔒
Convert an R factor SEXP to a Vec of enum values.
validate_factor_levels 🔒
Validate that a factor has the expected levels.