[Rcpp-commits] r2497 - pkg/Rcpp/inst/doc/Rcpp-modules
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Nov 23 12:21:24 CET 2010
Author: romain
Date: 2010-11-23 12:21:24 +0100 (Tue, 23 Nov 2010)
New Revision: 2497
Modified:
pkg/Rcpp/inst/doc/Rcpp-modules/Rcpp-modules.Rnw
Log:
some more
Modified: pkg/Rcpp/inst/doc/Rcpp-modules/Rcpp-modules.Rnw
===================================================================
--- pkg/Rcpp/inst/doc/Rcpp-modules/Rcpp-modules.Rnw 2010-11-23 11:02:21 UTC (rev 2496)
+++ pkg/Rcpp/inst/doc/Rcpp-modules/Rcpp-modules.Rnw 2010-11-23 11:21:24 UTC (rev 2497)
@@ -212,10 +212,12 @@
\proglang{C++} code and the \proglang{R} code easier.
+
\section{Rcpp modules}
Rcpp modules are inspired from Python modules that are generated by the
-\texttt{Boost.Python} library \citep{Abrahams+Grosse-Kunstleve:2003:Boost.Python}. They provide an easy way
+\texttt{Boost.Python} library \citep{Abrahams+Grosse-Kunstleve:2003:Boost.Python}.
+They provide an easy way
to expose \proglang{C++} functions and classes to \proglang{R}, grouped
together in a single entity.
@@ -225,24 +227,23 @@
\subsection{Exposing \proglang{C++} functions}
-Consider the \texttt{hello} function from the previous section.
+Consider the \texttt{norm} function from the previous section.
We can expose it to \proglang{R} :
<<lang=cpp>>=
-const char* hello( std::string who ){
- std::string result( "hello " ) ;
- result += who ;
- return result.c_str() ;
+using namespace Rcpp ;
+
+double norm( double x, double y ){
+ return sqrt( x*x + y*y ) ;
}
-RCPP_MODULE(yada){
- using namespace Rcpp ;
- function( "hello", &hello ) ;
+RCPP_MODULE(mod){
+ function( "norm", &norm ) ;
}
@
The code creates an Rcpp module called \texttt{yada}
-that exposes the \texttt{hello} function. \pkg{Rcpp} automatically
+that exposes the \texttt{norm} function. \pkg{Rcpp} automatically
deduces the conversions that are needed for input and output. This alleviates
the need for a wrapper function using either \pkg{Rcpp} or the \proglang{R} API.
@@ -251,8 +252,8 @@
<<eval=FALSE>>=
require( Rcpp )
-yada <- Module( "yada" )
-yada$hello( "world" )
+mod <- Module( "mod" )
+mod$norm( 3, 4 )
@
A module can contain any number of calls to \texttt{function} to register
More information about the Rcpp-commits
mailing list