[Rcpp-devel] accessing list elements without using names

Dirk Eddelbuettel edd at debian.org
Fri Feb 10 21:10:17 CET 2012


On 10 February 2012 at 14:49, Sameer Soi wrote:
| Hello Rcpp'ers,
| 
| I want to access a List of matrices in an Rcpp/C++ function that will passed to
| me via R...
| 
| src <- "
|   using namespace Rcpp ;
|   List inputList(L) ;
|   NumericMatrix tmpMatrix ;
| 
|   tmpMatrix = NumericMatrix(inputList[0]) ;
|   return(wrap(tmpMatrix.ncol())) ;
| "
| test <- cxxfunction(signature(L="list"), src, plugin = "Rcpp" )
| 
| This gives me the following error-
| 
| file30b9664194fc.cpp: In function ‘SEXPREC* file30b9664194fc(SEXPREC*)’:
| file30b9664194fc.cpp:34: error: call of overloaded ‘Matrix
| (Rcpp::internal::generic_proxy<19>)’ is ambiguous
| /usr/share/R/include/Rcpp/vector/SubMatrix.h:57: note: candidates are:
| Rcpp::Matrix<RTYPE>::Matrix(const Rcpp::SubMatrix<RTYPE>&) [with int RTYPE =
| 14]
| /usr/share/R/include/Rcpp/vector/Matrix.h:67:
| note:                 Rcpp::Matrix<RTYPE>::Matrix(const
| Rcpp::Matrix<RTYPE>&) [with int RTYPE = 14]
| /usr/share/R/include/Rcpp/vector/Matrix.h:65:
| note:                 Rcpp::Matrix<RTYPE>::Matrix(const int&)
| [with int RTYPE = 14]
| /usr/share/R/include/Rcpp/vector/Matrix.h:46:
| note:                 Rcpp::Matrix<RTYPE>::Matrix(const
| Rcpp::Dimension&) [with int RTYPE = 14]
| /usr/share/R/include/Rcpp/vector/Matrix.h:39:
| note:                 Rcpp::Matrix<RTYPE>::Matrix(SEXPREC*)
| [with int RTYPE = 14]
| make: *** [file30b9664194fc.o] Error 1
| 
| In looking at the error, it's not clear to me how these candidates will help.
| What am I missing in telling Rcpp what class of object it should be expecting
| coming out of inputList?

Template code gets complicate. The subsetting (of the List) confuses here.
And one more statement and all is well -- you were very close!

R> library(inline)
R> src <- '
+   Rcpp::List inputList(L) ;
+   Rcpp::NumericMatrix tmpMatrix ;
+   SEXP tmp = inputList[0];
+   tmpMatrix = Rcpp::NumericMatrix(tmp) ;
+   return(wrap(tmpMatrix.ncol())) ;
+ '
R> f <- cxxfunction(signature(L="list"), src, plugin = "Rcpp" )
R> f(list(matrix(1:9,3,3)))
[1] 3
R> 


Hope this helps,  Dirk

| 
| I know this kind of question (i.e. I want a 3d cube etc) pops up frequently and
| the answer is typically "use arma's 3d cubes" (e.g. http://stackoverflow.com/
| questions/8159872/r-list-of-numeric-vectors-c-2d-array-with-rcpp), but I don't
| intend on doing linear algebra. My data really is just a series of matrices of
| arbitrary sizes.
| 
| Also, I looked into multi-dimensional arrays, but how to access them seems
| unclear; I did see a post on this list to that nature but the solution seemed
| inelegant.
| 
| Thanks in advance for any help!
| 
| ----------------------------------------------------------------------
| _______________________________________________
| 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
-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list