[Rcpp-devel] Inconsistent returns

Dirk Eddelbuettel edd at debian.org
Tue Oct 19 20:15:08 CEST 2010


Hi Mitch,

A self-contained example using inline would help...

On 19 October 2010 at 14:06, Lovett, Mitch wrote:
| A clarification on what I wrote - the dims reported are NULL for objects
| that do not report the dims correctly and then when accessing the
| elements as a matrix it acts like a vector (and fails). However, both
| problems only happen some of the time. Other times the correct dims are
| reported and access does not fail.
| 
|  
| 
| From: Lovett, Mitch 
| Sent: Tuesday, October 19, 2010 1:54 PM
| To: 'Rcpp-devel at lists.r-forge.r-project.org'
| Subject: Inconsistent returns
| 
|  
| 
| I am running into some strange behavior with Rcpp objects returned to R.
| Specifically, I run a program that sets values on several large
| matrices. I initialize these and pass the matrices back with the code
| below (I don't include all the code to fill the matrix). So, I pass it
| back as a list. Once in R, I summarize the list to find all the objects
| of the right length. I try to get the dims on the objects and only some
| of them return the correct dims some of the time, while all of them do
| other times. In case it matters, these are running on MacOS and running
| the previous version of Rcpp. Any help would be great.
| 
|  
| 
| Thanks,
| 
| Mitch
| 
|  
| 
| //initialize
| 
| Rcpp::NumericMatrix Res(n,nmcmc/keep);
| 
| Rcpp::NumericMatrix REs(n,nmcmc/keep);
| 
| Rcpp::NumericMatrix Rais(n,nmcmc/keep);
| 
| Rcpp::NumericMatrix Rbis(n,nmcmc/keep);
| 
| Rcpp::NumericMatrix Rthets(thet.n_rows,nmcmc/keep);
| 
| Rcpp::NumericMatrix RmovsE(n/2,mreport);
| 
|  
| 
| arma::mat Es(REs.begin(),n,nmcmc/keep,false);
| 
| arma::mat ais(Rais.begin(),n,nmcmc/keep,false);
| 
| arma::mat bis(Rbis.begin(),n,nmcmc/keep,false);
| 
| arma::mat es(Res.begin(),n,nmcmc/keep,false);  
| 
| arma::mat thets(Rthets.begin(),thet.n_rows,nmcmc/keep,false);
| 
| arma::colvec lls(nmcmc/keep); 
| 
| arma::mat movsE(RmovsE.begin(),n/2,mreport,false);
| 
|  
| 
| //return
| 
| Rcpp::List ret=Rcpp::List::create(Rcpp::Named("lls")=Rcpp::wrap(lls),
| 
| 
|  
| Rcpp::Named("thets")=Rcpp::wrap(thets),
| 
|  
| Rcpp::Named("Es")=Rcpp::wrap(Es),
| 
|  
| Rcpp::Named("es")=Rcpp::wrap(es),
| 
|  
| Rcpp::Named("ais")=Rcpp::wrap(ais),
| 
|  
| Rcpp::Named("bis")=Rcpp::wrap(bis),
| 
|  
| Rcpp::Named("movsAB")=Rcpp::wrap(movsE)

There is a closing ");"  missing here.

| return Rcpp::wrap(ret);

You don't need wrap for the Rcpp::List object.

What I would do now is to print all pairs of dimensions to screen before you
return so 'see what it was in C++'.  Rcpp shouldn't be altering anything; I
suspect you may just have slipped an object creation up here or there.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list