Struct extendr_api::wrapper::matrix::RArray
source · pub struct RArray<T, D> {
robj: Robj,
dim: D,
_data: PhantomData<T>,
}
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);
}
Fields§
§robj: Robj
Owning Robj (probably should be a Pin).
dim: D
Dimensions of the array.
_data: PhantomData<T>
Implementations§
source§impl<T, D> RArray<T, D>
impl<T, D> RArray<T, D>
pub fn get_dimnames(&self) -> List
sourcepub fn set_names(&mut self, names: Strings)
pub fn set_names(&mut self, names: Strings)
Set the names of the elements of an array.
Equivalent to names<-
in R
sourcepub fn set_dimnames(&mut self, dimnames: List)
pub fn set_dimnames(&mut self, dimnames: List)
Set the dimension names of an array.
For RMatrix
a list of length 2 is required, as that would entail
column-names and row-names. If you only wish to set one, but not the other,
then the unset element must be R NULL
Equivalent to dimnames<-
in R
source§impl<T> RArray<T, [usize; 2]>
impl<T> RArray<T, [usize; 2]>
pub fn get_colnames(&self) -> Option<Strings>
pub fn get_rownames(&self) -> Option<Strings>
source§impl<T, D> RArray<T, D>where
Robj: for<'a> AsTypedSlice<'a, T>,
impl<T, D> RArray<T, D>where
Robj: for<'a> AsTypedSlice<'a, T>,
source§impl<T> RArray<T, [usize; 2]>
impl<T> RArray<T, [usize; 2]>
sourcepub fn new_matrix<F: Clone + FnMut(usize, usize) -> T>(
nrows: usize,
ncols: usize,
f: F,
) -> Self
pub fn new_matrix<F: Clone + FnMut(usize, usize) -> T>( nrows: usize, ncols: usize, f: F, ) -> Self
Create a new matrix wrapper.
§Arguments
nrows
- the number of rows the returned matrix will havencols
- the number of columns the returned matrix will havef
- a function that will be called for each entry of the matrix in order to populate it with values. It must return a scalar value that can be converted to an R scalar, such asi32
,u32
, orf64
, i.e. see ToVectorValue. It accepts two arguments:r
- the current row of the entry we are creatingc
- the current column of the entry we are creating
Methods from Deref<Target = Robj>§
sourcepub fn is_na(&self) -> bool
pub fn is_na(&self) -> bool
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);
}
sourcepub fn as_integer_slice<'a>(&self) -> Option<&'a [i32]>
pub fn as_integer_slice<'a>(&self) -> Option<&'a [i32]>
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]);
}
sourcepub fn as_integer_vector(&self) -> Option<Vec<i32>>
pub fn as_integer_vector(&self) -> Option<Vec<i32>>
Get a Vec<i32>
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]);
}
sourcepub fn as_logical_slice(&self) -> Option<&[Rbool]>
pub fn as_logical_slice(&self) -> Option<&[Rbool]>
Get a read-only reference to the content of a logical vector using the tri-state Rbool. Returns None if not a logical vector.
use extendr_api::prelude::*;
test! {
let robj = r!([TRUE, FALSE]);
assert_eq!(robj.as_logical_slice().unwrap(), [TRUE, FALSE]);
}
sourcepub fn as_logical_vector(&self) -> Option<Vec<Rbool>>
pub fn as_logical_vector(&self) -> Option<Vec<Rbool>>
Get a Vec<Rbool>
copied from the object
using the tri-state Rbool
.
Returns None
if not a logical vector.
use extendr_api::prelude::*;
test! {
let robj = r!([TRUE, FALSE]);
assert_eq!(robj.as_logical_vector().unwrap(), vec![TRUE, FALSE]);
}
sourcepub fn as_logical_iter(&self) -> Option<impl Iterator<Item = &Rbool>>
pub fn as_logical_iter(&self) -> Option<impl Iterator<Item = &Rbool>>
Get an iterator over logical elements of this slice.
use extendr_api::prelude::*;
test! {
let robj = r!([TRUE, FALSE, NA_LOGICAL]);
let mut num_na = 0;
for val in robj.as_logical_iter().unwrap() {
if val.is_na() {
num_na += 1;
}
}
assert_eq!(num_na, 1);
}
sourcepub fn as_real_slice(&self) -> Option<&[f64]>
pub fn as_real_slice(&self) -> Option<&[f64]>
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 Rbool 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.);
}
sourcepub fn as_real_iter(&self) -> Option<impl Iterator<Item = &f64>>
pub fn as_real_iter(&self) -> Option<impl Iterator<Item = &f64>>
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.);
}
sourcepub fn as_real_vector(&self) -> Option<Vec<f64>>
pub fn as_real_vector(&self) -> Option<Vec<f64>>
Get a Vec<f64>
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.]);
}
sourcepub fn as_raw_slice(&self) -> Option<&[u8]>
pub fn as_raw_slice(&self) -> Option<&[u8]>
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]);
}
sourcepub fn as_integer_slice_mut(&mut self) -> Option<&mut [i32]>
pub fn as_integer_slice_mut(&mut self) -> Option<&mut [i32]>
Get a read-write reference to the content of an integer or logical vector.
Note that rust slices are 0-based so slice[1]
is the middle value.
use extendr_api::prelude::*;
test! {
let mut robj = r!([1, 2, 3]);
let slice : & mut [i32] = robj.as_integer_slice_mut().unwrap();
slice[1] = 100;
assert_eq!(robj, r!([1, 100, 3]));
}
sourcepub fn as_real_slice_mut(&mut self) -> Option<&mut [f64]>
pub fn as_real_slice_mut(&mut self) -> Option<&mut [f64]>
Get a read-write reference to the content of a double vector.
Note that rust slices are 0-based so slice[1]
is the middle value.
use extendr_api::prelude::*;
test! {
let mut robj = r!([1.0, 2.0, 3.0]);
let slice = robj.as_real_slice_mut().unwrap();
slice[1] = 100.0;
assert_eq!(robj, r!([1.0, 100.0, 3.0]));
}
sourcepub fn as_raw_slice_mut(&mut self) -> Option<&mut [u8]>
pub fn as_raw_slice_mut(&mut self) -> Option<&mut [u8]>
Get a read-write reference to the content of a raw vector.
use extendr_api::prelude::*;
test! {
let mut robj = r!(Raw::from_bytes(&[1, 2, 3]));
let slice = robj.as_raw_slice_mut().unwrap();
slice[1] = 100;
assert_eq!(robj, r!(Raw::from_bytes(&[1, 100, 3])));
}
sourcepub fn as_string_vector(&self) -> Option<Vec<String>>
pub fn as_string_vector(&self) -> Option<Vec<String>>
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);
}
sourcepub fn as_str_vector(&self) -> Option<Vec<&str>>
pub fn as_str_vector(&self) -> Option<Vec<&str>>
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);
}
sourcepub fn as_str<'a>(&self) -> Option<&'a str>
pub fn as_str<'a>(&self) -> Option<&'a str>
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);
}
sourcepub fn as_integer(&self) -> Option<i32>
pub fn as_integer(&self) -> Option<i32>
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);
}
sourcepub fn as_real(&self) -> Option<f64>
pub fn as_real(&self) -> Option<f64>
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);
}
sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
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);
}
sourcepub fn as_logical(&self) -> Option<Rbool>
pub fn as_logical(&self) -> Option<Rbool>
Get a scalar boolean as a tri-boolean Rbool 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().unwrap().is_na(), true);
}
Trait Implementations§
source§impl<T> Index<[usize; 2]> for RArray<T, [usize; 2]>where
Robj: for<'a> AsTypedSlice<'a, T>,
impl<T> Index<[usize; 2]> for RArray<T, [usize; 2]>where
Robj: for<'a> AsTypedSlice<'a, T>,
source§fn index(&self, index: [usize; 2]) -> &Self::Output
fn index(&self, index: [usize; 2]) -> &Self::Output
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.);
}
source§impl<T> IndexMut<[usize; 2]> for RArray<T, [usize; 2]>where
Robj: for<'a> AsTypedSlice<'a, T>,
impl<T> IndexMut<[usize; 2]> for RArray<T, [usize; 2]>where
Robj: for<'a> AsTypedSlice<'a, T>,
source§fn index_mut(&mut self, index: [usize; 2]) -> &mut Self::Output
fn index_mut(&mut self, index: [usize; 2]) -> &mut Self::Output
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.]);
}
impl<T, D> StructuralPartialEq for RArray<T, D>
Auto Trait Implementations§
impl<T, D> Freeze for RArray<T, D>where
D: Freeze,
impl<T, D> RefUnwindSafe for RArray<T, D>where
D: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, D> !Send for RArray<T, D>
impl<T, D> !Sync for RArray<T, D>
impl<T, D> Unpin for RArray<T, D>
impl<T, D> UnwindSafe for RArray<T, D>where
D: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more