RcppExamples 0.1.0

March 10, 2010

(This article was first published on dirk.eddelbuettel, and kindly contributed to R-bloggers)

Version 0.1.0 of RcppExamples, a simple demo package for Rcpp should appear on CRAN some time tomorrow.

As mentioned in the
post about release 0.7.8 of Rcpp, Romain and I carved this
out of Rcpp itself to provide a cleaner separation of code that implements our
R / C++ interfaces (which remain in Rcpp)
and code that illustrates how to use it — which is now in RcppExamples.
This also provides an easier template for people wanting to use Rcpp in their packages as it will be easier to wrap one’s head around the much
smaller RcppExamples package.

A simple example (using the newer API) may illustrate this:

#include <Rcpp.h> RcppExport SEXP newRcppVectorExample(SEXP vector) { Rcpp::NumericVector orig(vector); // keep a copy (as the classic version does) Rcpp::NumericVector vec(orig.size()); // create a target vector of the same size // we could query size via // int n = vec.size(); // and loop over the vector, but using the STL is so much nicer // so we use a STL transform() algorithm on each element std::transform(orig.begin(), orig.end(), vec.begin(), sqrt); Rcpp::Pairlist res(Rcpp::Named( "result", vec), Rcpp::Named( "original", orig)); return res;

With essentially five lines of code, we provide a function that takes any
numeric vector and returns both the original vector and a tranformed
version—here by applying a square root operation. Even the looping along
the vector is implicit thanks to the generic programming idioms of the
Standard Template Library.

Nicer still, even on misuse, exceptions get caught cleanly and we get
returned to the R prompt without any explicit coding on the part of
the user:

R> library(RcppExamples)
Loading required package: Rcpp
R> print(RcppVectorExample( 1:5, "new" )) # select new API
[1] 1.000 1.414 1.732 2.000 2.236 $original
[1] 1 2 3 4 5 R> RcppVectorExample( c("foo", "bar"), "new" )
Error in RcppVectorExample(c("foo", "bar"), "new") : not compatible with INTSXP

There is also analogous code for the older API in the package, but it is
about three times as long, has to loop over the vector and needs to
set up the execption handling explicitly.

As of right now, RcppExamples does not document every class but it should already
provide a fairly decent start for using Rcpp. And many more actual usage
examples are … in the over two-hundred unit tests in Rcpp.

Update: Now actually showing new rather than classic API.

To leave a comment for the author, please follow the link and comment on their blog: dirk.eddelbuettel.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)