File: Rcpp-quickref.Rnw

package info (click to toggle)
rcpp 0.11.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,948 kB
  • ctags: 16,427
  • sloc: ansic: 42,692; cpp: 34,078; makefile: 32; sh: 21
file content (388 lines) | stat: -rw-r--r-- 9,713 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
\documentclass[8pt,twocolumn,a4paper]{article}
%\VignetteIndexEntry{Rcpp-quickref}
%\VignetteEngine{highlight::highlight}
%\VignetteKeywords{Rcpp, reference}
%\VignetteDepends{Rcpp}

\setlength{\hoffset}{-0.8in}
\setlength{\voffset}{-0.8in}

\setlength{\marginparwidth}{0pt}
\setlength{\marginparsep}{0pt}
\setlength{\oddsidemargin}{0pt}
\setlength{\headheight}{0pt}
\setlength{\topmargin}{0pt}
\setlength{\headsep}{0pt}
\setlength{\footskip}{0pt}
\setlength{\textheight}{27cm}
\setlength{\textwidth}{20cm}

\usepackage[colorlinks]{hyperref}

\usepackage{microtype}                     %% cf http://www.khirevich.com/latex/microtype/
\usepackage[T1]{fontenc}		   %% cf http://www.khirevich.com/latex/font/
\usepackage[bitstream-charter]{mathdesign} %% cf http://www.khirevich.com/latex/font/

