Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Seamless R and C++ Integration with Rcpp
Seamless R and C++ Integration with Rcpp
Seamless R and C++ Integration with Rcpp
Ebook387 pages2 hours

Seamless R and C++ Integration with Rcpp

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Rcpp is the glue that binds the power and versatility of R with the speed and efficiency of C++.  With Rcpp, the transfer of data between R and C++ is nearly seamless, and high-performance statistical computing is finally accessible to most R users.  Rcpp should be part of every statistician's toolbox.  -- Michael Braun, MIT Sloan School of Management

 "Seamless R and C++ integration with Rcpp" is simply a wonderful book.  For anyone who uses C/C++ and R, it is an indispensable resource.  The writing is outstanding.  A huge bonus is the section on applications. This section covers the matrix packages Armadillo and Eigen and the GNU Scientific Library as well as RInside which enables you to use R inside C++. These applications are what most of us need to know to really do scientific programming with R and C++. I love this book. -- Robert McCulloch, University of Chicago Booth School of Business

 Rcpp is now considered an essential package for anybody doing serious computational research using R. Dirk's book is an excellent companion and takes the reader from a gentle introduction to more advanced applications via numerous examples and efficiency enhancing gems. The book is packed with all you might have ever wanted to know about Rcpp, its cousins (RcppArmadillo, RcppEigen .etc.), modules, package development and sugar. Overall, this book is a must-have on your shelf. -- Sanjog Misra, UCLA Anderson School of Management

The Rcpp package represents a major leap forward for scientific computations with R. With very few lines of C++ code, one has R's data structures readily at hand for further computations in C++. Hence, high-level numerical programming can be made in C++ almost as easily as in R, but often with a substantial speed gain. Dirk is a crucial person in these developments, and his book takes the reader from the first fragile steps on to using the full Rcpp machinery. A very recommended book! --Søren Højsgaard, Department of Mathematical Sciences, Aalborg University, Denmark 

 

"Seamless R and C ++ Integration with Rcpp" provides the first comprehensive introduction to Rcpp. Rcpp has become the most widely-used language extension for R, and is deployed by over one-hundred different CRAN and BioConductor packages. Rcpp permits users to pass scalars, vectors, matrices, list or entire R objects back and forth between R and C++ with ease. This brings the depth of the R analysis framework together with the power, speed, and efficiency of C++.

Dirk Eddelbuettel has been a contributor to CRAN for over a decade and maintains around twenty packages.  He is the Debian/Ubuntu maintainer for R and other quantitative software, edits the CRAN Task Views for Finance and High-Performance Computing, is a co-founder of the annual R/Finance conference, and an editor of the Journal of Statistical Software.  He holds a Ph.D. in Mathematical Economics from EHESS (Paris), and works in Chicago as a Senior Quantitative Analyst.

LanguageEnglish
PublisherSpringer
Release dateJun 4, 2013
ISBN9781461468684
Seamless R and C++ Integration with Rcpp

Related to Seamless R and C++ Integration with Rcpp

Titles in the series (18)

View More

Related ebooks

Applications & Software For You

View More

Related articles

