The Rcpp package test notes


Test Environment: OS: Windows XP, R: R 2.12.2(G:\Program Files\R\R-2.12.2\), Rcpp: 0.9.4

1. Download and install the Rtools(for windows): http://www.murdoch-sutherland.com/Rtools/
I installed the Rtools into “G:\Rtools“.

2. Set Enviroment Path for windows:
Desktop -> right-click My Computer -> click Properties -> click the Advanced tab -> click Environment Variables button
-> highlight the “Path” variable in the Systems Variable section -> click the Edit button -> addG:\Rtools\bin;G:\Rtools\MinGW\bin;” there (“C:\MiKTeX\miktex\bin;” may needed too.)
and
in the Systems Variable section -> click the New button -> add a new variable named “CYGWIN” with value “nodosfilewarning” (Turn off MS-DOS style path warnings with cygwin utilities)

3. Install the R package “Rcpp” and “inline”.

4. Quick test using inline

library(inline);
library("Rcpp");

inc <- '
double norm(double x, double y){
return sqrt(x*x+y*y);
}

RCPP_MODULE(mod){
 function("norm", &norm);
}
'
fx <- cxxfunction( signature(), "" , include = inc, plugin = "Rcpp" );
mod <- Module( "mod", getDynLib(fx) );
mod$norm(3,4);

Result:

[1] 5

5. Quick test of writing a simple package

Rcpp.package.skeleton("RcppTestPackage");
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './RcppTestPackage/Read-and-delete-me'.

Adding Rcpp settings
 >> added Depends: Rcpp
 >> added LinkingTo: Rcpp
 >> added useDynLib directive to NAMESPACE
 >> added Makevars file with Rcpp settings
 >> added Makevars.win file with Rcpp settings
 >> added example header file using Rcpp classes
 >> added example src file using Rcpp classes
 >> added example R file calling the C++ example
 >> added Rd file for rcpp_hello_world

Now you can find the created file in “C:\Documents and Settings\Administrator\My Documents\RcppTestPackage” (or your working directory)

Now we can compile and install the test package:
Windows Start -> run -> cmd -> cd C:\Documents and Settings\Administrator\My Documents
C:\Documents and Settings\Administrator\My Documents>
“G:\Program Files\R\R-2.12.2\bin\i386\Rcmd” check RcppTestPackage
“G:\Program Files\R\R-2.12.2\bin\i386\Rcmd” INSTALL RcppTestPackage
(“G:\Program Files\R\R-2.12.2\bin\i386\Rcmd” build RcppTestPackage (This is the file you can upload to CRAN.))

Finally, go to R and test your package:

library(RcppTestPackage);
rcpp_hello_world();

Result:

[[1]]
[1] "foo" "bar"

[[2]]
[1] 0 1

6. Create a simple package using Rcpp Module:

Rcpp.package.skeleton("RcppTestModule");

Find the created file in “C:\Documents and Settings\Administrator\My Documents\RcppTestPacakge” (or your working directory)
Go to the “src” folder and make changes to the files “rcpp_hello_world.h” and “rcpp_hello_world.cpp”,
Let’s write something meaningful:

//rcpp_hello_world.h
#ifndef _RcppTestPacakge_RCPP_HELLO_WORLD_H
#define _RcppTestPacakge_RCPP_HELLO_WORLD_H

#include <Rcpp.h>

/*
 * note : RcppExport is an alias to `extern "C"` defined by Rcpp.
 *
 * It gives C calling convention to the rcpp_hello_world function so that 
 * it can be called from .Call in R. Otherwise, the C++ compiler mangles the 
 * name of the function and .Call can't find it.
 *
 * It is only useful to use RcppExport when the function is intended to be called
 * by .Call. See the thread http://thread.gmane.org/gmane.comp.lang.r.rcpp/649/focus=672
 * on Rcpp-devel for a misuse of RcppExport
 */

#endif
//rcpp_hello_world.cpp
#include "rcpp_hello_world.h"

using namespace Rcpp;

double norm(double x, double y){
return sqrt(x*x+y*y);
}

RCPP_MODULE(mod){
 function("norm", 
	      &norm,
		  List::create( _["x"] = 0.0, _["y"] = 0.0),
		  "Provides a simple vector norm");
}

Similarly, compile and install the test package:
Windows Start -> run -> cmd -> cd C:\Documents and Settings\Administrator\My Documents
C:\Documents and Settings\Administrator\My Documents>
“G:\Program Files\R\R-2.12.2\bin\i386\Rcmd” check RcppTestModule
“G:\Program Files\R\R-2.12.2\bin\i386\Rcmd” INSTALL RcppTestModule

Finally, test our package in R:

library("RcppTestModule");
mod <- Module( "mod", PACKAGE = "RcppTestModule");
mod$norm(3,4);

Result:

[1] 5

and

mod$norm();

Result:

[1] 0

also

show(mod$norm);

Result:

internal C++ function <0241A0A8>
    docstring : Provides a simple vector norm
    signature : double norm(double, double)

Source code of package “RcppTestModule”: https://bzstat.googlecode.com/svn/trunk/blogpost/RcppTestModule

Links:

http://dirk.eddelbuettel.com/code/rcpp.html
http://dirk.eddelbuettel.com/code/rcpp/Rcpp-package.pdf
http://dirk.eddelbuettel.com/code/rcpp/Rcpp-modules.pdf
http://cran.r-project.org/doc/manuals/R-exts.html
http://www.stat.psu.edu/~dsy109/SOS_Talk.pdf
http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf

Advertisements
This entry was posted in programming, r. Bookmark the permalink.

One Response to The Rcpp package test notes

  1. Pingback: Using Lib FANN in R via Rcpp | Bruce Zhou on Statistics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s