[Rcpp-commits] r2055 - papers/rjournal
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Aug 29 01:40:52 CEST 2010
Author: edd
Date: 2010-08-29 01:40:52 +0200 (Sun, 29 Aug 2010)
New Revision: 2055
Modified:
papers/rjournal/EddelbuettelFrancois.tex
Log:
added (simplified) Rcpp::as<> signature
standardized code examples w.r.t. semicolons
Modified: papers/rjournal/EddelbuettelFrancois.tex
===================================================================
--- papers/rjournal/EddelbuettelFrancois.tex 2010-08-28 22:11:27 UTC (rev 2054)
+++ papers/rjournal/EddelbuettelFrancois.tex 2010-08-28 23:40:52 UTC (rev 2055)
@@ -331,9 +331,9 @@
code more concisely:
\begin{example}
-CharacterVector ab(2) ;
-ab[0] = "foo" ;
-ab[1] = "bar" ;
+CharacterVector ab(2);
+ab[0] = "foo";
+ab[1] = "bar";
\end{example}
\section{R and C++ data interchange}
@@ -353,7 +353,7 @@
\begin{example}
template <typename T>
-SEXP wrap(const T& object) ;
+SEXP wrap(const T& object);
\end{example}
The templated function takes a reference to a `wrappable`
@@ -392,9 +392,9 @@
m1["foo"] = 1; m1["bar"] = 2;
m2["foo"] = 1; m2["bar"] = 2; m2["baz"] = 3;
-v.push_back( m1) ;
-v.push_back( m2) ;
-Rcpp::wrap( v ) ;
+v.push_back( m1);
+v.push_back( m2);
+Rcpp::wrap( v );
\end{example}
The code creates a list of two named vectors, equal to the
@@ -408,7 +408,13 @@
\subsection{R to C++ : as}
The reversed conversion is implemented by variations of the
-\code{Rcpp::as} template. It offers less flexibility and currently
+\code{Rcpp::as} template whose signature is:
+\begin{example}
+template <typename T>
+T as(SEXP x);
+\end{example}
+
+It offers less flexibility and currently
handles conversion of R objects into primitive types (bool, int, std::string, ...),
STL vectors of primitive types (\code{std::vector<bool>},
\code{std::vector<double>}, etc ...) and arbitrary types that offer
@@ -416,6 +422,8 @@
be fully or partially specialized to manage conversion of R data
structures to third-party types.
+
+
\subsection{Implicit use of converters}
The converters offered by \code{wrap} and \code{as} provide a very
@@ -431,19 +439,19 @@
// assuming the global environment contains
// a variable 'x' that is a numeric vector
Rcpp::Environment global =
-\ \ \ \ Rcpp::Environment::global_env()
+\ \ \ \ Rcpp::Environment::global_env();
// extract a std::vector<double> from
// the global environment
-std::vector<double> vx = global["x"] ;
+std::vector<double> vx = global["x"];
// create a map<string,string>
-std::map<std::string,std::string> map ;
-map["foo"] = "oof" ;
-map["bar"] = "rab" ;
+std::map<std::string,std::string> map;
+map["foo"] = "oof";
+map["bar"] = "rab";
// push the STL map to R
-global["y"] = map ;
+global["y"] = map;
\end{example}
In the first part of the example, the code extracts a
@@ -512,10 +520,10 @@
\ \ LCONS( install("rnorm"),
\ \ \ \ CONS(ScalarInteger(10),
\ \ \ \ \ \ CONS(ScalarReal(100.0),R_NilValue))));
-SET_TAG( CDDR(call), install("sd") ) ;
+SET_TAG( CDDR(call), install("sd") );
SEXP res = PROTECT(eval(call, R_GlobalEnv));
-UNPROTECT(2) ;
-return res ;
+UNPROTECT(2);
+return res;
\end{example}
\end{minipage}
\caption{\pkg{Rcpp} versus the R API: Four ways of calling \code{rnorm(10L, sd=100)} in C / C++.
@@ -631,14 +639,14 @@
\begin{example}
> src <- '
+ Rcpp::List input(data);
-+ Rcpp::Function f(fun) ;
++ Rcpp::Function f(fun);
+ Rcpp::List output(input.size());
+ std::transform(
+ input.begin(), input.end(),
+ output.begin(),
-+ f ) ;
-+ output.names() = input.names() ;
-+ return output ;
++ f );
++ output.names() = input.names();
++ return output;
+ '
> cpp_lapply <- cfunction(
+ \ \ signature(data="list", fun = "function"),
@@ -684,10 +692,10 @@
\begin{example}
> fun <- cfunction(signature(x = "integer"),'
-+ int dx = as<int>(x) ;
++ int dx = as<int>(x);
+ if( dx > 10 )
+ throw std::range_error("too big") ;
-+ return wrap(dx*dx) ;
++ return wrap(dx*dx);
+ ', Rcpp = TRUE,
+ includes = "using namespace Rcpp;" )
> tryCatch( fun(12),
@@ -749,19 +757,19 @@
RcppExport SEXP convolve4cpp(SEXP a, SEXP b)\{
Rcpp::NumericVector xa(a);
Rcpp::NumericVector xb(b);
- int n_xa = xa.size() ;
- int n_xb = xb.size() ;
+ int n_xa = xa.size();
+ int n_xb = xb.size();
Rcpp::NumericVector xab(n_xa + n_xb - 1);
- double* pa = xa.begin() ;
- double* pb = xb.begin() ;
- double* pab = xab.begin() ;
+ double* pa = xa.begin();
+ double* pb = xb.begin();
+ double* pab = xab.begin();
int i,j=0;
for (i = 0; i < n_xa; i++)
for (j = 0; j < n_xb; j++)
pab[i + j] += pa[i] * pb[j];
- return xab ;
+ return xab;
\}
\end{example}
More information about the Rcpp-commits
mailing list