DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Using R - Calling C Code with Rcpp

Using R - Calling C Code with Rcpp

Jonathan Callahan user avatar by
Jonathan Callahan
·
Feb. 04, 13 · Interview
Like (0)
Save
Tweet
Share
3.51K Views

Join the DZone community and get the full member experience.

Join For Free

In two previous posts we described how R can call C code with .C() and the more complex yet more robust option of calling C code with .Call().  Here we will describe how the Rcpp package can be used to greatly simplify your C code without forcing you to become expert in C++.

First off, kudos to Dirk Eddelbuettel and Romain François who’s tireless efforts to improve, promote and document Rcpp have produced one of CRAN’s most popular packages.  As of Rcpp version 0.9.15 there are 82 “Reverse depends” — other packages that utilize Rcpp.  There are also eight vignettes that describe the package in human terms.  Even more accessible are Dirk’s papers and presentations and Romain’s blog.  While we’re heaping praise, let’s not forget to mention Romain’s graph gallery.

Even after all that praise and all the available documentation we’re still left with the problem of where to start.  There are no vignettes targeted at the R user who wants to try out a couple of C routines but isn’t otherwise inclined to learn C++ and who doesn’t want to write an entire package — at least not yet.  A good place to review the motivation behind Rcpp and see some great example code would be Dirk’s 62 slide Rcpp: Seamless R and C++ Integration presentation or the longer, 146 slide Rcpp Tutorial Parts I and II.  Between those two presentations and the package documentation you really have access to all the information you need.  However, in the interest of continuing our “baby steps” theme, we will once again recreate our three “Hello World!” examples, this time using Rcpp.

Getting Set Up

The very first thing you will have to do is install the Rcpp package.  (See Installing Packages if this is unfamiliar.)  If you keep your version of R at the bleeding edge, you can do this by invoking R and telling it to just grab the latest package.

1
2
3
4
5
6
7
8
9
10
11
$ R --vanilla
R version 2.14.1 (2011-12-22)
Copyright (C) 2011 The R Foundation for Statistical Computing
...
> install.packages( repos=c('http://cran.fhcrc.org/'), pkgs=c('Rcpp') )
Installing package(s) into ‘/usr/lib/R/library’
(as ‘lib’ is unspecified)
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
 package ‘Rcpp’ is not available (for R version 2.14.1)

Oops!  Some of us are slightly behind the times.  Rather than upgrade my version of R today, I’ll instead take a look inside the Rcpp Old sources: archive and find a version that was released close to the (2011-12-22) date of my version of R.  Version 0.9.10 was released on 17-Feb-2012 and should be compatible.  We’ll install that version from the command line with the following:

1
2
3
4
5
6
7
8
9
10
11
$ wget http://cran.r-project.org/src/contrib/Archive/Rcpp/Rcpp_0.9.10.tar.gz
...
$ sudo R CMD INSTALL Rcpp_0.9.10.tar.gz
[sudo] password for jonathan:
* installing to library ‘/usr/lib/R/library’
* installing *source* package ‘Rcpp’ ...
...
** testing if installed package can be loaded
* DONE (Rcpp)
Making packages.html ... done

The last thing we need to do is let our compilers and linkers know where the new, Rcpp libraries are located (see slide 48 of the Tutorial).

1
2
$export PKG_CPPFLAGS=`Rscript-e"Rcpp:::CxxFlags()"`
$export PKG_CPPFLAGS=`Rscript-e"Rcpp:::LdFlags()"`

Yes, Whew! once again.  But now we are ready to write C code that looks more like C code and, once we embrace Rcpp’s syntactic sugar, perhaps even like R code.

Baby steps example

When using the Rcpp package we are still using the .Call() interface to C code.  All of the changes will be seen in the C code which now becomes C++ code with a .cpp extension.  Because C++ is a superset of C, this is perfectly legal but we will need to educate Rcpp by using RcppExport.  According to slide 136 of the Tutorial:

