【问题标题】:Call a function from c++ via environment Rcpp通过环境 Rcpp 从 C++ 调用函数
【发布时间】:2016-04-13 20:58:55
【问题描述】:

我正在考虑通过环境从 c++ 调用 R 函数,但我得到了一个错误,这就是我所做的

#include <Rcpp.h>
using namespace Rcpp;



// [[Rcpp::export]]
NumericVector call(NumericVector x){
  Environment env = Environment::global_env();
  Function f = env["fivenum"];
  NumericVector res = f(x);
  return res;
}

输入call(x),这是我得到的,

Error: cannot convert to function

我知道我可以用另一种方式做到这一点,

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
NumericVector callFunction(NumericVector x, Function f) {
    NumericVector res = f(x);
    return res;
}

然后输入

callFunction(x,fivenum)

但仍然想知道为什么第一种方法失败了。

【问题讨论】:

  • fivenum 函数未在全局环境中定义,但在 stats 包中...不确定,但这应该可以:Environment stats("package:stats"); Function f = stats["fivenum"];
  • 是的!有用!非常感谢!

标签: r rcpp


【解决方案1】:

fivenum函数不是在全局环境中定义的,而是在stats包环境中定义的,所以你应该从中得到它:

...
Environment stats("package:stats"); 
Function f = stats["fivenum"];
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多