Calling R functions from C++ - c++

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.

Related

CPLEX equivalent C++ of cplexmiqp MATLAB

I have a matlab script, which uses Cplex, to implement in C++. Does the C++ equivalent of the toolbox function cplexmiqp exist ?
If not, how can I solve my problem? I only have the parameters needed to this function to solve the MIP problem (H, f, Aineq, bineq, Aeq, beq, sostype, sosind, soswt, lb, ub, ctype)
cplexmiqp is a convenience function in the CPLEX MATLAB Toolbox API. There is nothing exactly equivalent to this in the C++ API. However, the C++ API is actually more powerful/flexible and you can definitely accomplish the same task. You'll have to build the model using your input data, solve it, and query the solution using methods in the C++ API. I'd suggest that you take a close look at the ilomiqpex1.cpp example that is shipped with CPLEX.

List of Rcpp sugar functions?

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/

time series "forecast" R package from C++

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.

Porting existing C++ code to R

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.

Matrices/Vectors in C++ with the R math standalone library?

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.