且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在构建R程序包时从另一个Rcpp函数调用Rcpp函数

更新时间:2023-11-24 11:41:46

两种可能的解决方案:

快捷方式"解决方案-在使用函数的文件中包含函数声明:

  #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.