How to call a R function from C++ with passing the parameters

后端 未结 1 1566
情歌与酒
情歌与酒 2020-12-22 05:15

I am trying to call my R function from C++ program.

rtest = function(input ,output) {
  a <- input
  b <- output 
  outpath <- a+b
  print(a+b)
  re         


        
相关标签:
1条回答
  • 2020-12-22 06:04

    This question has multiple interpretations, which is why I did not attempt an answer before. Here Solutions for several possible interpretations:

    C++ function defined with Rcpp that is called from R and uses a user defined R function. This follows http://gallery.rcpp.org/articles/r-function-from-c++/:

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    NumericVector callFunction(NumericVector x, NumericVector y, Function f) {
      NumericVector res = f(x, y);
      return res;
    }
    
    /*** R
    set.seed(42)
    x <- rnorm(1e5)
    y <- rnorm(1e5)
    
    rtest <- function(x, y) {
      x + y
    }
    
    head(callFunction(x, y, rtest))
    head(x + y)
    */
    

    The R function rtest is defined in R and passed to the C++ function callFunction together with it's two arguments. Partial result from Rcpp::sourceCpp():

    > head(callFunction(x, y, rtest))
    [1]  0.95642325 -0.57197358 -1.45084989 -0.18220091  0.07592864  0.56367202
    
    > head(rtest(x, y))
    [1]  0.95642325 -0.57197358 -1.45084989 -0.18220091  0.07592864  0.56367202
    

    Calling the function in R and via C++ gives the same result.

    C++ program using RInside which calls an user defined R function on data present in C++. Here we have two possibilities: either transfer the data to R and call the function there or move the function to C++ and call the R function in C++ like above:

    #include <RInside.h>
    
    int main(int argc, char *argv[]) {
        // define two vectors in C++
        std::vector<double> x({1.23, 2.34, 3.45});
        std::vector<double> y({2.34, 3.45, 1.23});
        // start R
        RInside R(argc, argv);
        // define a function in R
        R.parseEvalQ("rtest <- function(x, y) {x + y}");
        // transfer the vectors to R
        R["x"] = x;
        R["y"] = y;
        // call the function in R and return the result
        std::vector<double> z = R.parseEval("rtest(x, y)");
        std::cout << z[0] << std::endl;
    
        // move R function to C++
        Rcpp::Function rtest((SEXP) R.parseEval("rtest"));
        // call the R function from C++
        z = Rcpp::as<std::vector<double> >(rtest(x, y));
        std::cout << z[0] << std::endl;
        exit(0);
    }
    

    In order to compile this, I am using the GNUmakefile that comes with the examples in RInside. Result:

    $ make -k run
    ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/local/lib/R/site-library/RInside/include -g -O2 -fdebug-prefix-map=/home/jranke/git/r-backports/stretch/r-base-3.5.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -Wno-ignored-attributes -Wall    call_function.cpp  -Wl,--export-dynamic -fopenmp -Wl,-z,relro -L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -licuuc -licui18n  -lblas -llapack  -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o call_function
    
    Running call_function:
    3.57
    3.57
    
    0 讨论(0)
提交回复
热议问题