Dirk Eddelbuettel Rcpp: An R / C++ interface
 

Overview

The Rcpp package provides C++ classes that greatly facilitate interfacing C or C++ code in R packages using the .Call() interface provided by R. Rcpp provides matching C++ classes for a large number of basic R data types. Hence, a package author can keep his data in normal R data structure without having to worry about translation or transfer to C++. At the same time, the data structures can be accessed as easily at the C++ level, and used in the normal manner. The mapping of data types works in both directions. It is as straightforward to pass data from R to C++, as it is it return data from C++ to R. The following two sections list supported data types.

Transfer from R to C++

Standard R datatypes that are understood in C++ are
  • named lists containing numeric (i.e. floating point), integer, character, logical (i.e. boolean) or Date and Datetime (i.e. POSIXct) arguments;
  • data frames containing numeric, integer, logical, character, Date, Datetime or Factor columns;
  • named vectors containing numeric or integer values,
  • vectors and matrices of different values
  • character strings

Transfer from C++ to R

Standard C++ datatypes can be returned to R in a named list, the most general data type in R. Permissible components of the returns list are the following C++ types:
  • double (scalar as well as vectors and vectors of vectors),
  • int (scalar as well as vectors and vectors of vectors),
  • string,
  • STL vector types and vector types of int and double
  • STL vector of strings
  • internal Rcpp types RcppDate, RcppDateVector, RcppDatetime, RcppDatetimeVector, RcppStringVector, RcppVector of int or double, RcppMatrix of int or double, RcppFrame

Usage for package building

Rcpp provides a header file and a library inside the installed package in the directory lib. From within R, you can compute the directory location via system.file("lib", "Rcpp.h", package="Rcpp")--but both are provided for your via the functions Rcpp::RcppCxxFlags() and Rcpp::RcppLdFlags(). So we can just use the following as a file src/Makevars (or src/Makevars.win on Windows)
PKG_CXXFLAGS=`Rscript -e 'cat(Rcpp:::RcppCxxFlags())'`

PKG_LIBS=`Rscript -e 'cat(Rcpp:::RcppLdFlags())'`
See the help page for Rcpp-package for details.

History

Rcpp was initially written by Dominick Samperi to ease contributions to the RQuantLib project, and then released as a project in its own right. During 2006, Dominick made several releases under the RCpp name (versions 1.0 to 1.4) before he changed the name to RCppTemplate and made more releases (1.5 to 5.2). The project saw no public releases during 2007 and then first ten months of 2008.

I have now adopted Rcpp and started to make new releases, starting with 0.6.0 which arrived on CRAN in early November 2008.

InFrequently Asked Questions

  1. What about using LinkingTo in the DESCRIPTION file?
    Great question, but nobody has explained yet how to do that for C++. For C, we do what lme4 and matrix do. But for C++? Let us know if you know how to do this.

ChangeLog

2008-12-02  Dirk Eddelbuettel  

	* Release 0.6.2
	
	* R/RcppLdPath.R: Correct output for the default Linux case

2008-11-30  Dirk Eddelbuettel  

	* Release 0.6.1

	* src/Makevars: Added, replacing src/Makefile: thanks to Simon for a
	rewrite that provides OS X support
	* src/Makefile: Retired
	* src/Makefile.win: Now build libRcpp.a instead of Rcpp.a

	* src/Rcpp.{cpp,h}: Removed #ifdef statements related to QuantLib and
	Date ops as we no longer need to split this off -- one build for all
	uses; also added another date offset for the difference between the
	Unix epoch of Jan 1, 1970 and what certain spreadsheets use (which is
	what QuantLib) follows.  With this change, Rcpp is completely
	decoupled from QuantLib headers, yet can be used from QuantLib 0.2.10
	or later. However, this also removes data conversion from R/Rcpp to
	QL as Rcpp no longer 'knows' QL.

	* R/RcppLdPath.R: Adds new functions RcppLdPath() to return lib/
	directory path, and RcppLdFlags() to return arguments, including the
	optional rpath settings on Linux

	* R/RcppExample.R: Added new example 'RcppVectorExample()', extended
	  default print method for examples

	* man/Rcpp-package.Rd: Expanded / updated documentation of package
	use when building other packages

	* cleanup: Removing static libraries too but do not remove src/Makevars

2008-11-05  Dirk Eddelbuettel  

	* Release 0.6.0

	* New maintainer

	* src/Makefile{,.win}: New files, including functionality from both
 	  configure and RcppSrc/Makefile; we now build two libraries, one for
	  use by the package which also runs the example, and one for users to
	  link against
	* src/Makevars.in: Removed
	* src/Rcpp.{cpp,h}: moved in from ../RcppSrc

	* src/Rcpp.{cpp,h}: Add new type RcppDatetime corresponding to
  	  POSIXct in with full support for microsecond time resolution
	  between R and C++

	* man/*: Several new manual pages added

	* configure{,.in,.win}: Removed as src/Makefile* can handle this more
	  easily

	* DESCRIPTION: Minor cleanup and reformatting, Date: now uses
  	  svn:keyword Date property

	* NAMESPACE: Renamed RcppTemplateVersion to RcppVersion,
	  deleted RcppDemo

	* demo/ directory removed as vignette("RcppAPI") is easier and more
	  reliable to show vignette documentation

	* R/zzz.R: RcppTemplateDemo() removed, vignette("RcppAPI") is easier
	* man/RcppTemplateDemo.Rd removed as well

	* R/RcppExample.R: reindented, rewritten to set default arguments

	* R/RcppTemplateVersion.R: renamed to RcppVersion.R
	* man/RcppTemplateVersion.Rd: renamed to RcppVersion.Rd

	* RcppSrc/Rcpp.{cpp,h}: moved to src/ directory
	* RcppSrc/Makefile{,.win}: removed

	* inst/doc/RcppAPI.{Rnw,pdf}: Added footnote onto titlepage about how
	  this document has not (yet) been updated along with the channges made

Download

A local archive is available here and at CRAN; SVN access is provided at R-Forge.

Last modified: Tue Dec 2 22:03:43 CST 2008