R语言的cpp扩展支持Rcpp模块介绍

概述

Rcpp包提供C++类方便C or C++代码与R软件包进行交互,使用R中提供的.Call() 调用界面。 Rcpp提供R中的基础数据类型的C++类供访问。包作者可以保持R的数据结构而无需与C++进行不断的转换。同时,这些数据结构提供C++级别的存取。数据类型可以双向映射。可以从 R中把数据赋给C++, 返回数据从C++到R也完全一样。下面列出支持的数据类型。

Transfer from R to C++, and from C++ to R

R 数据类型 (SEXP) 与C++对象是一致的,按照类的衍生关系。所有的R类型都支持 (vectors, functions, environment, etc ...) 并且每一种对对应到C++的类对象。例如, numeric vectors代表类Rcpp::NumericVector的实例, environments代表Rcpp::Environment, functions代表Rcpp::Function,等等... 相应的 C++库都提供Rcpp::wrap函数,该函数是一个模版函数负责把数据转换为SEXP。

This makes it straightforward to implement C++ logic in terms of standard C++ types such as STL containers and then wrap them when they need to be returned to R. Internally, wrap uses advanced template meta programming techniques and currently supports these data types: primitive types (bool, int, double, size_t, Rbyte, Rcomplex, std::string), STL containers (e.g std::vector) where T is wrappable, STL maps (e.g std::map) where T is wrappable, and arbitrary types that support implicit conversion to SEXP. The reverse conversion (from R into C++) is performed by the Rcpp::as function template offering a similar degree of flexibility.

New features

Starting with release 0.7.1, a namespace Rcpp is provided. It contains a main class RObject as well as other classes that derive from RObject to deal with environments (ENVSXP) , "Language" for calls (LANGSXP) and the template XPTr for external pointers. Releases 0.7.2 and later extend this to a number of additional R types along with a number of facilities for automatic conversion thanks to clever use of templates. Release 0.8.1 adds support for exposing code in C++ directly to R using modules. The corresponding Rcpp-modulesvignette has more details. Release 0.8.3 adds sugar: expression templates that allow compact vectorised expression just like in R but at compiled speed; see the Rcpp-sugarvignette. Release 0.8.6 adds special functions 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. Release 0.8.7 adds support for ReferenceClasses in R 2.12.0; this now brings S4-based ReferenceClasses in the OO-style of Java or C++ to the R language. Release 0.9.0 splits support for the legacy classic API into its own package RcppClassic. Release 0.10.0 brings Rcpp attributes, enhanced modules support and more. Release 0.11.0 brings simplified builds for packages using Rcpp which no longer need to link against it.

Inline use

As of version 0.7.0, Rcpp also contains a modified function ‘cfunction‘ taken from the excellent ‘inline‘ package by Oleg Sklyar. This allows the user to define the body of a C++ function as a standard R character vector -- which is passed to ‘cfunction‘ along with a few other parameters. The function then builds a complete C++ source file containing a function with the given body --- and then compiles, links and loads it for us. Together with the Rcpp interface classes this makes for very easy use of C++ from R --- as everything can be done from the R prompt without any need for Makefiles, configuration settings etc pp.

As of version 0.8.1, an extended function ‘cxxfunction‘ is used (which requiers inline 0.3.5). This function makes it easier to use C++ code with Rcpp. In particular, it enforces use of the .Call interface, adds the Rcpp amespace, and sets up exception forwarding. It employs the macros BEGIN_RCPP and END_RCPP macros to enclose the user code

Moreover, with cfunction (and cxxfunction), we can even call external libraries and have them linked as well.

Several examples of this are included with the packages; one has also been posted on myblog.

This even works on Windows if you have the working ‘R tools‘ installed along with R. See the R-on-Windows FAQ and additional documentation.

With version 0.10.0, this has been complemented by Rcpp attributeswhich is even easier and more powerful than inline --- see the correspondingRcpp attributesvignette for details.

Unit testing

As of version 0.11.5, about 928 unit tests called from over 470 unit test functions are included in the package to ensure that no regressions are introduced in terms of API compatibility. The unit tests also serve as a (arguably somewhat raw) form of examples for usage. A vignette is auto-generated with the results of the unit tests.

Usage for package building

Rcpp provides a main header file Rcpp.h and a library inside the installed package in the directory lib. From within R, you can compute the directory location viasystem.file("lib", "Rcpp.h", package="Rcpp")--but both are provided for your use via the functions Rcpp::RcppCxxFlags()and Rcpp::RcppLdFlags() functions. So we can just use the following as a file 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()"`

See the help page for Rcpp-package for details. Also note that starting with version 0.8.0, the ‘LinkingTo‘ argument 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.

Rcpp book

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.

Rcpp Gallery

The Rcpp Gallery regroups over fifty contributed articles and examples for Rcpp. It is open for user contributions.

Demo package

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.

Class documentation

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.

Other 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.

Google Tech Talk

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

Example usage

A long and growing list of packages using Rcpp is provideon a separate page.

History

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.

Download

A local archive is availablehere and atCRAN; SVN access is provided atR-Forge.

License

Rcpp is licensed under the GNU GPL version 2 or later.

时间: 2024-10-12 19:00:55

R语言的cpp扩展支持Rcpp模块介绍的相关文章

R语言与数据分析之六:时间序列简单介绍

