通过环境 Rcpp 从 C++ 调用函数 [英] Call a function from c++ via environment Rcpp
本文介绍了通过环境 Rcpp 从 C++ 调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在考虑通过环境从 C++ 调用 R 函数,但出现错误,这是我所做的
I am considering calling a R function from c++ via environment, but I got an error, here is what I did
#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
我知道我可以用另一种方式来做,
I know I can do it right in another way,
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector callFunction(NumericVector x, Function f) {
NumericVector res = f(x);
return res;
}
并输入
callFunction(x,fivenum)
但还是想知道为什么第一种方法失败了.
But still wondering why first method failed.
推荐答案
fivenum
函数不是在全局环境中定义的,而是在 stats
包环境中定义的,因此您应该从中获取:>
fivenum
function is not defined in the global environment but in the stats
package enviroment, so you should get it from that:
...
Environment stats("package:stats");
Function f = stats["fivenum"];
...
这篇关于通过环境 Rcpp 从 C++ 调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文