在 Rcpp 中调用 R 函数 [英] Call R functions in Rcpp

查看:53
本文介绍了在 Rcpp 中调用 R 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Rcpp 中调用 sd(x),它是一个 R 函数.我看过一个在 Rcpp 中调用 R 函数 dbat(x) 的例子,效果很好.

//dens 调用 R 中 beta 分布的 pdf//[[Rcpp::export]]双密度(双x,双a,双b){返回 R::dbeta(x,a,b,false);}

但是当我累了把这个方法应用到 sd(x) 如下时,它出错了.

//std 调用 R 中的 sd 函数//[[Rcpp::export]]双标准(NumericVector x){返回 R::sd(x);}

有人知道为什么这不起作用吗?

解决方案

您的代码存在一些问题.

  1. stdC++ 标准库命名空间
    • 这是触发:<块引用>

      错误:将std"重新定义为不同类型的符号

  2. R:: 是处理 Rmath 函数.在此范围内将找不到其他R函数.
  3. 要在 C++ 中直接调用 R 函数,您必须使用 Rcpp::EnvironmentRcpp::Function,如示例 sd_r_cpp_call().
    • 这种方法存在许多问题,包括但不限于速度损失.
  4. 最好使用 Rcpp Sugar 表达式或实现您自己的方法.

说到这里,让我们谈谈代码:

#include //' @title 从 Rcpp 访问 R 的 sd 函数//[[Rcpp::export]]双 sd_r_cpp_call(const Rcpp::NumericVector& x){//获取包含函数的环境Rcpp::Environment base("package:stats");//使函数可从 C++ 调用Rcpp::Function sd_r = base["sd"];//调用函数并接收其列表输出Rcpp::NumericVector res = sd_r(Rcpp::_["x"] = x,rcpp::_["na.rm"] = true);//附加参数示例//返回列表结构中的测试对象返回资源[0];}//std 调用 R 中的 sd 函数//[[Rcpp::export]]double sd_sugar(const Rcpp::NumericVector& x){返回 Rcpp::sd(x);//使用 Rcpp 糖}/***Rx = 1:5r = sd(x)v1 = sd_r_cpp_call(x)v2 = sd_sugar(x)all.equal(r,v1)all.equal(r,v2)*/

I was trying to call the sd(x), which is a R function, in Rcpp. I've seen an example of calling the R function dbate(x) in Rcpp and it works perfectly.

// dens calls the pdf of beta distribution in R
//[[Rcpp::export]]
double dens(double x, double a, double b)
{
    return R::dbeta(x,a,b,false);
}

But when I tired to apply this method to sd(x) as following, it went wrong.

// std calls the sd function in R
//[[Rcpp::export]]
double std(NumericVector x)
{
  return R::sd(x);
}

Does anyone know why this doesn't work?

解决方案

There are a few issues with your code.

  1. std is related to the C++ Standard Library namespace
    • This is triggering:

      error: redefinition of 'std' as different kind of symbol

  2. The R:: is a namespace that deals with Rmath functions. Other R functions will not be found in within this scope.
  3. To directly call an R function from within C++ you must use Rcpp::Environment and Rcpp::Function as given in the example sd_r_cpp_call().
    • There are many issues with this approach though including but not limited to the loss of speed.
  4. It is ideal to use Rcpp sugar expressions or implement your own method.

With this being said, let's talk code:

#include <Rcpp.h>

//' @title Accessing R's sd function from Rcpp
// [[Rcpp::export]]
double sd_r_cpp_call(const Rcpp::NumericVector& x){

  // Obtain environment containing function
  Rcpp::Environment base("package:stats"); 

  // Make function callable from C++
  Rcpp::Function sd_r = base["sd"];    

  // Call the function and receive its list output
  Rcpp::NumericVector res = sd_r(Rcpp::_["x"] = x,
                                 Rcpp::_["na.rm"]  = true); // example of additional param

  // Return test object in list structure
  return res[0];
}


// std calls the sd function in R
//[[Rcpp::export]]
double sd_sugar(const Rcpp::NumericVector& x){
  return Rcpp::sd(x); // uses Rcpp sugar
}

/***R
x = 1:5
r = sd(x)
v1 = sd_r_cpp_call(x)
v2 = sd_sugar(x)

all.equal(r,v1)
all.equal(r,v2)
*/

这篇关于在 Rcpp 中调用 R 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