pub trait Attributes: Types + Length {
    // Provided methods
    fn get_attrib<'a, N>(&self, name: N) -> Option<Robj>
       where Self: 'a,
             Robj: From<N> + 'a { ... }
    fn has_attrib<'a, N>(&self, name: N) -> bool
       where Self: 'a,
             Robj: From<N> + 'a { ... }
    fn set_attrib<N, V>(&mut self, name: N, value: V) -> Result<Robj>
       where N: Into<Robj>,
             V: Into<Robj> { ... }
    fn names(&self) -> Option<StrIter> { ... }
    fn has_names(&self) -> bool { ... }
    fn set_names<T>(&mut self, names: T) -> Result<Robj>
       where T: IntoIterator,
             T::IntoIter: ExactSizeIterator,
             T::Item: ToVectorValue + AsRef<str> { ... }
    fn dim(&self) -> Option<Integers> { ... }
    fn dimnames(&self) -> Option<ListIter> { ... }
    fn class(&self) -> Option<StrIter> { ... }
    fn set_class<T>(&mut self, class: T) -> Result<Robj>
       where T: IntoIterator,
             T::IntoIter: ExactSizeIterator,
             T::Item: ToVectorValue + AsRef<str> { ... }
    fn inherits(&self, classname: &str) -> bool { ... }
    fn levels(&self) -> Option<StrIter> { ... }
}
Expand description

These are helper functions which give access to common properties of R objects.

Provided Methods§

source

fn get_attrib<'a, N>(&self, name: N) -> Option<Robj>
where Self: 'a, Robj: From<N> + 'a,

Get a specific attribute as a borrowed Robj if it exists.

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

   let mut robj = r!("hello");
   robj.set_attrib(sym!(xyz), 1);
   assert_eq!(robj.get_attrib(sym!(xyz)), Some(r!(1)));
}
source

fn has_attrib<'a, N>(&self, name: N) -> bool
where Self: 'a, Robj: From<N> + 'a,

Return true if an attribute exists.

source

fn set_attrib<N, V>(&mut self, name: N, value: V) -> Result<Robj>
where N: Into<Robj>, V: Into<Robj>,

Set a specific attribute in-place and return the object.

Note that some combinations of attributes are illegal and this will return an error.

use extendr_api::prelude::*;
test! {
   let mut robj = r!("hello").set_attrib(sym!(xyz), 1)?;
   assert_eq!(robj.get_attrib(sym!(xyz)), Some(r!(1)));
}
source

fn names(&self) -> Option<StrIter>

Get the names attribute as a string iterator if one exists.

use extendr_api::prelude::*;
test! {
   let list = list!(a = 1, b = 2, c = 3);
   let names : Vec<_> = list.names().unwrap().collect();
   assert_eq!(names, vec!["a", "b", "c"]);
}
source

fn has_names(&self) -> bool

Return true if this object has names.

source

fn set_names<T>(&mut self, names: T) -> Result<Robj>

Set the names attribute from a string iterator.

Returns Error::NamesLengthMismatch if the length of the names does not match the length of the object.

use extendr_api::prelude::*;
test! {
    let mut obj = r!([1, 2, 3]).set_names(&["a", "b", "c"]).unwrap();
    assert_eq!(obj.names().unwrap().collect::<Vec<_>>(), vec!["a", "b", "c"]);
    assert_eq!(r!([1, 2, 3]).set_names(&["a", "b"]), Err(Error::NamesLengthMismatch(r!(["a", "b"]))));
}
source

fn dim(&self) -> Option<Integers>

Get the dim attribute as an integer iterator if one exists.

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

   let array = R!(r#"array(data = c(1, 2, 3, 4), dim = c(2, 2), dimnames = list(c("x", "y"), c("a","b")))"#).unwrap();
   let dim : Vec<_> = array.dim().unwrap().iter().collect();
   assert_eq!(dim, vec![2, 2]);
}
source

fn dimnames(&self) -> Option<ListIter>

Get the dimnames attribute as a list iterator if one exists.

use extendr_api::prelude::*;
test! {
   let array = R!(r#"array(data = c(1, 2, 3, 4), dim = c(2, 2), dimnames = list(c("x", "y"), c("a","b")))"#).unwrap();
   let names : Vec<_> = array.dimnames().unwrap().collect();
   assert_eq!(names, vec![r!(["x", "y"]), r!(["a", "b"])]);
}
source

fn class(&self) -> Option<StrIter>

Get the class attribute as a string iterator if one exists.

use extendr_api::prelude::*;
test! {
   let formula = R!("y ~ A * x + b").unwrap();
   let class : Vec<_> = formula.class().unwrap().collect();
   assert_eq!(class, ["formula"]);
}
source

fn set_class<T>(&mut self, class: T) -> Result<Robj>

Set the class attribute from a string iterator, and returns the same object.

May return an error for some class names.

use extendr_api::prelude::*;
test! {
    let mut obj = r!([1, 2, 3]).set_class(&["a", "b", "c"])?;
    assert_eq!(obj.class().unwrap().collect::<Vec<_>>(), vec!["a", "b", "c"]);
    assert_eq!(obj.inherits("a"), true);
}
source

fn inherits(&self, classname: &str) -> bool

Return true if this object has this class attribute. Implicit classes are not supported.

use extendr_api::prelude::*;
test! {
   let formula = R!("y ~ A * x + b").unwrap();
   assert_eq!(formula.inherits("formula"), true);
}
source

fn levels(&self) -> Option<StrIter>

Get the levels attribute as a string iterator if one exists.

use extendr_api::prelude::*;
test! {
   let factor = factor!(vec!["abcd", "def", "fg", "fg"]);
   let levels : Vec<_> = factor.levels().unwrap().collect();
   assert_eq!(levels, vec!["abcd", "def", "fg"]);
}

Object Safety§

This trait is not object safe.

Implementors§