I want to create a list of matrices that I am updating in a loop and return it to R. I have
std::vector<IntegerMatrix> zb_list;
and
IntegerMatrix tb(J,nmax), zb(J,nmax);
before the loop. Inside the loop, I update zb and then have
zb_list.push_back(zb);
I also have
Rcout << (zb_list[itr]) << "\n";
Rcout << (zb) << "\n\n";
where itr counts the iterations. These both confirm that zb is changing inside the loop and zb_list keeps track of it.
Then I return zb_list after the loop. When accessing the result in R, the list contains copies of the same zb, the last one computed in the loop. I suspect there is some pass by reference going on... but can't figure it out. I don't have a good understanding of what is going on (tried to use return(wrap(zb_list))without luck) but clearly something is wrong. Also used List zb_list; for defining it which doesn't help. Any suggestions?
EDiT: Here is the minimal working example:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List test_weird(int ITRmax=2) {
IntegerMatrix zb(2,2);
std::vector<IntegerMatrix> zb_list;
int itr = 0;
while (itr < ITRmax) {
zb( (1+itr)%2 ,(1+itr)%2 ) ++ ;
zb_list.push_back(zb);
Rcout << (zb) << (zb_list[itr]) << "\n\n";
++itr;
}
return List::create(_["zb"] = zb,
_["zb_list"] = zb_list);
}
/*** R
res <- test_weird()
res$zb_list
*/
This the output when the look is running:
0 0
0 1
0 0
0 1
1 0
0 1
1 0
0 1
... and this is the output from R:
> res$zb_list
[[1]]
[,1] [,2]
[1,] 1 0
[2,] 0 1
[[2]]
[,1] [,2]
[1,] 1 0
[2,] 0 1
As you can see both items in the list are the last zb in the loop.