[Rd] call R function from C code

Dirk Eddelbuettel edd at debian.org
Fri May 10 13:27:26 CEST 2013

On 10 May 2013 at 13:34, Matwey V. Kornilov wrote:
| Thanks, It is what I was looking for. But now, I poorly understand 
| environment conception. My initial C function in invoked from R (in some 
| environment I suppose), how do I know this env, to provide it to eval()? 
| Or, may I just make a clean env?

Given that you seem a little fuzzy about all this, may I suggest a pivot over
to Rcpp? While it uses C++ features, you are not really forced to use C++.
Here is one really quick example:

  R> library(Rcpp)
  R> cppFunction('NumericVector callRonVec(NumericVector x, Function f) { NumericVector res = f(x); return(res); }')
  R> callRonVec( (1:100)^2, fivenum)
  [1]     1.0   650.5  2550.5  5700.5 10000.0
  R> fivenum( (1:100)^2 )   # obviously the same
  [1]     1.0   650.5  2550.5  5700.5 10000.0

All we are doing here is instantiating some variable.  You can access their
content and modify it C style if you so desire, or you can use the C++
features you like and ignore any others.  As you mentioned environment, you
can also pass down any R environment.

You can also combine this with other packages such as RcppArmadillo which
give you easy-to-use idioms often modeled after Matlab. One such example is
the solve() function at the C++ level.  Another quick example:

  R> X <- as.matrix(cbind(1:100, sqrt(1:100))); y <- rowSums(X) + rnorm(100)
  R> lm.fit(X,y)$coefficients       # fit in R as a baseline
        x1       x2 
  1.003357 0.981233 
  R> cppFunction('arma::vec mySolve(arma::mat x, arma::vec y) { return(solve(x,y)); }', depends="RcppArmadillo")
  R> mySolve(X, y)                  # no surprise, we get the same answer
  [1,] 1.003357
  [2,] 0.981233

This use the Armadillo matrix and vector types, and calls solve() for you --
which will dispatch to the same BLAS functions R uses, but gets there at no
extra effort for you (besides having to read up on (Rcpp)Armadillo).  As a
side benefit, these things tend to be faster in C++.

Hope this helps.  The introductory vignettes to Rcpp and RcppArmadillo are
also available as papers in JSS (in 2011) and CSDA (2013, in press), and
there is a fair bit of other documentation out there.

| 10.05.2013 00:13, Gabriel Becker пишет:
| > Matwey,
| >
| > There are a number of ways to do this, but it depends on what exactly you
| > want. Do you want to execute a call to an actual R function from within C,
| > or do you want to directly call one of R's internal C functions (which may
| > work but is not future-safe unless it is part of the official API).
| >
| > If its the first, see Martin Morgan's post here:
| > http://stackoverflow.com/questions/7457635/calling-r-function-from-c for
| > more detail about the approach I believe Simon is referring to. As for the
| > second, they are normal C functions, but I believe the consensus is that if
| > they aren't part of the API you are on your own figuring out how they work
| > (the source code is available for this of course).
| >
| > Hope that helps,
| > ~G
| ______________________________________________
| R-devel at r-project.org mailing list
| https://stat.ethz.ch/mailman/listinfo/r-devel

Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com

More information about the R-devel mailing list