Rcpp包提供C++類方便C or C++代碼與R軟件包進行交互,使用R中提供的.Call()
調用界面。 Rcpp提供R中的基礎數據類型的C++類供訪問。包做者能夠保持R的數據結構而無需與C++進行不斷的轉換。同時,這些數據結構提供C++級別的存取。數據類型能夠雙向映射。能夠從 R中把數據賦給C++, 返回數據從C++到R也徹底同樣。下面列出支持的數據類型。
html
R 數據類型 (SEXP) 與C++對象是一致的,按照類的衍生關係。全部的R類型都支持 (vectors, functions, environment, etc ...) 而且每一種對對應到C++的類對象。例如, numeric vectors表明類Rcpp::NumericVector的實例, environments表明Rcpp::Environment, functions表明Rcpp::Function,等等... 相應的 C++庫都提供Rcpp::wrap函數,該函數是一個模版函數負責把數據轉換爲SEXP。web
這個機制讓使用標準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函數模版進行。spring
0.7.1開始, 提供了命名空間Rcpp。包含主要類 RObject,其餘類都是從中繼承而來,處理跟環境 (ENVSXP)相關的操做 , "Language" 語言 (LANGSXP) 和模版 XPTr 操做外部指針.
編程
0.7.2和後續版本擴展了這些特徵,支持其餘的 R 類型實現自動轉換,更聰明地使用模版。數據結構
0.8.1加入的支持使用modules直接暴露C++代碼給R。對應的 Rcpp-modules描述更多的細節。app
0.8.3 加入 sugar: 表達式模版,容許像R中的緊湊矢量化的表達式可是以編譯速度實現,詳情查看 Rcpp-sugar。less
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.
ide
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' 參數能夠在 Rcpp的包中使用。 這讓 R 能夠肯定頭文件的位置,而且用戶只須要使用Rcpp::RcppLdFlags()
(as detailed above) 指向實際的庫, 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.