[Rcpp-commits] r2277 - pkg/Rcpp/inst/doc/Rcpp-FAQ

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Oct 6 21:35:43 CEST 2010


Author: edd
Date: 2010-10-06 21:35:42 +0200 (Wed, 06 Oct 2010)
New Revision: 2277

Modified:
   pkg/Rcpp/inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw
Log:
add 'can I use templates with Rcpp/inline' question


Modified: pkg/Rcpp/inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw
===================================================================
--- pkg/Rcpp/inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw	2010-10-06 19:35:14 UTC (rev 2276)
+++ pkg/Rcpp/inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw	2010-10-06 19:35:42 UTC (rev 2277)
@@ -211,6 +211,46 @@
 If and when \texttt{LinkingTo} changes and lives up to its name, we will be
 sure to adapt \pkg{Rcpp} as well.
 
+
+\subsection{Can I use templates with \pkg{Rcpp} and \pkg{inline} ? }
+
+\begin{quote}
+  \emph{I'm curious whether one can provide a class definition inline in an R
+    script and then initialize an instance of the class and call a method on
+    the class, all inline in R.}
+\end{quote}
+
+\noindent Most certainly, consider this simple example of a templated class
+which squares its argument:
+
+<<echo=FALSE>>=
+writeLines( '
+inc <- \'template <typename T>
+        class square : public std::unary_function<T,T> {
+        public:
+            T operator()( T t) const { return t*t ;}
+        };
+       \'
+
+src <- \'
+       double x = Rcpp::as<double>(xs);
+       int i = Rcpp::as<int>(is);
+       square<double> sqdbl;
+       square<int> sqint;
+       return Rcpp::DataFrame::create(Rcpp::Named("x", sqdbl(x)),
+                                      Rcpp::Named("i", sqint(i)));
+       \'
+fun <- cxxfunction(signature(xs="numeric", is="integer"),
+                   body=src, include=inc, plugin="Rcpp")
+
+fun(2.2, 3L)
+', "templateUse.R")
+@
+<<echo=FALSE,results=tex>>=
+ex_highlight( "templateUse.R" )
+@
+
+
 \subsection{Does \pkg{Rcpp} work on windows}
 
 Yes of course. See the Windows binaries provided by CRAN.
@@ -311,6 +351,7 @@
 It should be noted that code below depends on the version \texttt{0.3.5} of
 \pkg{inline} and the version \texttt{0.2.2} of \pkg{RcppArmadillo}
 
+
 \subsection{Is the API documented ? }
 
 You bet. We use \proglang{doxygen} to generate html, latex and man page



More information about the Rcpp-commits mailing list