我在哪里可以学习如何编写 C 代码来加速慢速 R 函数? [英] Where can I learn how to write C code to speed up slow R functions?

查看:23
本文介绍了我在哪里可以学习如何编写 C 代码来加速慢速 R 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习如何编写用于 R 的 C 代码的最佳资源是什么?我了解系统和外语界面 R 扩展部分,但我觉得这很难.什么是编写用于 R 的 C 代码的好资源(在线和离线)?

What's the best resource for learning how to write C code for use with R? I know about the system and foreign language interfaces section of R extensions, but I find it pretty hard going. What are good resources (both online and offline) for writing C code for use with R?

澄清一下,我不想学习如何编写 C 代码,我想学习如何更好地集成 R 和 C.例如,我如何从 C 整数向量转换为 R 整数向量(或反之亦然)还是从 C 标量到 R 向量?

To clarify, I don't want to learn how to write C code, I want to learn how to better integrate R and C. For example, how do I convert from a C integer vector to a R integer vector (or vice versa) or from a C scalar to an R vector?

推荐答案

好吧使用源代码,Luke! --- R 本身有很多(非常有效的)C 代码一个可以学习,CRAN 有数百个软件包,其中一些来自您信任的作者.这提供了真实的、经过测试的例子来研究和适应.

Well there is the good old Use the source, Luke! --- R itself has plenty of (very efficient) C code one can study, and CRAN has hundreds of packages, some from authors you trust. That provides real, tested examples to study and adapt.

但正如 Josh 怀疑的那样,我更倾向于 C++,因此 Rcpp.它还有很多例子.

But as Josh suspected, I lean more towards C++ and hence Rcpp. It also has plenty of examples.

我觉得有两本书很有帮助:

There were two books I found helpful:

  • 第一个是 Venables 和 Ripley 的S 编程",尽管它已经很长了(并且多年来一直有传言说第二版).那时没有别的.
  • Chambers 的数据分析软件"中的第二个,它是更新的,并且具有更好的以 R 为中心的感觉——以及关于扩展 R 的两章.C 和 C++ 都被提及.另外,John 为我对 digest 所做的事情粉碎了我,因此仅此一项就值得入场.
  • The first one is Venables and Ripley's "S Programming" even though it is getting long in the tooth (and there have been rumours of a 2nd edition for years). At the time there was simply nothing else.
  • The second in Chambers' "Software for Data Analysis" which is much more recent and has a much nicer R-centric feel -- and two chapters on extending R. Both C and C++ get mentioned. Plus, John shreds me for what I did with digest so that alone is worth the price of admission.

也就是说,John 越来越喜欢 Rcpp(并做出贡献),因为他发现R 对象和 C++ 对象之间的匹配(通过 Rcpp)非常自然——并且参考类在这方面有所帮助.

That said, John is growing fond of Rcpp (and contributing) as he finds the match between R objects and C++ objects (via Rcpp) to be very natural -- and ReferenceClasses help there.

编辑 2: 有了 Hadley 重新聚焦的问题,我非常强烈敦促您考虑 C++.你必须用 C 做很多样板式的废话---非常乏味而且非常可以避免.查看Rcpp-introduction 小插图.另一个简单的例子是这篇博文,我展示了这一点,而不是担心10% 的差异(在 Radford Neal 示例中)我们可以使用 C++ 获得 80 倍的增加(当然这是一个人为的示例).

Edit 2: With Hadley's refocussed question, I very strongly urge you to consider C++. There is so much boilerplate nonsense you have to do with C---very tedious and very avoidable. Have a look at the Rcpp-introduction vignette. Another simple example is this blog post where I show that instead of worrying about 10% differences (in one of the Radford Neal examples) we can get eightyfold increases with C++ (on what is of course a contrived example).

编辑 3: 复杂性在于您可能会遇到 C++ 错误,坦率地说,这些错误很难理解.但是如果只是使用 Rcpp 而不是扩展它,你应该几乎不需要它.虽然这个成本是不可否认的,但它的好处远远超过了更简单的代码、更少的样板、没有保护/不保护、没有内存管理等 pp. Doug Bates 就在昨天表示他发现 C++ 和 Rcpp 更像是编写 R 而不是编写 C++.YMMV等等.

Edit 3: There is complexity in that you may run into C++ errors that are, to put it mildly, hard to grok. But to just use Rcpp rather than to extend it, you should hardly ever need it. And while this cost is undeniable, it is far eclipsed by the benefit of simpler code, less boilerplate, no PROTECT/UNPROTECT, no memory management etc pp. Doug Bates just yesterday stated that he finds C++ and Rcpp to be much more like writing R than writing C++. YMMV and all that.

这篇关于我在哪里可以学习如何编写 C 代码来加速慢速 R 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