[Rcpp-devel] basic usage help

Vinh Nguyen vinhdizzo at gmail.com
Wed May 26 18:55:55 CEST 2010

Dear Rcpp list,

It's time for me to finally try out Rcpp since I have to do some
computing for my research.  Some questions (please be kind as I'm only
a beginner C programmer using .C() and not smart enough to understand .

1.  I plan to use objects like
Rcpp::NumericMatrix orig(matrix);
to store.  To confirm, storing to orig directly stores into the
original R object matrix right?
orig(i,j) = some double;

2.  int n = orig.rows();
Where can I find documentation on all the functions associated with
the class?  I'd like to know all the available functions plus
information about it (for example, the above returns an int).

3.  Is there a class for multidimensional arrays?  I'm thinking of
array() in R.  I use something like
S2[i + (*nObs)*j + (*nObs)*(*nParam)*k] = ...;
for all vectors, matrices, and arrays in C, and this is a pain as it
is a souce of a lot of my errors.

4.  My original code is using nlm() in R to fit my model. However, I
think it is best to do the Newton-Raphson directly in C/C++.  Can some
how evaluate matrix multiplication using "%*%" in R directly in my C++
code (using the Rcpp::NumericMatrix class)?

5.  How do I return a list in R that consists of a value, vector, and
matrix?  The three can be allocated in C++ or allocated prior to
entering C++ and passed on in .Call()?

6.  How do I compile the .cpp file on the
command line?  I got the following based on Dirk's HPC talk but
getting errors on my mac os x:

$ PKG_CPPFLAGS='r -e'Rcpp:::CxxFlags\(\)'' PKG_LIBS='r
-e'Rcpp:::LdFlags\(\)'' R CMD SHLIB rcpp.cpp
g++ -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include
-I/Library/Frameworks/R.framework/Resources/include/x86_64 r
-eRcpp:::CxxFlags() -I/usr/local/include    -fPIC  -g -O2 -c rcpp.cpp
-o rcpp.o
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `g++ -arch x86_64
-I/Library/Frameworks/R.framework/Resources/include/x86_64 r
-eRcpp:::CxxFlags() -I/usr/local/include    -fPIC  -g -O2 -c rcpp.cpp
-o rcpp.o'
make: *** [rcpp.o] Error 2

If I get these things going then I'd be happy to release the code as
an example on using Rcpp to do maximization/model fitting.


More information about the Rcpp-devel mailing list