[英]Calling a Rcpp function from another Rcpp function while building an R package
I took this example from a different question. 我从另一个问题中拿了这个例子。 I am building an R package with Rcpp. 我正在用Rcpp构建一个R包。 I have a function like fun1
(below) that I want to put into its own .cpp
file. 我有一个像fun1
(下面)这样的函数,我想将它放入自己的.cpp
文件中。 Then I want to call fun1
with other functions (like fun()
does below). 然后我想用其他函数调用fun1
(如下面的fun()
)。 I want fun1
in a separate file because I am going to call it from several Rcpp functions that are in different .cpp
files. 我想在一个单独的文件中使用fun1
,因为我将从不同的.cpp
文件中的几个Rcpp函数调用它。 Are there certain include statements and things I need to do to make the fun1
function accessible in the .cpp
where fun()
is located? 是否有某些include语句和我需要做的事情来使fun1
函数在.cpp
中可以访问fun()
所在的位置? Thank you. 谢谢。
library(inline)
library(Rcpp)
a = 1:10
cpp.fun = cxxfunction(signature(data1="numeric"),
plugin="Rcpp",
body="
int fun1( int a1)
{int b1 = a1;
b1 = b1*b1;
return(b1);
}
NumericVector fun_data = data1;
int n = data1.size();
for(i=0;i<n;i++){
fun_data[i] = fun1(fun_data[i]);
}
return(fun_data);
")
So for my code I will have two .cpp
files: 所以对于我的代码,我将有两个.cpp
文件:
#include <Rcpp.h>
using namespace Rcpp;
// I think I need something here to make fun1.cpp available?
// [[Rcpp::export]]
Rcpp::NumericVector fun(Rcpp::NumericVector data1)
{
NumericVector fun_data = data1;
int n = data1.size();
for(i=0;i<n;i++){
fun_data[i] = fun1(fun_data[i]);
}
return(fun_data);
}
And a second .cpp
file: 第二个.cpp
文件:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int fun1( int a1)
{int b1 = a1;
b1 = b1*b1;
return(b1);
}
Two possible solutions: 两种可能的解决方
The 'quick-and-dirty', solution -- include the function declaration in the file where you use it: “快速而肮脏”的解决方案 - 在您使用它的文件中包含函数声明:
#include <Rcpp.h>
using namespace Rcpp;
// declare fun1
int fun1(int a1);
// [[Rcpp::export]]
Rcpp::NumericVector fun(Rcpp::NumericVector data1)
{
NumericVector fun_data = data1;
int n = data1.size();
for(i=0;i<n;i++){
fun_data[i] = fun1(fun_data[i]);
}
return(fun_data);
}
The more robust solution: write header files that declare the functions, which can then be #include
-ed in each file. 更强大的解决方案:编写声明函数的头文件,然后可以在每个文件中使用#include
-ed。 So you might have a header file fun1.h
in the same src
directory: 所以你可能在同一个src
目录中有一个头文件fun1.h
:
#ifndef PKG_FOO1_H
#define PKG_FOO1_H
int foo(int);
#endif
which you could then use with something like: 您可以使用以下内容:
#include <Rcpp.h>
#include "fun1.h"
using namespace Rcpp;
// [[Rcpp::export]]
Rcpp::NumericVector fun(Rcpp::NumericVector data1)
{
NumericVector fun_data = data1;
int n = data1.size();
for(i=0;i<n;i++){
fun_data[i] = fun1(fun_data[i]);
}
return(fun_data);
}
As you progress, you're going to need to learn more C++ programming skills, so I recommend checking out one of the books here ; 随着您的进步,您将需要学习更多的C ++编程技能,所以我建议您查看其中一本书 ; in particular, Accelerated C++ is a great introduction. 特别是, Accelerated C ++是一个很好的介绍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.