Movatterモバイル変換


[0]ホーム

URL:


Rcpp: Seamless R and C++ Integration

Extending and growing R applications via an easy-to-use, robust, and performant C++ interface.

All key R data types map naturally into key C++ data structures, and simple interfaces make transfer back and forth very easy. And by composing more complex interfaces become possible.

Numerous examples, as well as extensive documentation, facilitate its use.

First Steps are Easy!

Rcpp makes it easy to create a first example. Just usecppFunction("...") and supply a valid C++ function in the argument string.

Here is a simple example from the introductory vignette:

library("Rcpp")cppFunction("bool isOddCpp(int num = 10) {   bool result = (num % 2 == 1);   return result;}")isOddCpp(42L)

Of course, that function could be written in R (and we show an R variant in that vignette) but this provides a nice and simple stepping stone.

Recursive Functions Are Easy Too!

Rcpp makes it equally easy to source code from a file viasourcCpp("...") and supply a path to a file as the argument.

The well-known Fibonacci sequence can set-up this way:

#include "Rcpp.h"// [[Rcpp::export]]int fibonacci(const int x) {   if (x< 2) return(x);   return (fibonacci(x - 1)) + fibonacci(x - 2);}/*** Rsystem.time(print(fibonacci(30)))*/

This shows another nice trick: We can include an R example use in our C++ file by placing it behind a marker of/*** R. Try it! On my computer, this call is still so fast thatsystem.time() can barely measure it!.

Rcpp Packages

Rcpp is used byover 3000 packages on CRAN alone. Below is a small selection of some of the key packages.

Rcpp
RcppArmadillo
RcppEigen
RcppParallel
rstan
forecast

The Gallery Has Numerous Examples!

The related websitegallery.rcpp.org contains over one hundred distinct examples, ranging from simple first steps on how to create certain data types for features to more complex applications examples. All examples are fully tested and should be runnable.

Best of all, it is open website to whichyou could contribute the next article! Seegallery.rcpp.org for more.

Ten PDF Vignettes

The Rcpp package comes with ten pdf vignettes documenting both first steps and more advanced use cases.

Thercpp-devel mailing can help with additional user- or developer questions.

Rcpp Attributes
Rcpp Extending
Rcpp FAQ
Rcpp Introduction
Rcpp Libraries
Rcpp Modules
Rcpp Package
Rcpp Quick Reference
Rcpp Sugar
Rcpp Introduction (2011)

The Rcpp Book

The book, published in the SpringeruseR! series, isavailable from the publisher as both a soft-cover version (ISBN 978-1-4614-6867-7) and an e-book (ISBN 978-1-4614-6868-4). Institutions with a Springer Link subscription can also access it. Springer provideselectronic previews.

The book is also available fromAmazon and other on-line booksellers.


[8]ページ先頭

©2009-2026 Movatter.jp