简体   繁体   English

使用rcpp将R函数传递给C例程

[英]Passing R functions to C routines using rcpp

I have a C function from a down-stream library that I call in C like this 我有一个来自下游库的C函数,我在C中调用这样的函数

result = cfunction(input_function)

input_function is a callback that needs to have the following structure input_function是一个需要具有以下结构的回调

double input_function(const double &x)
{
  return(x*x);
}

Where x*x is a user-defined computation that is usually much more complicated. 其中x*x是用户定义的计算,通常要复杂得多。 I'd like to wrap cfunction using Rcpp so that the R user could call it on arbitrary R functions. 我想使用Rcpp包装cfunction以便R用户可以在任意R函数上调用它。

NumericVector rfunction(Function F){

  NumericVector result(1);

  // MAGIC THAT I DON'T KNOW HOW TO DO
  // SOMEHOW TURN F INTO COMPATIBLE input_funcion

  result[0] = cfunction(input_function);

  return(result);
  }

The R user then might do rfunction(function(x) {x*x}) and get the right result. 然后R用户可以执行rfunction(function(x) {x*x})并获得正确的结果。

I am aware that calling R functions within cfunction will kill the speed but I figure that I can figure out how to pass compiled functions later on. 我知道在cfunction中调用R函数会降低速度,但我认为我可以弄清楚如何在以后传递编译函数。 I'd just like to get this part working. 我只想让这部分工作。

The closest thing I can find that does what I need is this https://sites.google.com/site/andrassali/computing/user-supplied-functions-in-rcppgsl which wraps a function that uses callback that has an oh-so-useful second parameter within which I could stuff the R function. 我能找到的最接近我需要的是这个https://sites.google.com/site/andrassali/computing/user-supplied-functions-in-rcppgsl ,它包含一个使用回调函数的函数非常有用的第二个参数,我可以填充R函数。

Advice would be gratefully received. 我们将非常感谢您的建议。

One possible solution would be saving the R-function into a global variable and defining a function that uses that global variable. 一种可能的解决方案是将R函数保存为全局变量并定义使用该全局变量的函数。 Example implementation where I use an anonymous namespace to make the variable known only within the compilation unit: 我使用匿名命名空间使变量只在编译单元中知道的示例实现:

#include <Rcpp.h>

extern "C" {
    double cfunction(double (*input_function)(const double&)) {
        return input_function(42);
    }
}

namespace {
std::unique_ptr<Rcpp::Function> func;
}

double input_function(const double &x) {
    Rcpp::NumericVector result = (*func)(x);
    return result(0);
}


// [[Rcpp::export]]
double rfunction(Rcpp::Function F){
    func = std::make_unique<Rcpp::Function>(F);
    return cfunction(input_function);
}

/*** R
rfunction(sqrt)
rfunction(log)
*/

Output: 输出:

> Rcpp::sourceCpp('57137507/code.cpp')

> rfunction(sqrt)
[1] 6.480741

> rfunction(log)
[1] 3.73767

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM