Skip to contents

Transfers array data to R and returns it as an R array. Only in the case of scalars is the result a vector of length 1, as R arrays cannot have 0 axes.

Usage

# S3 method for class 'AnvlArray'
as_array(x, check = "warn", ...)

as_array(x, ...)

Arguments

x

(arrayish)
An array-like object.

check

(character(1) | FALSE)
How to report a materialized value that the R type cannot hold: "warn" (the default) warns and returns it anyway, "err" aborts, and FALSE skips the scan. Problematic values are the bit-representations reserved for R's NA values, as well as out-of-range values. See the Gotchas article for more.

...

Passed on to methods.

Value

(array | vector(1))
An R array with the input's shape, or – for a scalar, which R cannot represent as an array – a vector of length 1.

Details

This is implemented via the generic xlamisc::as_array().

Data types

R has fewer data types than anvl, so the values are converted to the R type that can represent them:

Data typeR type
f32, f64double
i8, i16, i32, ui8, ui16integer
i64, ui32, ui64bit64::integer64
boollogical

This has two consequences:

  • An f32 value is widened to a double and keeps the rounding error of the 32-bit float, e.g. as_array(nv_scalar(0.1)) is not exactly 0.1.

  • Some integer values cannot be represented in R: an i32 or i64 value equal to the smallest representable integer is read as NA, and a ui64 value >= 2^63 wraps to a negative number. The check argument decides whether this is reported.

To obtain a different R type, convert the array with nv_convert() first, or use the coercion functions described in as.double().

Examples

x <- nv_array(1:4, dtype = "f32")
as_array(x)
#> [1] 1 2 3 4
y <- nv_scalar(1L)
# R arrays can't have 0 axes:
as_array(y)
#> [1] 1