r-lib / cpp11

cpp11 helps you to interact with R objects using C++ code.
https://cpp11.r-lib.org/
Other
193 stars 46 forks source link

Idiomatic way to cast or convert between different r_vector types #264

Open vspinu opened 2 years ago

vspinu commented 2 years ago

The following conversions results in a run time error Invalid input type, expected 'integer' actual 'logical'

// cpp11::logicals val
cpp11::integers ints(val);
cpp11::integers ints = cpp11::as_cpp<cpp11::integers>(val);

It's not clear to me why logicals cannot be re-interpreted as integers. After all it's the same internal representation.

What is an idiomatic way to convert within cpp11 when converting at R level is not an option? Is it enough to cpp11::integers X2(Rf_coerceVector(X, INTSXP));, or do I still need to protect the converted vector?

It would be great if this information could be added to the conversion section of the vignette.

Thanks! `