Skip to main content

DataFrame

Struct DataFrame 

Source
pub struct DataFrame {
    sexp: SEXP,
}
Expand description

An owned, validated R data.frame. The data-frame type.

Wraps a built VECSXP carrying the data.frame class + row.names. A single coherent type for building (Rust → R), reading (R → Rust), and post-assembly editing — replacing the historical row-buffer / built-SEXP / read-wrapper trio with one coherent type.

§Building

Prefer the IntoDataFrame trait on your data:

let df: DataFrame = rows.into_dataframe()?;

or the closure-fill DataFrame::builder for heterogeneous parallel column fill (feature = "rayon").

§Reading

Wrap an incoming SEXP with DataFrame::from_sexp (or accept DataFrame directly as a #[miniextendr] argument), then pull typed columns with DataFrame::column, or deserialize whole rows with FromDataFrame.

Fields§

§sexp: SEXP

Implementations§

Source§

impl DataFrame

Source

pub fn group_by(&self, col: &str) -> Result<GroupedDataFrame, DataFrameError>

Partition this frame’s rows by the values of the named column.

Computes group indices in a single pass on the main thread. Supported key columns: factor (fast path — levels are the keys, level order kept, empty levels included), character, integer, and logical. Double columns error — cut() or factor() the column in R first.

NA keys form one group, ordered last (unlike R split(), which drops NA-keyed rows). See the module docs for the full key semantics.

Source§

impl DataFrame

Source

pub unsafe fn from_built_sexp(sexp: SEXP) -> Self

Wrap an already-built data.frame SEXP without re-validation.

Used by the column assemblers, which produce a well-formed data.frame by construction.

§Safety

sexp must be a VECSXP with the data.frame class and consistent row.names.

Source

pub fn from_sexp(sexp: SEXP) -> Result<Self, DataFrameError>

Wrap an existing R data.frame SEXP, validating it.

Validates that the object:

  1. Is a VECSXP (list)
  2. Inherits from "data.frame"
  3. Has a names attribute
  4. Has extractable row.names for nrow
§Errors

Returns DataFrameError if validation fails.

Source

pub fn column<T>(&self, name: &str) -> Option<T>
where T: TryFromSexp<Error = SexpError>,

Get a column by name, converting each element to type T.

Returns None if the column name is not found or conversion fails.

Source

pub fn column_index<T>(&self, idx: usize) -> Option<T>
where T: TryFromSexp<Error = SexpError>,

Get a column by 0-based index, converting to type T.

Source

pub fn column_raw(&self, name: &str) -> Option<SEXP>

Get the raw SEXP for a column by name.

Source

pub fn nrow(&self) -> usize

Number of rows.

Source

pub fn ncol(&self) -> usize

Number of columns.

Source

pub fn names(&self) -> Vec<String>

Collect column names in column order.

Source

pub fn contains_column(&self, name: &str) -> bool

Check whether a column name exists.

Source

pub fn validate( &self, spec: &TypedListSpec, ) -> Result<TypedList, TypedListError>

Validate the data frame’s column types against a TypedListSpec.

Source

pub fn as_list(&self) -> List

Get the underlying List.

Source

pub fn as_sexp(&self) -> SEXP

Get the underlying SEXP.

Source

fn named_list(&self) -> NamedList

Build the NamedList index for O(1) column-by-name access.

Source

pub fn rename(self, from: &str, to: &str) -> Self

Rename a column. No-op if from doesn’t match any column name.

Source

pub fn strip_prefix(self, prefix: &str) -> Self

Strip a prefix from all column names that start with it.

Source

pub fn drop(self, col: &str) -> Self

Remove a column by name. No-op if the column doesn’t exist.

Source

pub fn select(self, cols: &[&str]) -> Self

Keep only the named columns, in the order given. Unknown names are skipped.

Source

pub fn select_rows(&self, idx: &[usize]) -> Self

Keep only the rows at the given 0-based indices, in order.

Subsets every column (each a vector or list-column) to the specified rows and rebuilds compact integer row.names. Used by the enum reader to densify a flattened sub-frame before recursing into the inner type’s reader.

§PROTECT discipline

Allocates one new column vector per column — OwnedProtects the output list across the loop so previously-built column SEXPs survive subsequent allocations.

Source

pub fn prepend_column(self, name: &str, column: SEXP) -> Self

Insert a column at index 0 (leftmost), removing any same-named column first.

Source

pub fn with_column(self, name: &str, column: SEXP) -> Self

Upsert a column: replace the column named name if it exists, else append.

Source

pub fn builder(nrow: usize) -> RDataFrameBuilder

Start a closure-per-column builder yielding a DataFrame.

The heterogeneous-column analogue of with_r_matrix: each column buffer is R memory filled by a per-column closure. Available regardless of the rayon feature (#1055); the columns are filled in parallel when rayon is enabled and serially otherwise — the resulting data.frame is identical either way.

let df = DataFrame::builder(1000)
    .column::<f64>("x", |chunk, off| for (i, v) in chunk.iter_mut().enumerate() { *v = (off + i) as f64 })
    .column_str("label", |i| Some(format!("row{i}")))
    .build();

Trait Implementations§

Source§

impl Clone for DataFrame

Source§

fn clone(&self) -> DataFrame

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for DataFrame

Source§

impl Debug for DataFrame

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl IntoR for DataFrame

Source§

type Error = Infallible

The error type for fallible conversions. Read more
Source§

fn try_into_sexp(self) -> Result<SEXP, Self::Error>

Try to convert this value to an R SEXP. Read more
Source§

unsafe fn try_into_sexp_unchecked(self) -> Result<SEXP, Self::Error>

Try to convert to SEXP without thread safety checks. Read more
Source§

fn into_sexp(self) -> SEXP

Convert this value to an R SEXP, panicking on error. Read more
Source§

unsafe fn into_sexp_unchecked(self) -> SEXP
where Self: Sized,

Convert to SEXP without thread safety checks, panicking on error. Read more
Source§

impl TrivialClone for DataFrame

Source§

impl TryFromSexp for DataFrame

Source§

type Error = SexpError

The error type returned when conversion fails.
Source§

fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error>

Attempt to convert an R SEXP to this Rust type. Read more
Source§

unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error>

Convert from SEXP without thread safety checks. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Printable for T
where T: Copy + Debug,

Source§

impl<T> RClone for T
where T: Clone,

Source§

fn clone(&self) -> T

Create a deep copy of this value.
Source§

impl<T> RCopy for T
where T: Copy,

Source§

fn copy(&self) -> T

Create a bitwise copy of this value. Read more
Source§

fn is_copy(&self) -> bool

Check if this type implements Copy. Read more
Source§

impl<T> RDebug for T
where T: Debug,

Source§

fn debug_str(&self) -> String

Get a compact debug string representation.
Source§

fn debug_str_pretty(&self) -> String

Get a pretty-printed debug string with indentation.
Source§

impl<T> SizeHint for T
where T: ?Sized,

Source§

default fn lower_bound(&self) -> usize

🔬This is a nightly-only experimental API. (core_io_internals)
Returns a lower bound on the number of elements this container-like item contains. For example, an array [u8; 12] could return any value between 0 and 12 inclusively as a correct implementation. Read more
Source§

default fn upper_bound(&self) -> Option<usize>

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an upper bound on the number of elements this container-like item contains if it can be determined, otherwise None. Read more
Source§

final fn size_hint(&self) -> (usize, Option<usize>)

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an estimate for the number of elements this container like type contains. Read more
Source§

impl<T> SizedTypeProperties for T

Source§

#[doc(hidden)]
const SIZE: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGNMENT: Alignment = _

🔬This is a nightly-only experimental API. (ptr_alignment_type)
Source§

#[doc(hidden)]
const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
Source§

#[doc(hidden)]
const LAYOUT: Layout = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const MAX_SLICE_LEN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
The largest safe length for a [Self]. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 8 bytes