Skip to main content

RCoerceDataFrame

Trait RCoerceDataFrame 

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

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

§Example

use miniextendr_api::r_coerce::{RCoerceDataFrame, RCoerceError};
use miniextendr_api::List;

impl RCoerceDataFrame for MyStruct {
    fn as_data_frame(&self) -> Result<List, RCoerceError> {
        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, RCoerceError>

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

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§