Skip to main content

AsDataFrame

Trait AsDataFrame 

Source
pub trait AsDataFrame {
    // Required method
    fn as_data_frame(&self) -> Result<List, AsCoerceError>;
}
Expand description

Trait for types that can be coerced to data.frame via as.data.frame().

§Example

use miniextendr_api::as_coerce::{AsDataFrame, AsCoerceError};
use miniextendr_api::List;

impl AsDataFrame for MyStruct {
    fn as_data_frame(&self) -> Result<List, AsCoerceError> {
        Ok(List::from_pairs(vec![
            ("col1", self.field1.clone()),
            ("col2", self.field2.clone()),
        ])
        .set_class_str(&["data.frame"])
        .set_row_names_int(self.field1.len()))
    }
}

Required Methods§

Source

fn as_data_frame(&self) -> Result<List, AsCoerceError>

Convert to 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

Implementors§