[Rcpp-devel] Accessing/Copying rows/columns of matrices

Dirk Eddelbuettel edd at debian.org
Thu Mar 24 22:56:33 CET 2011


Michael,

On 24 March 2011 at 17:45, Michael Braun wrote:
| Hi, all.  Another question as I embark on my first project that uses Rcpp.
| 
| Consider the following test code, that comes right off of the Using Matrices section of the Rcpp-quickref file on CRAN.
| 
| RcppExport int test() {
| 
|   Rcpp::NumericMatrix xx(3,2);
|   int i;
|   int xsize = xx.nrow() * xx.ncol();
| 
|   for (i = 0; i < xsize; i++) {
|     xx[i] = 7;
|   }
| 
|   Rcpp::NumericMatrix::Column z1 = xx( _,1);   // line 91
| //  Rcpp::NumericVector z2 = xx(_,1); 
| 
|   return 0;
| 
| }
| 
| 
| When I try to compile this code in g++, I get the following:
| 
| g++ -O0 -arch x86_64 -g -Wall -fopenmp -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/x86_64  -I/usr/local/include -I/Library/Frameworks/R.framework/Resources/library/Rcpp/include -c Rcpp_omp.cpp -o Rcpp_omp.o
| Rcpp_omp.cpp: In function ‘int test()’:
| Rcpp_omp.cpp:91: error: ‘_’ was not declared in this scope
| Rcpp_omp.cpp:91: warning: unused variable ‘z1’
| 
| and I get something similar if I comment out the z1 assignment instead of z2.  I tried playing around with spacing around the underscore, to no avail.  This also happens with the Intel compiler, so I doubt it's that.
| 
| >From sessionInfo(), I confirmed that I have Rcpp 0.9.2 installed.
| 
| It seems like I am doing *exactly* what is in the documentation.  Am I missing something?


Yes, a recent enough compiler.

Works here as you could expect with code that is after all in the unit tests.

R> require(inline)
R> src <- '
+   Rcpp::NumericMatrix xx(3,2);
+   int i;
+   int xsize = xx.nrow() * xx.ncol();
+   for (i = 0; i < xsize; i++) {
+     xx[i] = 7;
+   }
+   Rcpp::NumericMatrix::Column z1 = xx( _,1);   // line 91
+   return(Rcpp::wrap(z1));
+ '
R> fun <- cxxfunction(, body=src, plugin="Rcpp")
R> fun()
[1] 7 7 7
R> 

(As an aside, have a look at inline which is what I used for the above. It
makes shooting examples and test cases around much easier.)

Can you possibly work on another account somewhere at your large-enough
university to not be constraint by the too-old-for-us compiler?

Else could you just run Ubuntu in a virtualised environment on your Mac?

Romain, who develops on OS X now, may have tips on how to survive in Steve's
Shiny World.

Sorry to be a bearer of bad news,  Dirk

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


More information about the Rcpp-devel mailing list