Porting existing C++ code to R

5

3

I would like to port the SpecialK Poker Hand evaluator to R. I think this should be relatively easy using the Rcpp package, but I have no idea where to begin.

The existing tutorials seem to focus on developing new C++ code for use in R. In this case, I have C++ that solves a problem, and I want to use this code in R with minimum hassle. If the code were one, self-contained function, I could compile and link it on the fly with inline, but this doesn't work here.

I guess this question has 2 parts:

  1. Will I need to make any changes to the C++ source to make it compatible with Rcpp?
  2. How do I call this code from R, given that it's not a small, self-contained function I can compile and link dynamically using inline?

I am also open to using the Java, python, or objective-C versions of the evaluator, but I don't think those will be easier to link to R.

Zach

Posted 2011-12-01T21:25:35.030

Reputation: 13 915

Can I ask what intention U have in changing code from C++ to R? – Hauleth – 2011-12-01T21:34:20.943

@Hauleth I would like to call the C++ code from R. In this case, I want to use R's statistics functions to do some basic poker research, but I need a hand evaluator. No R packages exist for evaluating poker hands, so this seemed like the best option. – Zach – 2011-12-01T21:38:49.100

U've got a point. – Hauleth – 2011-12-01T21:48:09.370

Answers

4

Have you looked at Rcpp which makes it fairly painfree to combine R and C++?

Lots of packages use it to bring existing C++ code to R. You can also look at questions here under the [rcpp] tag. Fairly extensive documentation in the package, at my site and other places.

Dirk Eddelbuettel

Posted 2011-12-01T21:25:35.030

Reputation: 273 788

Hi Dirk: Where's the best tutorial for someone brand new to C++ who wants to port some existing code? – Zach – 2011-12-01T22:14:28.910

That is not easy. To access C++ code, you need to know some C++. But we have literally dozens of example using inline in various places, you could start with those. And/or looking at existing 'similar' CRAN packages you could try to learn from (when linking to existing libs). And of course the rcpp-devel list and its archives. – Dirk Eddelbuettel – 2011-12-01T22:21:14.023

Thanks. I was hoping there would be an easy solution, but suspected there wasn't. – Zach – 2011-12-02T15:07:43.930

It depends on your definition of easy. Compared to what you have to do without Rcpp: really easy. Is it automatic? No. Do you get by without reading docs? No. There are over 40 packages on CRAN that use Rcpp. Pick one or two which use an external library and see how they do it. Read our docs. Ask on rcpp-devel. – Dirk Eddelbuettel – 2011-12-02T15:25:48.013

You bet there is. – Dirk Eddelbuettel – 2011-12-03T15:08:00.763

@Dirk Hi, which R package do you suggest to look into the source that ports existing C++ library to R? I mean in terms of simplicity. Its exciting!! – Suman Khanal – 2018-01-02T14:25:02.767