\newcommand{\pkg}[1]{{\fontseries{b}\selectfont #1}}

<<echo=FALSE,print=FALSE>>=
options( width= 50)
library( "Rcpp" )
prettyVersion <- packageDescription("Rcpp")$Version
prettyDate <- format(Sys.Date(), "%B %e, %Y")
@

\author{Romain Fran\c{c}ois \and Dirk Eddelbuettel}
\title{\pkg{Rcpp} Quick Reference Guide}
\date{\pkg{Rcpp} version \Sexpr{prettyVersion} as of \Sexpr{prettyDate}}

\newsavebox{\quickrefbox}
\definecolor{quickrefBg}{rgb}{0.949019607843137,0.949019607843137,0.949019607843137}
\definecolor{quickrefBorder}{rgb}{0,0,0}
\renewenvironment{Hchunk}%
{%
\vspace{0.5em}\noindent\begin{lrbox}{\quickrefbox}%
\begin{minipage}[b]{.42\textwidth}%
}%
{%
\end{minipage}%
\end{lrbox}%
\fcolorbox{quickrefBorder}{quickrefBg}{\usebox{\quickrefbox}}%
\vspace{0.5em}}%

\begin{document}
\maketitle
\thispagestyle{empty}

% without the ~, latex does not want to newline
% a newline between paragraph and code disconnects them and can orphan heading

\paragraph{Important Notes}~
  \newline
<<lang=cpp>>=
// If you experience compiler errors, please check that you have an appropriate version of g++. See `Rcpp-FAQ' for more information.

// Many of the examples here imply the following:
using namespace Rcpp;
// The inline package adds this for you. Alternately, use e.g.:
Rcpp::NumericVector xx(10);
@

\paragraph{Create simple vectors}~
  \newline
<<lang=cpp>>=
SEXP x; std::vector<double> y(10);

// from SEXP
NumericVector xx(x);

// of a given size (filled with 0)
NumericVector xx(10);
// ... with a default for all values
NumericVector xx(10, 2.0);

// range constructor
NumericVector xx( y.begin(), y.end() );

// using create
NumericVector xx = NumericVector::create(
    1.0, 2.0, 3.0, 4.0 );
NumericVector yy = NumericVector::create(
    Named["foo"] = 1.0,
    _["bar"]     = 2.0 );  // short for Named
@

\paragraph{Extract and set single elements}~
  \newline
<<lang=cpp>>=
// extract single values
double x0 = xx[0];
double x1 = xx(1);

double y0 = yy["foo"];
double y1 = yy["bar"];

// set single values
xx[0] = 2.1;
xx(1) = 4.2;

yy["foo"] = 3.0;

// grow the vector
yy["foobar"] = 10.0;
@

\paragraph{Using matrices}~
  \newline
<<lang=cpp>>=
// Initializing from SEXP,
// dimensions handled automatically
SEXP x;
NumericMatrix xx(x);

// Matrix of 4 rows & 5 columns (filled with 0)
NumericMatrix xx(4, 5);

// Fill with value
int xsize = xx.nrow() * xx.ncol();
for (int i = 0; i < xsize; i++) {
    xx[i] = 7;
}
// Same as above, using STL fill
std::fill(xx.begin(), xx.end(), 8);

// Assign this value to single element
// (1st row, 2nd col)
xx(0,1) = 4;

// Reference the second column
// Changes propagate to xx (same applies for Row)
NumericMatrix::Column zzcol = xx( _, 1);
zzcol = zzcol * 2;

// Copy the second column into new object
NumericVector zz1 = xx( _, 1);
// Copy the submatrix (top left 3x3) into new object
NumericMatrix zz2 = xx( Range(0,2), Range(0,2));
@

\paragraph{Inline}~
  \newline
<<lang=cpp>>=
## Note - this is R code. inline allows rapid testing.
require(inline)
testfun = cxxfunction(
            signature(x="numeric", i="integer"),
            body = '
                NumericVector xx(x);
                int ii = as<int>(i);
                xx = xx * ii;
                return( xx );
            ', plugin="Rcpp")
testfun(1:5, 3)
@

\paragraph{Interface with R}~
  \newline
<<lang=cpp>>=
## In R, create a package shell. For details, see the "Writing R Extensions" manual.

Rcpp.package.skeleton("myPackage")

## Add R code to pkg R/ directory. Call C++ function. Do type-checking in R.

myfunR = function(Rx, Ry) {
    ret = .Call("myCfun", Rx, Ry,
            package="myPackage")
    return(ret)
}

// Add C++ code to pkg src/ directory.
using namespace Rcpp;
// Define function as extern with RcppExport
RcppExport SEXP myCfun( SEXP x, SEXP y) {
    // If R/C++ types match, use pointer to x.  Pointer is faster, but changes to xx propagate to R ( xx -> x == Rx).
    NumericVector xx(x);
    // clone is slower and uses extra memory. Safe, R-like.
    NumericVector yy(clone(y));
    xx[0] = yy[0] = -1.5;
    int zz = xx[0];
    // use wrap() to return non-SEXP objects, e.g:
    // return(wrap(zz));
    // Build and return a list
    List ret; ret["x"] = xx; ret["y"] = yy;
    return(ret);
}

## From shell, above package directory
R CMD check myPackage  ## Optional
R CMD INSTALL myPackage

## In R:
require(myPackage)
aa = 1.5; bb = 1.5; cc =  myfunR(aa, bb)
aa == bb ## FALSE, C++ modifies aa
aa = 1:2; bb = 1:2; cc =  myfunR(aa, bb)
identical(aa, bb)
## TRUE, R/C++ types don't match
@

\paragraph{STL interface}~
  \newline
<<lang=cpp>>=
// sum a vector from beginning to end
double s = std::accumulate(x.begin(),
   x.end(), 0.0);
// prod of elements from beginning to end
int p = std::accumulate(vec.begin(),
    vec.end(), 1, std::multiplies<int>());
// inner_product to compute sum of squares
double s2 = std::inner_product(res.begin(),
    res.end(), res.begin(), 0.0);
@

\paragraph{Function}~
  \newline
<<lang=cpp>>=
Function rnorm("rnorm");
rnorm(100, _["mean"] = 10.2, _["sd"] = 3.2 );
@

\paragraph{Environment}~
    \newline
<<lang=cpp>>=
Environment stats("package:stats");
Environment env( 2 ); // by position

// special environments
Environment::Rcpp_namespace();
Environment::base_env();
Environment::base_namespace();
Environment::global_env();
Environment::empty_env();

Function rnorm = stats["rnorm"];
glob["x"] = "foo";
glob["y"] = 3;
std::string x = glob["x"];

glob.assign( "foo" , 3 );
int foo = glob.get( "foo" );
int foo = glob.find( "foo" );
CharacterVector names = glob.ls()
bool b = glob.exists( "foo" );
glob.remove( "foo" );

glob.lockBinding("foo");
glob.unlockBinding("foo");
bool b = glob.bindingIsLocked("foo");
bool b = glob.bindingIsActive("foo");

Environment e = stats.parent();
Environment e = glob.new_child();
@

\paragraph{Modules}~
  \newline
<<lang=cpp>>=
// Warning -- At present, module-based objects do not persist across quit(save="yes")/reload cycles.  To be safe, save results to R objects and remove module objects before exiting R.

// To create a module-containing package from R, use:
Rcpp.package.skeleton("mypackage",module=TRUE)
// You will need to edit the RcppModules: line of the DESCRIPTION file to match your module name (in this example, from yada to mod_bar).

class Bar {
  public:
    Bar(double x_) :
      x(x_), nread(0), nwrite(0) {}

    double get_x( ) {
      nread++;    return x;
    }

    void set_x( double x_) {
      nwrite++;   x = x_;
    }

    IntegerVector stats() const {
      return IntegerVector::create(
        _["read"] = nread,
        _["write"] = nwrite);
    }
  private:
    double x; int nread, nwrite;
};

RCPP_MODULE(mod_bar) {
  class_<Bar>( "Bar" )
  .constructor<double>()
  .property( "x", &Bar::get_x, &Bar::set_x,
    "Docstring for x" )
  .method( "stats", &Bar::stats,
    "Docstring for stats")
;}

## The following is R code.
require(mypackage); show(Bar)
b <- new(Bar, 10);  b$x <- 10
b_persist <- list(stats=b$stats(), x=b$x)
rm(b)
@

\newpage

\paragraph{Rcpp sugar}~
  \newline
<<lang=cpp>>=
NumericVector x = NumericVector::create(
  -2.0, -1.0, 0.0, 1.0, 2.0 );
IntegerVector y = IntegerVector::create(
  -2, -1, 0, 1, 2 );

NumericVector xx = abs( x );
IntegerVector yy = abs( y );

bool b = all( x < 3.0 ).is_true() ;
bool b = any( y > 2 ).is_true();

NumericVector xx = ceil( x );
NumericVector xx = ceiling( x );
NumericVector yy = floor( y );
NumericVector yy = floor( y );

NumericVector xx = exp( x );
NumericVector yy = exp( y );

NumericVector xx = head( x, 2 );
IntegerVector yy = head( y, 2 );

IntegerVector xx = seq_len( 10 );
IntegerVector yy = seq_along( y );

NumericVector xx = rep( x, 3 );
NumericVector xx = rep_len( x, 10 );
NumericVector xx = rep_each( x, 3 );

IntegerVector yy = rev( y );
@



\paragraph{Random functions}~
  \newline
<<lang=cpp>>=
// Set seed
RNGScope scope;

// For details see Section 6.7.1--Distribution functions of the `Writing R Extensions' manual. In some cases (e.g. rnorm), distribution-specific arguments can be omitted; when in doubt, specify all dist-specific arguments. The use of doubles rather than integers for dist-specific arguments is recommended. Unless explicitly specified, log=FALSE.

// Equivalent to R calls
NumericVector xx = runif(20);
NumericVector xx1 = rnorm(20);
NumericVector xx1 = rnorm(20, 0);
NumericVector xx1 = rnorm(20, 0, 1);

// Example vector of quantiles
NumericVector quants(5);
for (int i = 0; i < 5; i++) {
    quants[i] = (i-2);
}

// in R, dnorm(-2:2)
NumericVector yy = dnorm(quants) ;
NumericVector yy = dnorm(quants, 0.0, 1.0) ;

// in R, dnorm(-2:2, mean=2, log=TRUE)
NumericVector yy = dnorm(quants, 2.0, true) ;

// Note - cannot specify sd without mean
// in R, dnorm(-2:2, mean=0, sd=2, log=TRUE)
NumericVector yy = dnorm(quants, 0.0, 2.0, true) ;

// To get original R api, use Rf_*
double zz = Rf_rnorm(0, 2);
@



\end{document}