Skip to main content

IntoDataFrame

Trait IntoDataFrame 

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

    // Provided method
#[doc(hidden)]
fn into_named_columns(self) -> Vec<(String, SEXP)> where Self: Sized { ... } }
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())
    }
}

Provided Methods§

Source

#[doc(hidden)]
fn into_named_columns(self) -> Vec<(String, SEXP)>
where Self: Sized,

Extract named column SEXPs from this DataFrame.

Returns a Vec<(String, SEXP)> where each entry is a column name and the raw SEXP for that column. The SEXPs are owned by the data frame SEXP and must be protected by the caller before the data frame SEXP is released.

Used by DataFrameRow-derived enum code to flatten struct-typed fields. The default implementation calls into_data_frame() and extracts the names and elements from the resulting VECSXP. Override only if you need a more efficient extraction path.

§Safety

This method calls R API functions and must run on the R main thread.

Implementors§