Rcpp包提供C++类方便C or C++代码与R软件包进行交互,使用R中提供的.Call()
调用界面。 Rcpp提供R中的基础数据类型的C++类供访问。包作者可以保持R的数据结构而无需与C++进行不断的转换。同时,这些数据结构提供C++级别的存取。数据类型可以双向映射。可以从 R中把数据赋给C++, 返回数据从C++到R也完全一样。下面列出支持的数据类型。
R 数据类型 (SEXP) 与C++对象是一致的,按照类的衍生关系。所有的R类型都支持 (vectors, functions, environment, etc ...) 并且每一种对对应到C++的类对象。例如, numeric vectors代表类Rcpp::NumericVector的实例, environments代表Rcpp::Environment, functions代表Rcpp::Function,等等... 相应的 C++库都提供Rcpp::wrap函数,该函数是一个模版函数负责把数据转换为SEXP。
这个机制让使用标准C++类型实现C++的逻辑变得非常直接,比如使用STL编程然后包装为SEXP返回到R中。内部的封装操作使用高级元编程技术,目前支持的数据类型包括:bool, int, double, size_t, Rbyte, Rcomplex, std::string, STL containers (e.g std::vector
) 中T可封装 , STL maps (e.g std::map
) 中T可封装, 支持的隐含转换的任意的类型到SEXP。反向转换(从R到C++)通过Rcpp::as函数模版进行。
0.7.1开始, 提供了命名空间Rcpp。包含主要类 RObject,其他类都是从中继承而来,处理跟环境 (ENVSXP)相关的操作 , "Language" 语言 (LANGSXP) 和模版 XPTr 操作外部指针.
0.7.2和后续版本扩展了这些特征,支持其他的 R 类型实现自动转换,更聪明地使用模版。
0.8.1加入的支持使用modules直接暴露C++代码给R。对应的 Rcpp-modules描述更多的细节。
0.8.3 加入 sugar: 表达式模版,允许像R中的紧凑矢量化的表达式但是以编译速度实现,详情查看 Rcpp-sugar。
0.8.6 特殊函数cherished for statistics: d/p/q/r-style for most relevant distribution, in a form that is very close to what we'd use in R.
0.8.7 加入ReferenceClasses支持 in R 2.12.0; 将S4-based ReferenceClasses in the OO-style of Java or C++ 到R语言中。 0.9.0分离legacy classic API 到 RcppClassic.。
0.10.0 带来 Rcpp attributes, enhanced modules support and more.。
0.11.0 带来简化的builds for packages using Rcpp,不再需要link。
0.7.0, Rcpp 包含修改的 'cfunction',从卓越的 'inline' package 而来,该包由Oleg Sklyar开发。这允许用户定义body of a C++ function 像 标准的R character vector -- which is passed to 'cfunction' along with a few other parameters. 该函数然后builds完整的C++ 源文件 --- 然后编译,链接和载入。这让Rcpp interface classes实现R到 C++非常容易 ---任何人都可以直接从R提示符操作而不需要Makefiles, configuration settings等等。
0.8.1, 扩展函数 'cxxfunction'被采用 (要求inline 0.3.5)。该函数使得在Rcpp中使用C++更容易。特殊地,这个强制使用 .Call interface, 加上Rcpp 名称空间, 并且设置exception forwarding。采用 macros BEGIN_RCPP 和 END_RCPP封闭user code。
更多的, 通过 cfunction (and cxxfunction), 我们甚至能call external libraries然后linked。
有几个例包含在包中; 其中一个已被发表到blog.
这个可以工作在Windows,如果有 'R tools' 和 R安装并能工作。查看R-on-Windows FAQ和其他文档。
0.10.0, Rcpp attributes完成,这个比 inline更为强大和易用 --- 查看Rcpp attributes获取细节信息。
在版本0.11.5,超过470 单元测试函数调用928个单元测试确保API兼容性。单元测试同时也是适用的的例子。 A vignette is auto-generated with the results of the unit tests.
Rcpp 提供主要的头文件 Rcpp.h
和 library,在安装包目录的 lib
中。在R中,计算目录位置通过system.file("lib", "Rcpp.h", package="Rcpp")
--但同时提供通过函数 Rcpp::RcppCxxFlags()
and Rcpp::RcppLdFlags()
。因此可以调用像下面的形式src/Makevars
(or src/Makevars.win
on Windows)
PKG_CXXFLAGS=`${R_HOME}/bin/Rscript -e "Rcpp:::CxxFlags()"` PKG_LIBS=`${R_HOME}/bin/Rscript -e "Rcpp:::LdFlags()"`
查看Rcpp-package
了解细节。并注意从0.8.0版本开始,'LinkingTo' 参数 can also be employed in packages using Rcpp。 This will let R determine the location of the header files and users only need to use Rcpp::RcppLdFlags()
(as detailed above) to point to the actual library, and this is clearly therecommended approach. Moreover, we added an entire vignette on how to use Rcpp in your package with a detailed discussion. Also note, the vignette forRcpp attributesdetail another approach.
The book Seamless R and C++ Integration with Rcpp (Springer, 2013) provides a thorough and complete documentation for Rcpp, along with many examples. More information isis available here. The book can be ordereddirectly from Springeras well as fromAmazonand other book sellers.
The Rcpp Gallery regroups over fifty contributed articles and examples for Rcpp. It is open for user contributions.
The RcppExamples package (on CRAN) provides a simple illustration of how to use Rcpp, and can also be used as a framework for deploying Rcpp. This package is however somewhat incomplete in terms of example, so please see below for examples provides by several dozen packages using Rcpp.
We now have Doxygen-generated documentation of all the classes inbrowseable and searchable htmland as apdf file. We no longer include the Doxygen-generated documentation in the source tarball as it simply too big. But we have zip archives of thehtml,latex, andman documentation.
Besides the doxygen-generated reference manual we also have these eight vignettes:
The Rcpp-introduction vignette provides a short overview of Rcpp and an introduction (and has also been published as Volume 40, Issue 8 of theJournal of Statistical Software),
the Rcpp-package vignette shows how to write your own package using Rcpp,
the Rcpp-FAQ vignette addresses several frequently asked questions,
Rcpp-modules vignette discusses how to expose C++ functions and modules with ease using an idea borrowed from Boost::Python,
the Rcpp-extending vignette details the steps needed to extend Rcpp with user-provided or third-party classes,
the Rcpp-sugar vignette provides an introduction to the Rcpp sugar features inspired by vectorised R code,
the Rcpp-attributes vignette introduces the attributes features for getting C++ into R with ease,
the Rcpp-quickref vignette provides a quick reference cheat sheet (but is still mostly incomplete),
the Rcpp-attributes vignette details the high-level syntax for declaring C++ functions as callable from R and shows how to automatically generate the code required to invoke them, and
the Rcpp-unitTests vignette contains a summary of the (by now over two hundred) units tests for Rcpp.
All vignettes are also installed with the package, and available at the CRAN page.
In late October 2010, the R intergrouplet at Google was kind enough to invite us for a talk on Rcpp. The resulting talk was recorded and is nowavailable on YouTube
A long and growing list of packages using Rcpp is provideon a separate page.
Rcpp was initially written by Dominick Samperi to ease contributions to theRQuantLibproject, 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). His project saw no public releases for the thirty-five months period from November 2006 to November 2009. As a user of Rcpp, I (Dirk) chose to adopt Rcpp during 2008, made a first release 0.6.0 in November 2008 and have made a number of new releases since -- see the ChangeLog for details. Rcpp is open for contributions and patches some of which have already been integrated.Romain Francoisjoined the effort just before the 0.7.0 release and brought along a lot of energy and new ideas. We now have amailing listfor discussions around Rcpp. If you have ideas or suggested changes, send an email there.
A local archive is available here and atCRAN; SVN access is provided at R-Forge.
Rcpp is licensed under the GNU GPL version 2 or later.