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.
- Factor
Mut - A mutable borrowed view into an R factor’s integer indices.
- Factor
Option Vec - Wrapper for
Vec<Option<T: RFactor>>with NA support. - Factor
Vec - Wrapper for
Vec<T: RFactor>enablingIntoR/TryFromSexp.
Statics§
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.