Expand description
Traits for R’s as.<class>() coercion functions.
This module provides traits for implementing R’s generic coercion methods
(as.data.frame(), as.list(), as.character(), etc.) for Rust types
wrapped in ExternalPtr.
See the as_coerce module documentation for usage examples.
Traits for R’s as.<class>() coercion functions.
This module provides traits that enable Rust types wrapped in ExternalPtr<T>
to define how they convert to R base types. When used with the #[miniextendr(as = "...")]
attribute, these generate proper S3 method wrappers for R’s coercion generics.
§Supported Conversions
| R Generic | Rust Trait | Method |
|---|---|---|
as.data.frame | AsDataFrame | as_data_frame(&self) |
as.list | AsList | as_list(&self) |
as.character | AsCharacter | as_character(&self) |
as.numeric / as.double | AsNumeric | as_numeric(&self) |
as.integer | AsInteger | as_integer(&self) |
as.logical | AsLogical | as_logical(&self) |
as.matrix | AsMatrix | as_matrix(&self) |
as.vector | AsVector | as_vector(&self) |
as.factor | AsFactor | as_factor(&self) |
as.Date | AsDate | as_date(&self) |
as.POSIXct | AsPOSIXct | as_posixct(&self) |
as.complex | AsComplex | as_complex(&self) |
as.raw | AsRaw | as_raw(&self) |
as.environment | AsEnvironment | as_environment(&self) |
as.function | AsFunction | as_function(&self) |
§Usage with #[miniextendr]
Use #[miniextendr(as = "...")] on impl methods to generate S3 method wrappers:
ⓘ
use miniextendr_api::{miniextendr, ExternalPtr, List};
use miniextendr_api::as_coerce::AsCoerceError;
pub struct MyData {
names: Vec<String>,
values: Vec<f64>,
}
#[miniextendr(s3)]
impl MyData {
pub fn new(names: Vec<String>, values: Vec<f64>) -> Self {
Self { names, values }
}
/// Convert to data.frame
#[miniextendr(as = "data.frame")]
pub fn as_data_frame(&self) -> Result<List, AsCoerceError> {
if self.names.len() != self.values.len() {
return Err(AsCoerceError::InvalidData {
message: "names and values must have same length".into(),
});
}
Ok(List::from_pairs(vec![
("name", self.names.clone()),
("value", self.values.clone()),
])
.set_class_str(&["data.frame"])
.set_row_names_int(self.names.len()))
}
/// Convert to character representation
#[miniextendr(as = "character")]
pub fn as_character(&self) -> Result<String, AsCoerceError> {
Ok(format!("MyData({} items)", self.values.len()))
}
}This generates R S3 methods:
# Generated automatically:
as.data.frame.MyData <- function(x, ...) {
.Call(C_MyData__as_data_frame, .call = match.call(), x)
}
as.character.MyData <- function(x, ...) {
.Call(C_MyData__as_character, .call = match.call(), x)
}Enums§
- AsCoerce
Error - Error type for
as.<class>()coercion failures.
Constants§
- SUPPORTED_
AS_ GENERICS - All supported R coercion generics.
Traits§
- AsCharacter
- Trait for types that can be coerced to
characterviaas.character(). - AsComplex
- Trait for types that can be coerced to
complexviaas.complex(). - AsData
Frame - Trait for types that can be coerced to
data.frameviaas.data.frame(). - AsDate
- Trait for types that can be coerced to
Dateviaas.Date(). - AsEnvironment
- Trait for types that can be coerced to
environmentviaas.environment(). - AsFactor
- Trait for types that can be coerced to
factorviaas.factor(). - AsFunction
- Trait for types that can be coerced to
functionviaas.function(). - AsInteger
- Trait for types that can be coerced to
integerviaas.integer(). - AsList
- Trait for types that can be coerced to
listviaas.list(). - AsLogical
- Trait for types that can be coerced to
logicalviaas.logical(). - AsMatrix
- Trait for types that can be coerced to
matrixviaas.matrix(). - AsNumeric
- Trait for types that can be coerced to
numeric/doubleviaas.numeric(). - AsPOSI
Xct - Trait for types that can be coerced to
POSIXctviaas.POSIXct(). - AsRaw
- Trait for types that can be coerced to
rawviaas.raw(). - AsVector
- Trait for types that can be coerced to a generic
vectorviaas.vector().
Functions§
- is_
supported_ as_ generic - Check if a generic name is a supported
as.<class>()generic. - r_
generic_ to_ method - Maps an R generic name to the corresponding trait method name.