pub struct AsNumericVec(pub Vec<Option<f64>>);Expand description
A numeric vector read like R’s as.numeric(): from a double, integer,
logical, character, or factor (by its labels) vector. NA of any type is
None.
Data read from CSV files or spreadsheets often arrives as character, with
numbers mixed with tokens such as "n/a" or "<0.1", or as a factor whose
labels are the numbers. This marker takes any of those, maps every kind of
NA to None, and names the values that are not numbers in one error.
§Reading rules
| R input | Result |
|---|---|
| double | as is; NA_real_ → None, NaN stays Some(NaN) |
| integer, logical | widened to f64; NA → None |
| character | parsed as as.numeric() does (see below) |
| factor | each label (levels(x)[x]) parsed as character, never the codes; code NA → None |
| anything else (list, raw, complex, …) | SexpError::Type |
Character parsing follows R’s String2Real (RealFromString in
src/main/coerce.c):
NA_character_→None.- A blank string (empty, or whitespace only) →
None, asas.numeric("")givesNAwithout a warning. - The token
"NA", with blanks around it at most, →None. This followsscan()/type.convert(), which read"NA"as missing;as.numeric("NA")also givesNA, but with a coercion warning. - Otherwise R’s own
R_strtodreads a number, and the rest of the string must be blank. That accepts surrounding spaces,"Inf","-inf","Infinity","NaN","1e3","+.5","5.", and hex such as"0x1A"or"0x1p3", exactly as the running R does. - Anything else fails. All failures are collected and reported together:
non-numeric value(s): "n/a", "<0.1" (elements 2, 5), with 1-based element numbers and at most 10 values listed (the rest as"; and N more").
§no_na
#[miniextendr(no_na)] refuses everything this marker reads as missing,
not only what R’s anyNA() sees: the R guard refuses NA (and a numeric
NaN) before the call, and the C wrapper checks the converted value
after it, refusing None (the token "NA", blank strings, factor labels
among those) and NaN (from the text "NaN" too). Both raise the same
'x' must not contain NA argument error. NULL given to an
Option<AsNumericVec> is still None, as not given. A type alias or a
#[derive(TryFromSexp)] newtype of the marker is checked the same way.
Option<AsNumericVec> (and Option<AsNumeric>) also accept NULL as
None.
§IntoR
The markers convert to R as their inner Vec<Option<f64>> /
Option<f64>: a double vector with NA for None, which the marker
reads back unchanged (NaN stays Some(NaN), though a NaN carrying R’s
NA payload reads back as None, as R_IsNA says), and Option<marker>
gives NULL for None. That is what lets a #[miniextendr] trait method
take a marker parameter: the trait’s View converts each argument with
IntoR and the implementing side reads it back with TryFromSexp. A
#[miniextendr] function returning a marker gives the same R value as
returning .0; returning .0 stays the idiom.
§Example
use miniextendr_api::{miniextendr, AsNumericVec};
#[miniextendr]
fn total(x: AsNumericVec) -> f64 {
x.0.into_iter().flatten().sum()
}
// R: total(c(" 1.5 ", "NA", "", "0x10")) → 17.5
// total(factor(c("10", "2"))) → 12 (labels, not codes)
// total(c("1", "n/a", "3", "4", "<0.1"))
// → error: non-numeric value(s): "n/a", "<0.1" (elements 2, 5)Tuple Fields§
§0: Vec<Option<f64>>Trait Implementations§
Source§impl Clone for AsNumericVec
impl Clone for AsNumericVec
Source§impl Debug for AsNumericVec
impl Debug for AsNumericVec
Source§impl Default for AsNumericVec
impl Default for AsNumericVec
Source§impl IntoR for AsNumericVec
impl IntoR for AsNumericVec
Source§type Error = <Vec<Option<f64>> as IntoR>::Error
type Error = <Vec<Option<f64>> as IntoR>::Error
Source§fn try_into_sexp(self) -> Result<SEXP, Self::Error>
fn try_into_sexp(self) -> Result<SEXP, Self::Error>
Source§unsafe fn try_into_sexp_unchecked(self) -> Result<SEXP, Self::Error>
unsafe fn try_into_sexp_unchecked(self) -> Result<SEXP, Self::Error>
Source§unsafe fn into_sexp_unchecked(self) -> SEXP
unsafe fn into_sexp_unchecked(self) -> SEXP
Source§impl PartialEq for AsNumericVec
impl PartialEq for AsNumericVec
impl StructuralPartialEq for AsNumericVec
Source§impl TryFromSexp for AsNumericVec
impl TryFromSexp for AsNumericVec
Source§fn __mx_has_na(&self) -> bool
fn __mx_has_na(&self) -> bool
#[miniextendr(no_na)]
refuses after the conversion. Read moreSource§fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error>
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error>
Source§const NATIVE_BORROW: Option<NativeBorrow> = None
const NATIVE_BORROW: Option<NativeBorrow> = None
Source§const CHARACTER_ONLY: bool = false
const CHARACTER_ONLY: bool = false
NULL, is a character vector or a factor. Read moreSource§#[doc(hidden)]const __MX_EXPECTATION: Option<&'static str> = None
#[doc(hidden)]const __MX_EXPECTATION: Option<&'static str> = None
#[miniextendr] cannot tell from the type as written: the
<expected> of a failed conversion’s '<p>' must be <expected>: .... Read moreSource§unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error>
unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error>
Source§#[doc(hidden)]unsafe fn __mx_try_from_rooted(root: Root<'_>) -> Result<Self, Self::Error>
#[doc(hidden)]unsafe fn __mx_try_from_rooted(root: Root<'_>) -> Result<Self, Self::Error>
Source§#[doc(hidden)]fn __mx_input_has_na(&self, input: SEXP) -> bool
#[doc(hidden)]fn __mx_input_has_na(&self, input: SEXP) -> bool
#[miniextendr(no_na)] refuses this converted argument, read
from input (the R value it converted from), when no R guard ran. Read moreSource§#[doc(hidden)]fn __mx_try_from_sexp_with_check(
sexp: SEXP,
check: &ElementCheck<'_>,
) -> Result<Self, SexpError>
#[doc(hidden)]fn __mx_try_from_sexp_with_check(
sexp: SEXP,
check: &ElementCheck<'_>,
) -> Result<Self, SexpError>
check, an element type’s
TryFromSexpElement::check_sexp,
on the R value that carries an element’s class (#1837). Read moreSource§#[doc(hidden)]unsafe fn __mx_try_from_sexp_unchecked_with_check(
sexp: SEXP,
check: &ElementCheck<'_>,
) -> Result<Self, SexpError>
#[doc(hidden)]unsafe fn __mx_try_from_sexp_unchecked_with_check(
sexp: SEXP,
check: &ElementCheck<'_>,
) -> Result<Self, SexpError>
Auto Trait Implementations§
impl Freeze for AsNumericVec
impl RefUnwindSafe for AsNumericVec
impl Send for AsNumericVec
impl Sync for AsNumericVec
impl Unpin for AsNumericVec
impl UnsafeUnpin for AsNumericVec
impl UnwindSafe for AsNumericVec
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ConsumingFallible for Twhere
T: Clone,
Source§impl<I> OptionInner for I
impl<I> OptionInner for I
Source§const OPTION_NATIVE_BORROW: Option<NativeBorrow> = const OPTION_NATIVE_BORROW: Option<NativeBorrow> =
<Option<I> as TryFromSexp>::NATIVE_BORROW;
const OPTION_NATIVE_BORROW: Option<NativeBorrow> = const OPTION_NATIVE_BORROW: Option<NativeBorrow> = <Option<I> as TryFromSexp>::NATIVE_BORROW;
<Option<Self> as TryFromSexp>::NATIVE_BORROW.Source§const OPTION_CHARACTER_ONLY: bool = const OPTION_CHARACTER_ONLY: bool =
<Option<I> as TryFromSexp>::CHARACTER_ONLY;
const OPTION_CHARACTER_ONLY: bool = const OPTION_CHARACTER_ONLY: bool = <Option<I> as TryFromSexp>::CHARACTER_ONLY;
<Option<Self> as TryFromSexp>::CHARACTER_ONLY.Source§impl<T> RDebug for Twhere
T: Debug,
impl<T> RDebug for Twhere
T: Debug,
Source§impl<T> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
core_io_internals)[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreSource§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize = _
#[doc(hidden)]const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize = _
#[doc(hidden)]const ALIGN: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment = _
#[doc(hidden)]const ALIGNMENT: Alignment = _
ptr_alignment_type)Source§#[doc(hidden)]const IS_ZST: bool = _
#[doc(hidden)]const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout = _
#[doc(hidden)]const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize = _
#[doc(hidden)]const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read moreLayout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 24 bytes