Struct extendr_api::wrapper::matrix::RArray[][src]

pub struct RArray<T, D> { /* fields omitted */ }
Expand description

Wrapper for creating and using matrices and arrays.

use extendr_api::prelude::*;
test! {
    let matrix = RMatrix::new_matrix(3, 2, |r, c| [
        [1., 2., 3.],
         [4., 5., 6.]][c][r]);
    let robj = r!(matrix);
    assert_eq!(robj.is_matrix(), true);
    assert_eq!(robj.nrows(), 3);
    assert_eq!(robj.ncols(), 2);

    let matrix2 : RMatrix<f64> = robj.as_matrix().ok_or("error")?;
    assert_eq!(matrix2.data().len(), 6);
    assert_eq!(matrix2.nrows(), 3);
    assert_eq!(matrix2.ncols(), 2);
}

Implementations

Get the underlying data fro this array.

Get the dimensions for this array.

Make a new column type.

Get the number of rows.

Create a new matrix wrapper. Make a new column type.

Get the number of rows.

Get the number of columns.

Get the number of rows.

Get the number of columns.

Get the number of submatrices.

Methods from Deref<Target = Robj>

Is this object is an NA scalar? Works for character, integer and numeric types.

use extendr_api::prelude::*;
test! {

assert_eq!(r!(NA_INTEGER).is_na(), true);
assert_eq!(r!(NA_REAL).is_na(), true);
assert_eq!(r!(NA_STRING).is_na(), true);
}

Get a read-only reference to the content of an integer vector.

use extendr_api::prelude::*;
test! {

let robj = r!([1, 2, 3]);
assert_eq!(robj.as_integer_slice().unwrap(), [1, 2, 3]);
}

Convert an Robj into Integers.

Get a Vec copied from the object.

use extendr_api::prelude::*;
test! {

let robj = r!([1, 2, 3]);
assert_eq!(robj.as_integer_slice().unwrap(), vec![1, 2, 3]);
}

Get a read-only reference to the content of a logical vector using the tri-state Bool. Returns None if not a logical vector.

use extendr_api::prelude::*;
test! {
    let robj = r!([TRUE, FALSE, NA_LOGICAL]);
    assert_eq!(robj.as_logical_slice().unwrap(), [TRUE, FALSE, NA_LOGICAL]);
}

Get a Vec copied from the object using the tri-state Bool. Returns None if not a logical vector.

use extendr_api::prelude::*;
test! {
    let robj = r!([TRUE, FALSE, NA_LOGICAL]);
    assert_eq!(robj.as_logical_vector().unwrap(), vec![TRUE, FALSE, NA_LOGICAL]);
}

Get an iterator over logical elements of this slice.

use extendr_api::prelude::*;
test! {
    let robj = r!([TRUE, FALSE, NA_LOGICAL]);
    let (mut nt, mut nf, mut nna) = (0, 0, 0);
    for val in robj.as_logical_iter().unwrap() {
      match *val {
        TRUE => nt += 1,
        FALSE => nf += 1,
        NA_LOGICAL => nna += 1,
        _ => ()
      }
    }
    assert_eq!((nt, nf, nna), (1, 1, 1));
}

Get a read-only reference to the content of a double vector. Note: the slice may contain NaN or NA values. We may introduce a “Real” type to handle this like the Bool type.

use extendr_api::prelude::*;
test! {
    let robj = r!([Some(1.), None, Some(3.)]);
    let mut tot = 0.;
    for val in robj.as_real_slice().unwrap() {
      if !val.is_na() {
        tot += val;
      }
    }
    assert_eq!(tot, 4.);
}

Get an iterator over real elements of this slice.

use extendr_api::prelude::*;
test! {
    let robj = r!([1., 2., 3.]);
    let mut tot = 0.;
    for val in robj.as_real_iter().unwrap() {
      if !val.is_na() {
        tot += val;
      }
    }
    assert_eq!(tot, 6.);
}

Get a Vec copied from the object.

use extendr_api::prelude::*;
test! {
    let robj = r!([1., 2., 3.]);
    assert_eq!(robj.as_real_vector().unwrap(), vec![1., 2., 3.]);
}

