I'm just getting started with Rcpp and wondering if somewhere out there a list of Rcpp sugar functions exists. In the process of translating some of my slow code to C/C++ I'll need functionality provided by base R functions like match, tabulate, and which.
According to Hadley's Advanced R book match is implemented and the Rcpp-sugar vignette lists a few more available functions though it doesn't seem comprehensive.
What I'm really wondering is: is there a way to find, in the documentation of the package, or elsewhere, what sweet-sweet-sweet functions are available and what I'll have to write for myself? With any other R package I'd go directly to R> help(package = "Rcpp") but that doesn't seem to be much help in this case.
As luck would have it, I started to document the Rcpp API in a more accessible manner than what is offered by the doxygen documentation. This documentation includes the Rcpp sugar set of functions alongside usable examples. I'm hopeful that Rcpp should ship with this documentation in the 0.13.0.
In the interim, you can view the source and contribute here:
https://github.com/coatless/rcpp-api/blob/master/rcpp-api-docs.Rmd
Or view a rendered version (that is lagged) here:
http://thecoatlessprofessor.com/programming/rcpp/unofficial-rcpp-api-docs/
Related
I am new to C++, with some training using fortran95. Trying to convert my knowledge into the new syntax but have run into a snag.
Many of my programs use modules with subroutines, and subroutines within subroutines and use of functions from a library described by NAG.com which are readily available and searchable.
I am currently looking for a c++ version of
http://www.nag.com/numeric/FL/manual/pdf/F08/f08naf.pdf
From what I have read so far, these libraries exist for c++ and I have used some simple ones thus far(like vector, cmath, math.h) but only ones that are already included in my Xcode package for my mac.
I haven't seen anybody mention one of these which my be included with my Xcode, and I am lacking in how to implement outside libraries I find. I am interested particularly in using:
http://www.alglib.net/download.php
Thus far I have been using subrutines as void type functions and simply including them in all of my code. But my code is becoming exceedingly cumbersome and I would like to make something similar to a fortran module to do chebyshev calculations. And I would much rather find a good library of eignevalue calculators and maybe even chebyshev calculators, . . . which I can use.
Essentially my question is, how do I implement external libraries I find and does anybody have a recommendation for a good one? How can I make my own code which contains a callable set of functions and then call it from within another piece of code?
If I understand right, the part of the NAG library you're using in Fortran is basically LAPACK. There is a C interface to LAPACK, called LAPACKE (http://www.netlib.org/lapack/lapacke.html). You can use it in a C++ program.
I didn't understand the other parts of your question.
I want to perform multinomial logistic regression [using glm()] and do some graphs [gplot()] in C++.
I'm not able to write a function for this kind of regression in C++, but maybe is possible to open R and call glm() function in order to performe it inside of C++, is it? how? I would appreciate any idea.
Thanks in advance!
Yes you can via the RInside project which provides you with a (very simple) class R which instantiates the R interpreter. You can then pass commands as (possibly multi-line) strings and evaluate via the REPL.
There are numerous examples in the package itself, and on different blog posts. As it uses Rcpp extensively, it is also covered in one chapter of the Rcpp book.
I can't find any documentation about how to use the "forecast" R package from C++. Is anything available online?
I have not looked closely but as it depends on the CRAN packages Rcpp and RcppArmadillo (which I am involved and which facilitate C++ use from R), I'd say yes. You can probably poke around in the sources and find other entry points. It's open source, after all.
But if you asked for a well-defined pre-existing API for forecast, the answer may be no.
Edit: Oh, and the obvious plug for RInside which allows you to embed [a single instance of] R very easily inside your C++ application. It does this by wrapping the existing (but hard to use) embedding API in a much simpler to use C++ class which abstracts away all the handholding.
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:
Will I need to make any changes to the C++ source to make it compatible with Rcpp?
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.
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.
All,
I have been playing with the R math standalone library in C++. I quite like being able to generate random numbers and use distribution functions that I am comfortable with from R. My question is: is it possible to use the matrix operations (multiplication, transpose, inverse, Chol, etc) that are available in R in a standalone library? I don't see them in Rmath.h.
If matrix operations are available to use in standalone C++ code, it seems that the R API becomes the perfect open source computational engine. Are people using it in this fashion?
Thanks so much for your guidance and suggestions!
Most, if not all, of the things you mention are provided by LAPACK or BLAS Fortran code that R links to, not something R provides new code for itself.
If you are interested in using C++ with R, look at the Rcpp package by Dirk Eddelbuettel and Romain Francois. Dirk has also written the RcppArmadillo package as an interface to the Armadillo C++ linear algebra library that can do the matrix operations you mention.
Whether this is of use will depend on whether you are wanting to write separate C++ code that is accessed outside R, or interfacing C++ code that you access within R. Rcpp facilitates (greatly) the latter. Take a look at Armadillo directly if it is the former situation.
I think the original questions starts at the wrong end. There is no C++ in R, and no C++ API in R itself.
So if you want to access R matrix functions, you have to go through the R API -- which is a C layer and very much not C++.
Gavin in his answer (and comments) and Josh are spot-on: You probably want something like Armadillo for high-level matrix algebra. Armadillo (just like related libraries) eventually goes to BLAS and LAPACK --- as does R. [ Doug Bates usually reminds us that there is one important difference related to pivoted decompositions; this is touched upon in the fastLm() implementation and example in RcppArmadillo. ]
Lastly, Rcpp can help with both cases:
whether you want to extend R by calling C++ code you wrote, where Rcpp makes it easy to pass objects back and forth, or
whether you want to embed R inside your C++ application using RInside as Rcpp once again provides the glue
The rcpp-devel list is a good place for more in-depth discussions and examples.