forked from RcppCore/Rcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRcpp-quickref.Rmd
554 lines (430 loc) · 11.7 KB
/
Rcpp-quickref.Rmd
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
---
title: \pkg{Rcpp} Quick Reference Guide
# Use letters for affiliations
author:
- name: Dirk Eddelbuettel
affiliation: a
- name: Romain François
affiliation: b
address:
- code: a
address: \url{http://dirk.eddelbuettel.com}
- code: b
address: \url{https://romain.rbind.io/}
# For footer text
lead_author_surname: Eddelbuettel and François
# Place DOI URL or CRAN Package URL here
doi: "https://cran.r-project.org/package=Rcpp"
# Abstract
abstract: |
This document provides short code snippets that are helpful for using the
\pkg{Rcpp} \citep{CRAN:Rcpp,JSS:Rcpp,Eddelbuettel:2013:Rcpp}.
# Optional: Acknowledgements
# acknowledgements: |
# Optional: One or more keywords
keywords:
- Rcpp
- quickref
- R
- C++
# Font size of the document, values of 9pt (default), 10pt, 11pt and 12pt
fontsize: 9pt
# Optional: Force one-column layout, default is two-column
#one_column: true
# Optional: Enables lineo mode, but only if one_column mode is also true
#lineno: true
# Optional: Enable one-sided layout, default is two-sided
#one_sided: true
# Optional: Bibliography
bibliography: Rcpp
# Optional: Enable a 'Draft' watermark on the document
#watermark: false
# Customize footer, eg by referencing the vignette
footer_contents: "Rcpp Vignette"
# Produce a pinp document
output: pinp::pinp
header-includes: >
\newcommand{\proglang}[1]{\textsf{#1}}
\newcommand{\pkg}[1]{\textbf{#1}}
\newcommand{\faq}[1]{FAQ~\ref{#1}}
\newcommand{\rdoc}[2]{\href{http://www.rdocumentation.org/packages/#1/functions/#2}{\code{#2}}}
\newcommand{\sugar}{\textsl{Rcpp sugar}~}
vignette: >
%\VignetteIndexEntry{Rcpp-quickref}
%\VignetteKeywords{Rcpp, quickref, R, Cpp}
%\VignettePackage{Rcpp}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
# Important Notes
```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:
#include <Rcpp.h>
using namespace Rcpp;
// The cppFunction will automatically add this.
// Or, prefix Rcpp objects with the Rcpp namespace
// as e.g. in:
Rcpp::NumericVector xx(10);
```
# Create simple vectors
```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
```
# Extract and set single elements
```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;
```
# Using matrices
```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 submatrix (top left 3x3) into new object
NumericMatrix zz2 = xx( Range(0,2), Range(0,2));
```
# Inline C++ Compile in R
```{r, eval = FALSE}
## Note - this is R code.
## cppFunction in Rcpp allows rapid testing.
require(Rcpp)
cppFunction("
NumericVector exfun(NumericVector x, int i){
x = x*i;
return x;
}")
exfun(1:5, 3)
## Use evalCpp to evaluate C++ expressions
evalCpp("std::numeric_limits<double>::max()")
```
# Interface with R
## First step in R
```{r, eval = FALSE}
# In R, create a package shell. For details,
# see the "Writing R Extensions" manual and
# the "Rcpp-package" vignette.
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)
}
```
## Additional C++
```cpp
// 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. No side effects.
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);
}
```
## On the command-line
```sh
# From shell, above package directory
R CMD build myPackage
R CMD check myPackage_1.0.tar.gz ## Optional
R CMD INSTALL myPackage_1.0.tar.gz
```
## Back in R
```{r, eval=FALSE}
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
# so a copy was made
```
# STL interface
```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);
```
# Rcpp Attributes
## In C++
```cpp
// Add code below into C++ file Rcpp_example.cpp
#include <Rcpp.h>
using namespace Rcpp;
// Place the 'Rcpp::export' tag
// right above function declaration.
// [[Rcpp::export]]
double muRcpp(NumericVector x){
int n = x.size(); // Size of vector
double sum = 0; // Sum value
// For loop, note cpp index shift to 0
for(int i = 0; i < n; i++){
// Shorthand for sum = sum + x[i]
sum += x[i];
}
return sum/n; // Obtain and return the Mean
}
// Place dependent functions above call or
// declare the function definition with:
double muRcpp(NumericVector x);
// [[Rcpp::export]]
double varRcpp(NumericVector x, bool bias = true){
// Calculate the mean using C++ function
double mean = muRcpp(x);
double sum = 0;
int n = x.size();
for(int i = 0; i < n; i++){
sum += pow(x[i] - mean, 2.0); // Square
}
return sum/(n-bias); // Return variance
}
```
## In R:
```{r, eval=FALSE}
Rcpp::sourceCpp("path/to/file/Rcpp_example.cpp")
x <- 1:5
all.equal(muRcpp(x), mean(x))
all.equal(var(x),varRcpp(x))
```
# Rcpp Extensions
```cpp
// Enable C++11
// [[Rcpp::plugins(cpp11)]]
// Enable OpenMP (excludes macOS)
// [[Rcpp::plugins(openmp)]]
// Use the RcppArmadillo package
// Requires different header file from Rcpp.h
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
```
# Rcpp sugar
```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 );
```
# Random Number Generation functions}
```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), dist-specific
// arguments can be omitted; when in doubt,
// specify all dist-specific arguments. The use
// of doublesrather 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);
```
# Environment
```cpp
// Special environments
Environment::Rcpp_namespace();
Environment::base_env();
Environment::base_namespace();
Environment::global_env();
Environment::empty_env();
// Obtain an R environment
Environment stats("package:stats");
Environment env( 2 ); // by position
Environment glob = Environment::global_env();
// Extract function from specific environment
Function rnorm = stats["rnorm"];
// Assign into the environment
glob["x"] = "foo";
glob["y"] = 3;
// Retrieve information from environment
std::string x = glob["x"];
glob.assign( "foo" , 3 );
int foo = glob.get( "foo" );
int foo = glob.find( "foo" );
CharacterVector names = glob.ls(TRUE)
bool b = glob.exists( "foo" );
glob.remove( "foo" );
// Administration
glob.lockBinding("foo");
glob.unlockBinding("foo");
bool b = glob.bindingIsLocked("foo");
bool b = glob.bindingIsActive("foo");
// Retrieve related environments
Environment e = stats.parent();
Environment e = glob.new_child();
```
# Calling Functions in R
```cpp
// Do NOT expect to have a performance gain
// when calling R functions from R!
// Retrieve functions from default loaded env.
Function rnorm("rnorm");
rnorm(100, _["mean"] = 10.2, _["sd"] = 3.2 );
// Passing in an R function and obtaining results
// Make sure function conforms with return type!
NumericVector callFunction(NumericVector x,
Function f) {
NumericVector res = f(x);
return res;
}
/*** R
# The following is R code executed
# by sourceCpp() as a convenience.
x = 1:5
callFunction(x, sum)
*/
```
# Modules
```cpp
// Warning -- 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:
// Rcpp.package.skeleton("mypackage", module=TRUE)
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")
;}
/*** R
## The following is R code.
require(mypackage) s
how(Bar)
b <- new(Bar, 10)
b$x <- 10
b_persist <- list(stats=b$stats(), x=b$x)
rm(b)
*/
```