[英]Using C++ function as an argument in another C++ function in R package "Rcpp"
I am new in using R package "Rcpp".我是使用 R 包“Rcpp”的新手。 My question is, if I want to use a C++ function as an argument in another C++ function, how will I do that?
我的问题是,如果我想在另一个 C++ 函数中使用一个 C++ 函数作为参数,我该怎么做? For example: suppose I have an C++ function like this:
例如:假设我有一个这样的 C++ 函数:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double maxllC3(const double mu){
double result;
result= R::dgamma(mu,0.1,1,0.1);
return result;
}
. . I want to use this function in another C++ function like we can do this in R:
我想在另一个 C++ 函数中使用这个函数,就像我们可以在 R 中那样:
sumf<-function(maxllC3,mu,y){
res<-maxllC3(mu)+y
return(res)
}
. . How can I do that in "Rcpp" package?
我怎样才能在“Rcpp”包中做到这一点?
put you rcpp code in an .cpp file (say ex.cpp
).将你的 rcpp 代码放在一个 .cpp 文件中(比如
ex.cpp
)。
use Rcpp::sourceCpp('ex.cpp')
to make this function available in the global environment使用
Rcpp::sourceCpp('ex.cpp')
使这个函数在全局环境中可用
then just use sumf <- function(mu, y) maxllC3(mu) + y
然后只需使用
sumf <- function(mu, y) maxllC3(mu) + y
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.