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<&mut Self>
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<&mut Self>
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<&mut Self>
where T: IntoIterator,
T::IntoIter: ExactSizeIterator,
T::Item: ToVectorValue + AsRef<str> { ... }
fn inherits(&self, classname: &str) -> bool { ... }
fn levels(&self) -> Option<StrIter> { ... }
}
Expand description
Provides access to the attributes of an R object.
The Attribute
trait provides a consistent interface to getting, setting, and checking for the presence of attributes in an R object.
Provided Methods§
Sourcefn get_attrib<'a, N>(&self, name: N) -> Option<Robj>
fn get_attrib<'a, N>(&self, name: N) -> Option<Robj>
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)));
}
Sourcefn has_attrib<'a, N>(&self, name: N) -> bool
fn has_attrib<'a, N>(&self, name: N) -> bool
Return true if an attribute exists.
Sourcefn set_attrib<N, V>(&mut self, name: N, value: V) -> Result<&mut Self>
fn set_attrib<N, V>(&mut self, name: N, value: V) -> Result<&mut Self>
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");
robj.set_attrib(sym!(xyz), 1)?;
assert_eq!(robj.get_attrib(sym!(xyz)), Some(r!(1)));
}
Sourcefn names(&self) -> Option<StrIter>
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"]);
}
Sourcefn set_names<T>(&mut self, names: T) -> Result<&mut Self>
fn set_names<T>(&mut self, names: T) -> Result<&mut Self>
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]);
obj.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"]))));
}
Sourcefn dim(&self) -> Option<Integers>
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]);
}
Sourcefn dimnames(&self) -> Option<ListIter>
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"])]);
}
Sourcefn class(&self) -> Option<StrIter>
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"]);
}
Sourcefn set_class<T>(&mut self, class: T) -> Result<&mut Self>
fn set_class<T>(&mut self, class: T) -> Result<&mut Self>
Set the class
attribute from a string iterator, and return the same
object.
May return an error for some class names.
use extendr_api::prelude::*;
test! {
let mut obj = r!([1, 2, 3]);
obj.set_class(&["a", "b", "c"])?;
assert_eq!(obj.class().unwrap().collect::<Vec<_>>(), vec!["a", "b", "c"]);
assert_eq!(obj.inherits("a"), true);
}
Sourcefn inherits(&self, classname: &str) -> bool
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);
}
Sourcefn levels(&self) -> Option<StrIter>
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"]);
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl Attributes for Complexes
impl Attributes for Doubles
impl Attributes for Integers
impl Attributes for List
impl Attributes for Logicals
impl Attributes for Strings
impl Attributes for Robj
impl<T> Attributes for Dataframe<T>
impl<T> Attributes for ExternalPtr<T>
set_attrib