1
2
3
4
5
* note : RcppExport is an alias to ‘extern "C"‘ defined by Rcpp.
*
* It gives C calling convention to the [...] function so that
* it can be called from .Call in R. Otherwise, the C++ compiler mangles the
* name of the function and .Call can’t find it.

OK.  Here we go with our first  C++ example: helloA2.cpp.

1
2
3
4
5
#include <Rcpp.h>
RcppExport SEXP helloA2(){
 printf("Hello World!\n");
 return(R_NilValue);
}

It looks remarkably similar to helloA1.c from the previous post and is compiled and invoked in much the same way.

1
2
3
4
5
6
7
RCMD SHLIB helloA2.cpp
g++-m32-I/usr/include/R-L/usr/lib/R/library/Rcpp/lib-lRcpp-Wl,-rpath,/usr/lib/R/library/Rcpp/lib
-I/usr/local/include -I/usr/lib/R/library/Rcpp/include-fpic -O2-g-pipe-Wall
-Wp,-D_FORTIFY_SOURCE=2-fexceptions-fstack-protector--param=ssp-buffer-size=4-m32-march=i686
-mtune=atom-fasynchronous-unwind-tables-chelloA2.cpp-ohelloA2.o
g++-m32-shared-L/usr/local/lib-ohelloA2.so helloA2.o-L/usr/lib/R/library/Rcpp/lib-lRcpp
-Wl,-rpath,/usr/lib/R/library/Rcpp/lib-L/usr/lib/R/lib-lR

The R wrapper code is identical to the one in the previous post for helloA1.c as is the usage in an R session.

1
2
3
4
5
# call RCpp C code
dyn.load("helloA2.so")
helloA2<-function(){
 result<-.Call("helloA2")
}
1
2
3
4
5
>source('wrappers.R')
>greeting<-helloA2()
Hello World!
>class(greeting)
[1]"NULL"

Simpler C code

We’ll leave out the wrappers and R session in the next two examples as they are identical to the examples in the previous post.  But just look at how much simpler the C code gets!  Instead of allocating memory, protecting from garbage collection and all that casting between types we get this for helloB2.cpp:

1
2
3
4
5
6
#include <Rcpp.h>
RcppExport SEXP helloB2(){
 Rcpp::StringVector result(1);
 result[0]="Hello World!";
 return(result);
}

and this for helloC2.cpp:

1
2
3
4
5
6
7
8
9
#include <Rcpp.h>
RcppExport SEXP helloC2(SEXP greetingPointer){
 Rcpp::StringVector greeting(greetingPointer);
 Rcpp::NumericVector result(greeting.size());
 for(inti=0;i<greeting.size();i++){
 result[i]=strlen(greeting[i]);
 }
 return(result);
}

Now we’re talkin’.  This is starting to look like code a C programmer, Heck, even a Java programmer could get comfortable with.  The tools provided by Rcpp are systematic, readable and, as a welcome change, very well documented.  Whereas I was hesitant to recommend writing C code for the .Call() interface because of the painful learning curve, I am happy to report that the learning curve for using Rcpp does not require that you first climb a mountain to learn C++.  C programmers of all skill levels will benefit from using Rcpp.

More Examples

While a “Hello World!” example may be a great place to start, it is unlikely to provide any useful template code for people.  For that you will want to poke around in the source code of the many packages that depend on Rcpp.  Dirk Eddelbuettel is of course quite interested in these dependent packages and describes some of them on slide 42 ofSeamless R and C++ Integration and slide 29 of Rcpp Tutorial Parts I and II.  I was pleased to learn that some of these packages are written in C and will hopefully provide excellent example code.

Hadley Wickham has written a comprehensive tutorial for the Rcpp package.

To everyone who is trying to improve and extend R — Best of Luck!

R (programming language)

Published at DZone with permission of Jonathan Callahan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Memory Debugging: A Deep Level of Insight
  • Apache Kafka vs. Memphis.dev
  • Explainer: Building High Performing Data Product Platform
  • Debugging Threads and Asynchronous Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: