Skip to main content

IntoDataFrame

Trait IntoDataFrame 

Source
pub trait IntoDataFrame {
    // Required method
    fn into_data_frame(self) -> List;
}
Expand description

Trait for types that can be converted into R data frames.

This trait allows Rust types to define how they convert to R data frames. Use with ToDataFrame wrapper or #[derive(PreferDataFrame)] to enable automatic conversion.

§Example

use miniextendr_api::convert::IntoDataFrame;
use miniextendr_api::List;

struct TimeSeries {
    timestamps: Vec<f64>,
    values: Vec<f64>,
}

impl IntoDataFrame for TimeSeries {
    fn into_data_frame(self) -> List {
        List::from_pairs(vec![
            ("timestamp", self.timestamps),
            ("value", self.values),
        ])
        .set_class_str(&["data.frame"])
        .set_row_names_int(self.timestamps.len())
    }
}

§Comparison with AsDataFrame coercion trait

  • AsDataFrame: Used with #[miniextendr(as = "data.frame")] to generate S3 methods for as.data.frame() on external pointer types
  • IntoDataFrame: Used for direct conversion when returning from functions

Both return a List with appropriate data.frame attributes, but serve different purposes:

  • S3 AsDataFrame is for coercion methods on existing objects (&self)
  • IntoDataFrame is for consuming conversion (self) when returning from functions

Required Methods§

Source

fn into_data_frame(self) -> List

Convert this value into an R data.frame.

The returned List should have:

  • Named columns of equal length
  • Class attribute set to “data.frame”
  • row.names attribute set appropriately
§Example
impl IntoDataFrame for MyStruct {
    fn into_data_frame(self) -> List {
        List::from_pairs(vec![
            ("col1", self.field1),
            ("col2", self.field2),
        ])
        .set_class_str(&["data.frame"])
        .set_row_names_int(self.field1.len())
    }
}

Implementors§