This post discusses some issues with the proxy model for parameter passing in Rcpp. However, when I implemented this function:
// [[Rcpp::export]]
void test_size(NumericVector test){
NumericVector test2(test);
NumericVector test3 = NumericVector::create(1,1,1,1,1);
test2 = test3;
Rf_PrintValue(test);
}
We get:
> temp = c(2,2,2,2)
> test_size(temp)
[1] 2 2 2 2
So the problem is that the previous post and this book say that in this case test2
should be a pointer to the underlying SEXP
object from R
. However, when we assigned test2 = test3
, this didn't apply to test
because the test
NumericVector
remained unchanged.
updated
I am adding an example where I think assignment isn't working as Dirk suggested, but of course I could be misunderstanding the problem.
So suppose I have the following function:
// [[Rcpp::export]]
NumericVector testing(){
NumericMatrix mat(3,3);
mat.row(0) = NumericVector::create(1,1,1);
mat.row(1) = NumericVector::create(1,1,1);
mat.row(2) = NumericVector::create(2,2,2);
NumericVector test;
NumericVector test2;
for (int i = 0; i < mat.nrow(); i++){
test = mat.row(i);
if (test[0] == 1){
test2 = test;
}
}
return test2;
}
This function is supposed to output 1,1,1
, but instead it outputs 2,2,2
. However when I replace test2 = test
with test2 = clone(test)
then I get the correct output. So I was wondering why am I getting this behavior even though this is just assignment as Dirk suggested?