本文介绍了如何通过C ++从C ++调用R函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从C ++程序调用R函数。

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)
  return(outpath)
}

这是我的R函数。我需要找到一种通过传递参数从C调用此函数的方法。 。在这里,我做了类似的方法来从python调用R。因此,我需要指定R脚本的路径和函数名称,还需要通过python传递参数。我正在C中寻找类似的方法。但是没有得到结果。这可能很简单。任何帮助表示赞赏。

This is my R function. I need to find a way for calling this function from C with passing arguments. calling a R function from python code with passing arguments. Here i have made similar method for calling R from python. So i need to specify the path to R script and the name of the function, and also able to pass the arguments through python. I am looking for similar way in C. But didn't get results. This may be a simple thing. Any help is appreciated.

推荐答案

这个问题有多种解释,这就是为什么我之前没有尝试答案。这里有几种可能的解释的解决方案:

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

使用Rcpp定义的C ++函数,该函数从R调用并使用用户定义的R函数。这遵循:

#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)
*/

R函数 rtest 在R中定义,并与两个参数一起传递给C ++函数 callFunction Rcpp :: sourceCpp()

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

通过R并通过C ++调用该函数会得到相同的结果。

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

使用RInside的C ++程序对存在于其中的数据调用用户定义的R函数在这里,我们有两种可能性:要么将数据传输到R并在其中调用函数,要么将函数移至C ++并在C ++中调用R函数,如上:

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);
}

为了进行编译,我使用了附带的示例中的://://github.com/eddelbuettel/rinside/blob/master/inst/examples/standard/GNUmakefile rel = nofollow noreferrer> GNUmakefile code>。结果:

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

这篇关于如何通过C ++从C ++调用R函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:57