RcppArmadillo传递用户定义的函数

bap*_*ste 38 r armadillo rcpp

考虑以下R代码,

## ----------- R version -----------

caller <- function(x=1:3, fun = "identity", ...){

  ## do some other stuff
  ## ...
  ## then call the function
  eval(call(fun, x))

}

fun1 <- function(x, ...){
  x + x
}

fun2 <- function(x, a = 10) a * x

caller(fun = "fun1")
caller(fun = "fun2")
Run Code Online (Sandbox Code Playgroud)

用户可以传递函数名称"fun" caller.我希望用RcppArmadillo对象执行相同的任务(显然是一个更复杂的任务的一部分).该函数将被定义C++,并且用户通过引用其名称在R级别选择它:

caller_cpp(1:3, "fun1_cpp")
Run Code Online (Sandbox Code Playgroud)

要么

caller_cpp(1:3, "fun2_cpp")
Run Code Online (Sandbox Code Playgroud)

等等

这是我对调用函数的天真尝试,甚至无法编译:

## ----------- C++ version -----------

library(Rcpp)
require( RcppArmadillo )    

sourceCpp( code = '

       // [[Rcpp::depends("RcppArmadillo")]]

       #include <RcppArmadillo.h>

       using namespace arma ; 
       using namespace Rcpp ;


       colvec fun1_cpp(const colvec x)
      {
       colvec y ;
       y = x + x;
       return (y);
      }

       colvec fun2_cpp(const colvec x)
      {
       colvec y ;
       y = 10*x;
       return (y);
      }

     // mysterious pointer business in an attempt 
     // to select a compiled function by its name

      typedef double (*funcPtr)(SEXP);
      SEXP putFunPtrInXPtr(SEXP funname) {
            std::string fstr = Rcpp::as<std::string>(funname);
            if (fstr == "fun1")
                return(Rcpp::XPtr<funcPtr>(new funcPtr(&fun1_cpp)));
            else if (fstr == "fun2")
            return(Rcpp::XPtr<funcPtr>(new funcPtr(&fun2_cpp)));

       }

       // [[Rcpp::export]]
       colvec caller_cpp(const colvec x, character funname)
      {
       Rcpp::XPtr fun = putFunPtrInXPtr(funname);
       colvec y ;
       y = fun(x);
       return (y);
      }

   ')
Run Code Online (Sandbox Code Playgroud)

编辑:按照德克的建议看了RcppDE之后改编了这个例子.

Dir*_*tel 29

(有时您需要使用svn log ...文件来查看它们的日期...)

我认为一个更好的用例是在我的"端口"基于C的DEoptim到Rcpp/RcppArmadillo:RcppDE.在其中,我允许优化例程使用R函数(如DEoptim所做)或用户提供的编译函数 - 这就是我所理解的这一点.

有一点C++脚手架,但你应该没有问题.

编辑于2013-01-21下面是一个完整的解决方案,我也在Rcpp画廊发布了这篇新帖子 - 包括一些评论和样本用法.

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>

using namespace arma; 
using namespace Rcpp;

vec fun1_cpp(const vec& x) {    // a first function 
    vec y = x + x;
    return (y);
}

vec fun2_cpp(const vec& x) {    // and a second function
    vec y = 10*x;
    return (y);
}

typedef vec (*funcPtr)(const vec& x);

// [[Rcpp::export]]
XPtr<funcPtr> putFunPtrInXPtr(std::string fstr) {
    if (fstr == "fun1")
        return(XPtr<funcPtr>(new funcPtr(&fun1_cpp)));
    else if (fstr == "fun2")
        return(XPtr<funcPtr>(new funcPtr(&fun2_cpp)));
    else
        return XPtr<funcPtr>(R_NilValue); // runtime error as NULL no XPtr
}

// [[Rcpp::export]]
vec callViaString(const vec x, std::string funname) {
    XPtr<funcPtr> xpfun = putFunPtrInXPtr(funname);
    funcPtr fun = *xpfun;
    vec y = fun(x);
    return (y);
}

// [[Rcpp::export]]
vec callViaXPtr(const vec x, SEXP xpsexp) {
    XPtr<funcPtr> xpfun(xpsexp);
    funcPtr fun = *xpfun;
    vec y = fun(x);
    return (y);
}
Run Code Online (Sandbox Code Playgroud)

  • 这太棒了; 你应该等待赏金! (3认同)
  • 你仍然需要实际的函数指针,但你可以在给定地图的情况下在它们之间切换. (2认同)