Seamless R and C++ Integration with Rcpp
()
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.
Related to Seamless R and C++ Integration with Rcpp
Titles in the series (18)
Simulation and Inference for Stochastic Processes with YUIMA: A Comprehensive R Framework for SDEs and Other Stochastic Processes Rating: 0 out of 5 stars0 ratingsApplied Spatial Data Analysis with R Rating: 3 out of 5 stars3/5Seamless R and C++ Integration with Rcpp Rating: 0 out of 5 stars0 ratingsBayesian Networks in R: with Applications in Systems Biology Rating: 0 out of 5 stars0 ratingsA Beginner's Guide to R Rating: 0 out of 5 stars0 ratingsRetirement Income Recipes in R: From Ruin Probabilities to Intelligent Drawdowns Rating: 0 out of 5 stars0 ratingsStatistical Analysis of Network Data with R Rating: 2 out of 5 stars2/5Epidemics: Models and Data using R Rating: 0 out of 5 stars0 ratingsR For Marketing Research and Analytics Rating: 0 out of 5 stars0 ratingsSingular Spectrum Analysis with R Rating: 0 out of 5 stars0 ratingsAudit Analytics: Data Science for the Accounting Profession Rating: 0 out of 5 stars0 ratingsSound Analysis and Synthesis with R Rating: 0 out of 5 stars0 ratingsNumerical Ecology with R Rating: 0 out of 5 stars0 ratingsR for Marketing Research and Analytics Rating: 0 out of 5 stars0 ratingsElements of Copula Modeling with R Rating: 0 out of 5 stars0 ratingsRandom Forests with R Rating: 0 out of 5 stars0 ratingsBusiness Analytics for Managers Rating: 0 out of 5 stars0 ratingsStatistical Analysis of Network Data with R Rating: 2 out of 5 stars2/5
Related ebooks
Applied Spatial Data Analysis with R Rating: 3 out of 5 stars3/5Mastering C++: A Comprehensive Guide to Mastering Fundamentals and Crafting Data-Driven Debut Programs Rating: 0 out of 5 stars0 ratingsR Programming - a Comprehensive Guide: Software Rating: 0 out of 5 stars0 ratingsC++ Programming Language: Simple, Short, and Straightforward Way of Learning C++ Programming Rating: 4 out of 5 stars4/5Advanced R Statistical Programming and Data Models: Analysis, Machine Learning, and Visualization Rating: 0 out of 5 stars0 ratingsCommon LISP: A Gentle Introduction to Symbolic Computation Rating: 4 out of 5 stars4/5Learn R Programming in 24 Hours Rating: 0 out of 5 stars0 ratingsPractical Enterprise React: Become an Effective React Developer in Your Team Rating: 0 out of 5 stars0 ratingsTeach Yourself C++ Rating: 4 out of 5 stars4/5Learn R By Coding Rating: 0 out of 5 stars0 ratingsUpdate to Modern C++ Rating: 0 out of 5 stars0 ratingsC++17 Standard Library Quick Reference: A Pocket Guide to Data Structures, Algorithms, and Functions Rating: 0 out of 5 stars0 ratingsC# Functional: Monads from Zero to Hero Rating: 0 out of 5 stars0 ratingsBeginning R: The Statistical Programming Language Rating: 5 out of 5 stars5/5C Clearly - Programming With C In Linux and On Raspberry Pi Rating: 0 out of 5 stars0 ratingsPro MERN Stack: Full Stack Web App Development with Mongo, Express, React, and Node Rating: 0 out of 5 stars0 ratingsC Programming Language, A Step By Step Beginner's Guide To Learn C Programming In 7 Days. Rating: 4 out of 5 stars4/5Rust for C++ Programmers: Learn how to embed Rust in C/C++ with ease (English Edition) Rating: 0 out of 5 stars0 ratingsC + +: C++ programming Rating: 0 out of 5 stars0 ratingsARDUINO CODING: A Comprehensive Guide to Arduino Programming (2024 Crash Course) Rating: 0 out of 5 stars0 ratingsARDUINO DETECTION: Harnessing Arduino for Sensing and Detection Applications (2024 Guide) Rating: 0 out of 5 stars0 ratingsQ Tips: Fast, Scalable, and Maintainable Kdb+ Rating: 0 out of 5 stars0 ratingsA++ The Smallest Programming Language in the World: An Educational Programming Language Rating: 0 out of 5 stars0 ratingsTCP/IP Sockets in C: Practical Guide for Programmers Rating: 4 out of 5 stars4/5Mastering Java: A Comprehensive Guide to Development Tools and Techniques Rating: 0 out of 5 stars0 ratingsC Programming Language Rating: 4 out of 5 stars4/5An Introduction to R: Data Analysis and Visualization Rating: 0 out of 5 stars0 ratingsLabVIEW – More LCOD Rating: 0 out of 5 stars0 ratings
Applications & Software For You
iPhone Photography For Dummies Rating: 0 out of 5 stars0 ratingsThe Best Hacking Tricks for Beginners Rating: 4 out of 5 stars4/5Blender 3D Basics Beginner's Guide Second Edition Rating: 5 out of 5 stars5/5Adobe Photoshop: A Complete Course and Compendium of Features Rating: 5 out of 5 stars5/5Adobe Illustrator: A Complete Course and Compendium of Features Rating: 0 out of 5 stars0 ratingsLogic Pro X For Dummies Rating: 0 out of 5 stars0 ratingsMastering ChatGPT Rating: 0 out of 5 stars0 ratingsAdobe Premiere Pro: A Complete Course and Compendium of Features Rating: 0 out of 5 stars0 ratingsAffinity Photo How To Rating: 0 out of 5 stars0 ratingsHow to Create Cpn Numbers the Right way: A Step by Step Guide to Creating cpn Numbers Legally Rating: 4 out of 5 stars4/52022 Adobe® Premiere Pro Guide For Filmmakers and YouTubers Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Kodi User Manual: Watch Unlimited Movies & TV shows for free on Your PC, Mac or Android Devices Rating: 0 out of 5 stars0 ratingsiPhone Photography: A Ridiculously Simple Guide To Taking Photos With Your iPhone Rating: 0 out of 5 stars0 ratingsYouTube Channels For Dummies Rating: 3 out of 5 stars3/5Sound Design for Filmmakers: Film School Sound Rating: 5 out of 5 stars5/5FL Studio Cookbook Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Canon EOS Rebel T3/1100D For Dummies Rating: 5 out of 5 stars5/5Hilarious Jokes for Minecrafters: Mobs, Creepers, Skeletons, and More Rating: 1 out of 5 stars1/5iPhone X Hacks, Tips and Tricks: Discover 101 Awesome Tips and Tricks for iPhone XS, XS Max and iPhone X Rating: 3 out of 5 stars3/5Adobe InDesign CC: A Complete Course and Compendium of Features Rating: 0 out of 5 stars0 ratingsGarageBand Basics: The Complete Guide to GarageBand: Music Rating: 0 out of 5 stars0 ratingsSix Figure Blogging In 3 Months Rating: 4 out of 5 stars4/5GarageBand For Dummies Rating: 5 out of 5 stars5/5Vocal Rescue: Rediscover the Beauty, Power and Freedom in Your Singing Rating: 4 out of 5 stars4/5How Do I Do That In InDesign? Rating: 5 out of 5 stars5/5Mastering QuickBooks 2020: The ultimate guide to bookkeeping and QuickBooks Online Rating: 0 out of 5 stars0 ratings
Reviews for Seamless R and C++ Integration with Rcpp
0 ratings0 reviews
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.gifFig. 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