5

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
  • 29,791
  • 35
  • 142
  • 201
  • Can I ask what intention U have in changing code from C++ to R? – Hauleth Dec 01 '11 at 21:34
  • @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 Dec 01 '11 at 21:38

1 Answers1

5

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
  • 360,940
  • 56
  • 644
  • 725
  • Hi Dirk: Where's the best tutorial for someone brand new to C++ who wants to port some existing code? – Zach Dec 01 '11 at 22:14
  • 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 Dec 01 '11 at 22:21
  • Thanks. I was hoping there would be an easy solution, but suspected there wasn't. – Zach Dec 02 '11 at 15:07
  • 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 Dec 02 '11 at 15:25
  • @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 Jan 02 '18 at 14:25