[Rcpp-devel] problem with sample() implemented in Rcpp/RcppArmadillo

Dirk Eddelbuettel edd at debian.org
Sat Oct 27 16:44:11 CEST 2012


Rahel,

Thanks for posting on the list.

On 26 October 2012 at 19:08, Rahel Sollmann wrote:
| Hi Rcpp folks;
| I am very new to c++ so I apologize in advance if this is a stupid question:
| For a larger analysis I am trying to implement the R function sample() in c++
| via RcppArmadillo. I want to use the resulting vector as an index to access a
| matrix. A simple dummy example of what I am trying to do is below. By breaking
| the code into several pieces I have been able to determine that the problem
| lies with the vector produced by sample() ('guys' in the example below) and the
| way I am attempting to use it as an index for the matrix tmpMat. I have tried
| making 'um', 'unmarked' and 'guys' integers/IntegerVectors but that didn't
| resolve my problem either.
| The function compiles fine but when I run it in R I get this nondescript error
| message: 'Error in fun(y, n, J, lam, um) :'
| Any help on this would be greatly appreciated.

I don;t have time to debug everybody's code, especially when the code is
longer and complex.  Can you possibly

  a) indent the code so that reading it is a little easier

  b) minimize it -- your email states an error in fun(... 5 parameters ...) 
     try to make it a function of 4, 3, 2, 1, ... parameters. Sometimes that
     exercise alone will lead you to fixing the issue

Otherwise, as others said, calling back into R from C++ __repeatedly__ is
almost always a sign of having the wrong design.

Dirk


| Thanks,
| Rahel
| 
| library(inline)
| library(RcppArmadillo)
| noccs<-5
| umn<-20
| J=15
| y<-list(matrix(0, nrow=umn, ncol=J),matrix(0, nrow=umn, ncol=J),matrix(0, nrow=
| umn, ncol=J),
| matrix(0, nrow=umn, ncol=J),matrix(0, nrow=umn, ncol=J) )
| n<-5
| um=c(1:20)
| lam <-rep(0.3, umn)
| 
| code<-'Rcpp::Environment base("package:base");
| Rcpp::Function sample = base["sample"];
| 
| Rcpp::List obs = y;
| int ntraps = Rcpp::as<int>(J);
| int npics= Rcpp::as<int>(n);
| arma::vec probs= Rcpp::as<arma::vec>(lam);
| arma::vec unmarked = Rcpp::as<arma::vec>(um);
| bool txtx = FALSE;
| 
| for (int t=0; t<5; t++ ) {
| arma::mat tmpMat = Rcpp::as<arma::mat>(obs(t));
| 
|    for (int k=0; k<ntraps; k++ ) {
| 
|               probs= probs/sum(probs);
|                arma::vec guys= Rcpp::as<arma::vec>(sample(unmarked, npics,_
| ["replace"] = txtx, probs));
|        
|         for (int kk=0; kk<npics; kk++){
|         tmpMat(guys(kk,0),k)=1;
|         }
|        
|         }
|     obs(t)=tmpMat;
|     }
| 
| return Rcpp::wrap(obs);
| '
| fun<-cxxfunction(signature(y="list", J="integer",n="integer", lam="numeric",
|              um="numeric"), body=code, plugin="RcppArmadillo"  )
| xx<-fun(y,J,n,lam,um)
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list