Get a read-only reference to the content of an integer or logical vector.

use extendr_api::prelude::*;
test! {
    let robj = r!(Raw::from_bytes(&[1, 2, 3]));
    assert_eq!(robj.as_raw_slice().unwrap(), &[1, 2, 3]);
}

Get a vector of owned strings. Owned strings have long lifetimes, but are much slower than references.

use extendr_api::prelude::*;
test! {
   let robj1 = Robj::from("xyz");
   assert_eq!(robj1.as_string_vector(), Some(vec!["xyz".to_string()]));
   let robj2 = Robj::from(1);
   assert_eq!(robj2.as_string_vector(), None);
}

Get a vector of string references. String references (&str) are faster, but have short lifetimes.

use extendr_api::prelude::*;
test! {
   let robj1 = Robj::from("xyz");
   assert_eq!(robj1.as_str_vector(), Some(vec!["xyz"]));
   let robj2 = Robj::from(1);
   assert_eq!(robj2.as_str_vector(), None);
}

Get a read-only reference to a scalar string type.

use extendr_api::prelude::*;
test! {
   let robj1 = Robj::from("xyz");
   let robj2 = Robj::from(1);
   assert_eq!(robj1.as_str(), Some("xyz"));
   assert_eq!(robj2.as_str(), None);
}

Get a scalar integer.

use extendr_api::prelude::*;
test! {
   let robj1 = Robj::from("xyz");
   let robj2 = Robj::from(1);
   let robj3 = Robj::from(NA_INTEGER);
   assert_eq!(robj1.as_integer(), None);
   assert_eq!(robj2.as_integer(), Some(1));
   assert_eq!(robj3.as_integer(), None);
}

Get a scalar real.

use extendr_api::prelude::*;
test! {
   let robj1 = Robj::from(1);
   let robj2 = Robj::from(1.);
   let robj3 = Robj::from(NA_REAL);
   assert_eq!(robj1.as_real(), None);
   assert_eq!(robj2.as_real(), Some(1.));
   assert_eq!(robj3.as_real(), None);
}

Get a scalar rust boolean.

use extendr_api::prelude::*;
test! {
   let robj1 = Robj::from(TRUE);
   let robj2 = Robj::from(1.);
   let robj3 = Robj::from(NA_LOGICAL);
   assert_eq!(robj1.as_bool(), Some(true));
   assert_eq!(robj2.as_bool(), None);
   assert_eq!(robj3.as_bool(), None);
}

Get a scalar boolean as a tri-boolean Bool value.

use extendr_api::prelude::*;
test! {
   let robj1 = Robj::from(TRUE);
   let robj2 = Robj::from([TRUE, FALSE]);
   let robj3 = Robj::from(NA_LOGICAL);
   assert_eq!(robj1.as_logical(), Some(TRUE));
   assert_eq!(robj2.as_logical(), None);
   assert_eq!(robj3.as_logical(), Some(NA_LOGICAL));
}

Trait Implementations

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Convert a column, matrix or matrix3d to an Robj.

Zero-based indexing in row, column order.

Panics if out of bounds.

use extendr_api::prelude::*;
test! {
   let matrix = RArray::new_matrix(3, 2, |r, c| [
       [1., 2., 3.],
       [4., 5., 6.]][c][r]);
    assert_eq!(matrix[[0, 0]], 1.);
    assert_eq!(matrix[[1, 0]], 2.);
    assert_eq!(matrix[[2, 1]], 6.);
}

The returned type after indexing.

Zero-based mutable indexing in row, column order.

Panics if out of bounds.

use extendr_api::prelude::*;
test! {
    let mut matrix = RMatrix::new_matrix(3, 2, |_, _| 0.);
    matrix[[0, 0]] = 1.;
    matrix[[1, 0]] = 2.;
    matrix[[2, 0]] = 3.;
    matrix[[0, 1]] = 4.;
    assert_eq!(matrix.as_real_slice().unwrap(), &[1., 2., 3., 4., 0., 0.]);
}

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.