Reviews for Seamless R and C++ Integration with Rcpp

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Seamless R and C++ Integration with Rcpp - Dirk Eddelbuettel

    Part 1

    Introduction

    Dirk EddelbuettelUse R!Seamless R and C++ Integration with Rcpp201310.1007/978-1-4614-6868-4_1© The Author 2013

    1. A Gentle Introduction to Rcpp

    Dirk Eddelbuettel¹ 

    (1)

    River Forest, Illinois, USA

    Abstract

    This initial chapter provides a first introduction to Rcpp. It uses a somewhat slower pace and generally more gentle approach than the rest of the book in order to show key concepts which are revisited and discussed in more depth throughout the remainder. So the aim of this chapter is to cover a fairly wide range of material, but at a more introductory level for an initial overview. Two larger examples are studied in detail. We first compute the Fibonacci sequence in three different ways in two languages. Second, we simulate from a multivariate dynamic model provided by a vector autoregression.

    1.1 Background: From R to C++

    R is both a powerful interactive environment for data analysis, visualization, and modeling and an expressive programming language designed and built to support these tasks. The interactive nature of working with data—through data displays, summaries, model estimation, simulation, and numerous other tasks—is a key strength of the R environment. And, so is the R programming language which permits use from interactive explorations to small scripts and all the way to complete implementations of new functionality. This R programming language is in fact a dialect of the S programming language initially developed by Bell Labs.

    The dual nature of interactive analysis, as well as programming, is no accident. As succinctly expressed in the title of one of the books on the S language (which has provided the foundations upon which R is built), it is designed to support Programming with Data (Chambers 1998). That is a rather unique proposition as far as programming languages go. As a domain-specific language (DSL), R is tailored specifically to support and enable data analysis work. Moreover, there is also a particular focus on research use for developing new and exciting approaches, as well as solidifying existing approaches. R and its predecessor S are not static languages: they have evolved since the first designs well over thirty years ago and continue to evolve today.

    To mention just one example of this evolution, object orientation in R is supported by the S3 and S4 class systems, as well as the newer Reference Classes. Of course, such flexibility of having alternate approaches can also be seen as a weakness. It may lead to yet more material which language beginners may find perplexing, and it may lead to small inconsistencies which may confuse intermediate and advanced users. Coincidentally, similar concerns are also sometimes raised about the C + +  language. These arguments have some merit, but on the margin more useful and actually used languages are preferable to those that are very cleanly designed, yet not used much.

    Having a proper programming language is a key feature supporting rigorous and reproducible research: by encoding all steps of a data analysis and estimation in a script or program, the analyst makes every aspect of the process explicit and thereby ensures full reproducibility.

    Consider the first example which is presented below. It is a slightly altered version of an example going back to a post by Greg Snow to the r-help mailing list.

    Listing 1.1 Plotting a density in R

    1  xx <- faithful$eruptions

       fit <- density(xx)

    3  plot(fit)

    We assign a new variable xx by extracting the named component eruptions of the (two-column) data.frame faithful included with the R system. The data set contains waiting times between eruptions, as well as eruption duration times, at the Old Faithful geyser in the Yellowstone National Park in the USA. To estimate the density function of eruption duration based on this data, we then call the R function density (which uses default arguments besides the data we pass in). This function returns an object we named fit, and the plot function then visualizes it as shown in the corresponding Fig. 1.1.

    A305558_1_En_1_Fig1_HTML.gif

    Fig. 1.1

    Plotting a density in R

    This is a nice example, and it illustrates some features of R such as the object-oriented nature in which we can simply plot an object returned from a modeling function. However, this example was introduced primarily to provide the basis for an extension also provided by Greg Snow and shown in the next listing.

    Listing 1.2 Plotting a density and bootstrapped confidence interval in R

    1  xx <- faithful$eruptions

       fit1 <- density(xx)

    3  fit2 <- replicate(10000, {

           x <- sample(xx,replace=TRUE);

    5      density(x, from=min(fit1$x), to=max(fit1$x))$y

       })

    7  fit3 <- apply(fit2, 1, quantile,c(0.025,0.975))

       plot(fit1, ylim=range(fit3))

    9  polygon(c(fit1$x,rev(fit1$x)),

               c(fit3[1,], rev(fit3[2,])),

    11         col=’grey’, border=F)

       lines(fit1)

    The first two lines are identical apart from now assigning to an object fit1 holding the estimated density. Lines three to six execute a minimal bootstrapping exercise. The replicate() function repeats N (here 10, 000) times the code supplied in the second argument. Here, this argument is a code block delimited by braces, containing two instructions. The first instruction creates a new data set by resampling with replacement from the original data. The second instruction then estimates a density on this resampled data. This time the data range is limited to the range of the initial estimated in fit1; this ensures that the bootstrapped density is estimated on the same grid of x values as in fit1. For this data set, the grid contains 512 points. We retain only the y coordinates of the fit—these will be collected as the N columns in the resulting object fit2 making this a matrix of dimension 512

    Enjoying the preview?
    Page 1 of 1