NA handling
Opened this issue · 4 comments
JosiahParry commented
Use extendr types for inputs which allow for missing values. Illustrate using match or ifelse statements to handle missing values. Discuss options:
- return a missing value (Rscalar::na() method or ().into_robj() for null
- Return a default value
- skip and return a smaller result using filter_map()
Ilia-Kosenkov commented
We also have Nullable<T>
to handle explicit NULL
values
JosiahParry commented
JosiahParry commented
Here's a minimal example of using this. @Ilia-Kosenkov what would you say the desired use pattern is for Nullable
?
I can see it being helpful in my code here: https://github.com/JosiahParry/rsgeo/blob/32d0d7e2f0faab349b5261f964a0091760b55fcd/src/rust/src/boundary.rs#L149-L159
struct MyStruct();
#[extendr]
impl MyStruct {
}
#[extendr]
fn mynull(x: bool) -> Nullable<MyStruct> {
match x {
true => Nullable::NotNull(MyStruct()),
false => Nullable::Null,
}
}
mynull(TRUE)
#> <pointer: 0x1>
#> attr(,"class")
#> [1] "MyStruct"
mynull(FALSE)
#> NULL
Ilia-Kosenkov commented
I think one of the best use-cases is NULL
default values, e.g.
#[extendr(use_try_from = true)]
fn with_optional_input(#[default = "NULL"] idx : Nullable<Integers>) -> Integers {
match idx {
NotNull(x) => x,
Null => (0..5).collect::<Integers>() /* do not remember the syntax */
}
}