[Rcpp-devel] basic usage help

Romain Francois romain at r-enthusiasts.com
Wed May 26 19:21:06 CEST 2010


Le 26/05/10 18:55, Vinh Nguyen a écrit :
>
> Dear Rcpp list,
>
> It's time for me to finally try out Rcpp

Great. You won't regret it ;-)

> 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 .
> Call()):

This is where Rcpp comes in. It will make you forget that .Call looks 
silly and complicated. .C seems easier, but that is an illusion.

> 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;

Yes. Rcpp classes of the new api (e.g. Rcpp::NumericMatrix) act as a 
wrapper. They change the way you look at the R object, but not the 
object itself.

However, if you pass a SEXP that really is an integer matrix, Rcpp will 
cast it to a numeric matrix. see the thread : 
http://permalink.gmane.org/gmane.comp.lang.r.rcpp/369 for how that might 
byte you if you are not aware of it.

> 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).

Dirk maintains a this 
http://dirk.eddelbuettel.com/code/rcpp/html/index.html generated by 
doxygen. Some people like it.

The issue with classes like NumericMatrix is that they are generated 
from a template and this makes it difficult for things like doxygen to 
produce something nice. (some people don't like it)

One approximate way is to grep around the unit tests. We do have quite a 
good coverage and we try to stress each functionality of Rcpp.

Otherwise the source code.

> 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.

Not currently. Not that it is difficult to achieve, but it has to raise 
up on our list of priorities.

> 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)?

we don't currently have operators on Rcpp types. maybe some day.

we however developped the RcppArmadillo package, which gives you the 
full power of armadillo (http://arma.sourceforge.net/) plus the 
convenience of Rcpp, at a small cost (see examples in RcppArmadillo)

> 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()?

Something like this:


using namespace Rcpp ; // for _

IntegerVector z(5) ;
NumericMatrix foo( 3, 4 );
return List::create(
	_["x"] = 2,
	_["z"] = z,
	_["foo"] = foo ) ;


You don't need to (but you can if you want) allocate things before. I 
told you .C was only easier by illusion ;-)

In fact, with Rcpp you don't have to care too much about allocation, etc 
... and you can concentrate on the problem you are trying to solve 
rather than fighting with the R API.

> 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:

The easiest way by far is to make a package and follow the guidelines of 
our last release notes. See the section "Using Rcpp in other packages" 
here http://romainfrancois.blog.free.fr/index.php?post/2010/05/17/Rcpp-0.8.0


The next easy way is to build on the smartness of the inline package. 
With verbose = TRUE, it will show you how to run the show:

fx <- cppfunction( , "return R_NilValue ;", verbose = TRUE )


> $ 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
> -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.

We look forward to it.

> Thanks.
> Vinh

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/cork4b : highlight 0.1-8
|- http://bit.ly/bklUXt : RcppArmadillo 0.2.1

`- http://bit.ly/936ck2 : Rcpp 0.8.0



More information about the Rcpp-devel mailing list