RcppExamples provides concrete examaples of how to use Rcpp. It alsoan easy template for people wanting to useRcpp in theirpackages as it will be easier to wrap one’s head around a smallerpackage such as RcppExamples.
A simple example may illustrate this:
#include<Rcpp.h>#include<cmath>usingnamespace Rcpp;// suncc needs help to disambiguate between sqrt( float ) and sqrt(double)inlinestaticdouble sqrt_double(double x){return::sqrt( x);}// [[Rcpp::export]]List NumericVectorExample(const NumericVector& orig){ 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 elementstd::transform(orig.begin(), orig.end(), vec.begin(), sqrt_double);return List::create(Named("result")= vec, Named("original")= orig);}With essentially five lines of code, we provide a function that takesany numeric vector and returns both the original vector and a tranformedversion—here by applying a square root operation. Even the looping alongthe vector is implicit thanks to the generic programming idioms of theStandard Template Library.
Nicer still, even on misuse, exceptions get caught cleanly and we getreturned to the R promptwithout any explicit coding on thepart of the user (which really is a standard feature of Rcpp and theRcpp Attributes interface mechanism used here):
R>library(RcppExamples)R> RcppExamples::RcppNumericVectorExample(1:5)$result[1]1.000001.414211.732052.000002.23607$original[1]12345R>RcppNumericVectorExample(c("tic","tac","toe"))Error: not compatible with requested typeR>RcppExamples does not document every class but it should alreadyprovide a fairly decent start for using Rcpp. Many more actual usageexamples are … in the over one thousand unit tests inRcpp — and ofcourse in by now almost one hundred examples of theRcpp Gallery.
The NEWS from all releases:
Changes in RcppExamples version 0.1.7 (2016-01-23)
All examples were updated to useRcpp Attributes and (wherepossible) use
const &interfaces.Updated
DESCRIPTIONfor currentR CMD checkstandardsThe
Rcpp package is now imported rather than dependedupon.Added
README.mdas well as.travis.yml.Also updated and refreshed all manual pages and R files.
Changes in RcppExamples version 0.1.6 (2013-01-15)
Moved
NEWS.Rdfrom top-level directory to correct locationinst/per CRAN maintainer suggestionChanges in RcppExamples version 0.1.5 (2012-12-27)
Moved all examples using
RcppClassic to a new packageRcppClassicExamples Various minor small updates
Changes in RcppExamples version 0.1.4 (2012-08-09)
Added new example for Rcpp sugar and vectorised draws of RNGs
Minor updates to reflect newer CRAN Policy
Changes in RcppExamples version 0.1.3 (2011-12-28)
Added new example for Rcpp::DataFrame
Switched two examples from using std::cout (which ‘Writing R Extensions’recommends agains) to the new Rcpp::Rcout device
Minor .Rd correction, suppressing one warning
Changes in RcppExamples version 0.1.2 (2010-12-20)
Updated src/Makevars
Now depends also on RcppClassic so that we can keep continue to showexamples using the classic API
Added examples for Rcpp::Date, Rcpp::Datetime and Rcpp::List
Changes in RcppExamples version 0.1.1 (2010-07-29)
Minor update, no new examples or documentation added yet
Changes in RcppExamples version 0.1.0 (2010-03-10)
Initial release as a package, carved out of Rcpp 0.7.7
RcppExamples is now aCRAN packageand lives otherwise in its own repo onGitHub.
RcppExamples is being written by Dirk Eddelbuettel and RomainFrancois.
RcppExamples is licensed under the GNU GPL version 2 or later.
Initially created: Thu Mar 11 11:14:31 CST 2010
Last modified: Fri Dec 20 08:18:30 CST 2024