Or if you want to show off you could collapse it to > src <- " + NumericMatrix tmpMatrix(as<SEXP>(List(L)[0])); + return wrap(tmpMatrix.ncol()); + " > f <- cxxfunction(signature(L="list"), src, plugin = "Rcpp" ) > f(list(matrix(1:9,3,3))) [1] 3
On Fri, Feb 10, 2012 at 3:01 PM, Douglas Bates <[email protected]> wrote: > Another approach is to force the evaluation of the list element with as<SEXP> > >> src <- " > + List inputList(L) ; > + NumericMatrix tmpMatrix(as<SEXP>(inputList[0])) ; > + > + return wrap(tmpMatrix.ncol()); > + " >> f <- cxxfunction(signature(L="list"), src, plugin = "Rcpp" ) >> f(list(matrix(1:9,3,3))) > [1] 3 > > In answer to Sameer's follow-up question of why it doesn't work > directly to form the NumericMatrix from the list element, try reading > the code. It's complicated. R is an untyped language with an > underlying generic structure called an SEXPREC whereas C++ is a > strongly typed language. It is not always possible to map an > amorphous untyped object to a particular data structure without going > through a lot of template magic. Template meta-programming is not for > the faint of heart. > > On Fri, Feb 10, 2012 at 2:10 PM, Dirk Eddelbuettel <[email protected]> wrote: >> >> 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 >> | [email protected] >> | 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 >> _______________________________________________ >> Rcpp-devel mailing list >> [email protected] >> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel _______________________________________________ Rcpp-devel mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
