在构建R程序包时从另一个Rcpp函数调用Rcpp函数 [英] Calling a Rcpp function from another Rcpp function while building an R package
问题描述
我从另一个问题中举了这个例子.我正在用Rcpp构建R包.我有一个类似 fun1
(如下)的函数,我想将其放入自己的 .cpp
文件中.然后,我想用其他函数调用 fun1
(例如下面的 fun()
一样).我想要 fun1
在一个单独的文件中,因为我将从不同 .cpp
文件中的多个Rcpp函数调用它.为了使 fun()
所在的 .cpp
中的 fun1
函数可访问,是否有某些include语句和我需要做的事情?谢谢你.
库(内联)图书馆(Rcpp)a = 1:10cpp.fun = cxxfunction(signature(data1 ="numeric"),plugin ="Rcpp",身体=int fun1(int a1){int b1 = a1;b1 = b1 * b1;return(b1);}NumericVector fun_data = data1;int n = data1.size();for(i = 0; i
因此对于我的代码,我将有两个 .cpp
文件:
#include< Rcpp.h>使用命名空间Rcpp;//我想我需要一些东西来使fun1.cpp可用?//[[[Rcpp :: export]]Rcpp :: NumericVector乐趣(Rcpp :: NumericVector data1){NumericVector fun_data = data1;int n = data1.size();for(i = 0; i
和第二个 .cpp
文件:
#include< Rcpp.h>使用命名空间Rcpp;//[[[Rcpp :: export]]int fun1(int a1){int b1 = a1;b1 = b1 * b1;return(b1);}
两种可能的解决方案:
快捷方式"解决方案-在使用函数的文件中包含函数声明:
#include< Rcpp.h>使用命名空间Rcpp;//声明fun1int fun1(int a1);//[[[Rcpp :: export]]Rcpp :: NumericVector乐趣(Rcpp :: NumericVector data1){NumericVector fun_data = data1;int n = data1.size();for(i = 0; i
更健壮的解决方案:编写声明函数的头文件,然后可以在每个文件中 #include
-ed.因此,您可能在同一 src
目录中有一个头文件 fun1.h
:
<代码> #ifndef PKG_FOO1_H#定义PKG_FOO1_Hint foo(int);#万一
然后可以将其与以下内容一起使用:
#include< Rcpp.h>#include"fun1.h"使用命名空间Rcpp;//[[[Rcpp :: export]]Rcpp :: NumericVector乐趣(Rcpp :: NumericVector data1){NumericVector fun_data = data1;int n = data1.size();for(i = 0; i
随着您的进步,您将需要学习更多的C ++编程技能,因此我建议您查阅加速的C ++ 是很好的介绍./p>
I took this example from a different question. I am building an R package with Rcpp. I have a function like fun1
(below) that I want to put into its own .cpp
file. Then I want to call fun1
with other functions (like fun()
does below). I want fun1
in a separate file because I am going to call it from several Rcpp functions that are in different .cpp
files. Are there certain include statements and things I need to do to make the fun1
function accessible in the .cpp
where fun()
is located? 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:
#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:
#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. So you might have a header file fun1.h
in the same src
directory:
#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; in particular, Accelerated C++ is a great introduction.
这篇关于在构建R程序包时从另一个Rcpp函数调用Rcpp函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!