今年在某服装企业蹲点了4个多月,之间很长一段时间在探索其现货和期货预测,时间序列也是做销售预测的首选,今天和小伙伴分享下时间序列的基本性质和如何用R来挖据时间序列的相关属性. 首先读入一个时间序列:从1946年1月到1959年12月的纽约每月出生人口数量(由牛顿最初收集)数据集可以从此链接下载(http://robjhyndman.com/tsdldata/data/nybirths.dat).我们将数据读入R,并且存储到一个时间序列对象中,输入以下代码: births<- scan("h

R+tmcn笔记︱tmcn包的基本内容以及李舰老师R语言大会展示内容摘录

tmcn包目前托管在在R-forge 上开发和发布.下载方式以及Rforge相关链接: install.packages("tmcn", repos = "http://R-Forge.R-project.org") tmcn 包是一个进行中文文本挖掘的R包.包含了中文编码处理.文字操作. 文本挖掘模型和算法的一些函数.主要针对目前可用的 R 资源的不足而开发的. 目前最常用的文本挖掘包是 tm 包,该包已经成了事实上的分析标准, 几乎所有的其他 R 包都是基于这个

R语言调用C++

R语言跨界调用C++ R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大. R语言作为统计学一门语言,一直在小众领域闪耀着光芒.直到大数据的爆发,R语言变成了一门炙手可热的数据分析的利器.随着越来越多的工程背景的人的加入,R语言的社区在迅速扩大成长.现在已不仅仅是统计领域,教育,银行,电商,互联网….都在使用R语言. 要成为有理想的极客,我们不能停留在语法上,要掌握牢固的数学,概率,统计知识,同时还要有创新精神,把R语言发挥到各个领域.让我们

在 SPSS Statistics 和 Modeler 中调用 R 语言的实现和应用

http://www.ibm.com/developerworks/cn/data/library/ba/ba-1401spss-r/index.html 登录 | 注册   IBM 技术主题 软件下载 社区 技术讲座 搜索 developerWorks 打印本页面 用电子邮件发送本页面 新浪微博 人人网 腾讯微博 搜狐微博 网易微博 Digg Facebook Twitter Delicious Linked In developerWorks 中国 技术主题 Information Mana

大数据时代的精准数据挖掘——使用R语言

老师简介: Gino老师,即将步入不惑之年,早年获得名校数学与应用数学专业学士和统计学专业硕士,有海外学习和工作的经历,近二十年来一直进行着数据分析的理论和实践,数学.统计和计算机功底强悍. 曾在某一世界500强公司核心部门担任高级主管负责数据建模和分析工作,在实践中攻克统计建模和数据分析难题无数,数据处理与分析科学精准,在实际应用中取得良好的效果. Gino老师担任数据分析培训师多年,探索出一套以实例讲解带动统计原理理解和软件操作熟悉的方法,授课的学生能迅速理解统计原理并使用统计软件独立开展数

R语言快速入门--从最基本的功能讲起

最近因为一些特殊的原因,开始接触R语言.越学越发现R语言的强大,原来完成一件事情是如此的简单.R语言中提供的工具和方法极大的方便了对一些数据的分析和处理的工作.对于一个要立志从事数据分析或者数据挖掘的学生或者工程师来说,学习R语言,确实可以达到如虎添翼的效果. R语言有如下特点: 1. 有效的数据处理和保存机制: 2. 拥有一整套的数组和矩阵操作运算符: 3. 一系列连续而又完整的数据分析工具: 4. 图形统计可以直接对数据进行分析和显示,可用于多种图形设备: 5. R语言是一种相当完善.简洁和

哪怕你不认可,我还是要为R语言正名

有些业界从业人士对R语言的价值并不认可,他们认为R语言只针对统计分析.R语言的确提供了很全面的统计分析的软件包,比如CRAN,Bioconductor,Neuroconductor,以及ROpenSci;并且提供了优秀的包管理功能. 但加米谷教育请添加链接描述告诉你在与计算机领域朋友的沟通中得知,R语言其实已经成长为一种多功能的编程语言,它的功能远不限于数据分析而已.但是,R语言的很多优秀特性并不为R语言社区以外的人所熟知. 在本文中,我将给大家介绍那些不为人知,却又好用到难以置信的R语言功能.

R语言解读多元线性回归模型

转载:http://blog.fens.me/r-multi-linear-regression/ 前言 本文接上一篇R语言解读一元线性回归模型.在许多生活和工作的实际问题中,影响因变量的因素可能不止一个,比如对于知识水平越高的人,收入水平也越高,这样的一个结论.这其中可能包括了因为更好的家庭条件,所以有了更好的教育:因为在一线城市发展,所以有了更好的工作机会:所处的行业赶上了大的经济上行周期等.要想解读这些规律,是复杂的.多维度的,多元回归分析方法更适合解读生活的规律. 由于本文为非统计的专业

R语言解读一元线性回归模型

前言 在我们的日常生活中,存在大量的具有相关性的事件,比如大气压和海拔高度,海拔越高大气压强越小:人的身高和体重,普遍来看越高的人体重也越重.还有一些可能存在相关性的事件,比如知识水平越高的人,收入水平越高:市场化的国家经济越好,则货币越强势,反而全球经济危机,黄金等避险资产越走强. 如果我们要研究这些事件,找到不同变量之间的关系,我们就会用到回归分析.一元线性回归分析是处理两个变量之间关系的最简单模型,是两个变量之间的线性相关关系.让我们一起发现生活中的规律吧. 由于本文为非统计的专业文章,所