Replies: 3 comments 17 replies
-
|
It may help to step back. 'Plain old scalar' types like Otherwise what you do in your post seems roughly correct. The |
Beta Was this translation helpful? Give feedback.
-
|
For Armadillo matrices, you can use a small lambda expression, initialise with arma advanced ctor, and invoke it immediately: cppFunction("
arma::mat foo(Nullable<NumericMatrix> m = R_NilValue) {
const arma::mat& x = [&m] {
if (m.isNotNull()) {
NumericMatrix M(m);
arma::mat x(M.begin(), M.nrow(), M.ncol(), false, true); // copy_aux_mem is set to false, no copying
return x;
} else {
arma::mat x(0, 0, arma::fill::none);
return x;
}
}(); // invoke!
return x;
}
/*** R
foo(m = NULL)
foo(m = matrix(1:4, ncol = 2))
*/
", depends="RcppArmadillo")From R console: foo()
<0 x 0 matrix>
foo(matrix(1:4, ncol = 2))
[,1] [,2]
[1,] 1 3
[2,] 2 4 |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I found the gallery post on working with Nullable, but couldn't find guidance specific to scalar types (integers and doubles) or Armadillo matrices. I wanted to check if the pattern below is a recommended approach.
Beta Was this translation helpful? Give feedback.
All reactions