简体   繁体   English

我们可以将Rcpp与多个C ++函数一起使用吗?

[英]Can we use Rcpp with multiple C++ functions?

I build my own function in .c language. 我用.c语言构建自己的函数。 Then, I buit an R function to call the .c function using .C eg, 然后,我使用.C来编写一个R函数来调用.c函数,例如,

 tmp <- .C("myfunction",
as.integer(N),
as.integer(n),
as.integer(w1),
as.integer(w2),
as.integer(w3),
PACKAGE = "mypackage")[[3]]

which is called a wraper R function. 这被称为包裹R功能。 Also, my function based or requries other .c functions. 此外,我的功能基于或要求其他.c功能。 As I know, using Rcpp make it is more flexiable and easy, such that: 据我所知,使用Rcpp使它更Rcpp ,更容易,这样:

cppFunction('int add(int x, int y, int z) {
  int sum = x + y + z;
  return sum;
}')

I also know that cppFunction works with C++ language. 我也知道cppFunction适用于C++语言。 However, I see there is no much different between .c function and .c++ . 但是,我发现.c函数和.c++之间没有太大区别。

My question is: Can I use cppFunction with my .c function that requrie wraper R function? 我的问题是:我可以将cppFunction与我的.c函数一起使用requrie wraper R函数吗? or I need to convert my .c functions into .c++ functions first? 或者我需要先将.c函数转换为.c++函数? What about other function that my function is based on? 那我的功能所基于的其他功能怎么样? Does that will be as any regular R function? 这会像任何常规的R函数一样吗? I mean: suppose I have two cppFunction functions myfunc1 and myfunc2 where myfunc2 based on myfunc1 . 我的意思是:假设我有两个cppFunction函数myfunc1myfunc2 ,其中myfunc2基于myfunc1 Then suppose my second cppFunction is as follows: 然后假设我的第二个cppFunction如下:

cppFunction('int myfunc2(int x, int y, int z) {
      int sum = x + y + z;
      myfunc1 ## do some works here
      return something;
    }')

Is that will be Ok? 那会好吗? or Do I need to write it as follow: 或者我需要写如下:

cppFunction('int myfunc2(int x, int y, int z) {
      int sum = x + y + z;
      cppFunction('int myfunc2(int some arguments) {## do some works here}
      return something;
    }')

In general how to use build cppFunction that contains multiple functions? 一般来说如何使用包含多个函数的build cppFunction

any help please? 有什么帮助吗?

Here is an example using an external cpp file. 以下是使用外部cpp文件的示例。 The functions can interact inside the same file, but like others said you have to use header to use function from others files. 这些函数可以在同一个文件中进行交互,但是像其他人一样,你必须使用header来使用其他文件中的函数。 You have to use // [[Rcpp::export]] before any functions you want available in R. 在R中需要的任何函数之前,必须使用// [[Rcpp::export]]

(Credit to @F.Privé for improving the code) (感谢@F.Privé改进代码)

1) File cpp: 1)文件cpp:

#include <Rcpp.h>
using namespace Rcpp;



// [[Rcpp::export]]
double sumC(NumericVector x) {
  int n = x.size();
  double total = 0;

  for(int i = 0; i < n; ++i) {
    total += x[i];
  }
  return total;
}

// [[Rcpp::export]]
double meanC(NumericVector x) {
  return sumC(x) / x.size();
}

File R: 档案R:

Rcpp::sourceCpp("your path/mean.cpp")
x <- 1:10
meanC(x)
sumC(x)

2) Alternative approach using cppfunction . 2) 使用cppfunction的替代方法 You have to use includes argument 你必须使用包含参数

cppFunction('double meanC(NumericVector x) {
  return sumC(x) / x.size();
}',includes='double sumC(NumericVector x) {
  int n = x.size();
  double total = 0;

  for(int i = 0; i < n; ++i) {
    total += x[i];
  }
  return total;
}')

Anyway I suggest you to use sourceCpp, using independent files result in much more maintainable and clean code 无论如何,我建议你使用sourceCpp,使用独立的文件会产生更多可维护和干净的代码

3) Using sourceCPP and multiple cpp files . 3) 使用sourceCPP和多个cpp文件 You have to use headers file and do an header file for every file.cpp you want to use inside other cpp files. 您必须使用头文件并为要在其他cpp文件中使用的每个file.cpp执行头文件。

sum.h file (ifndef prevent multiple definition) sum.h文件(ifndef防止多重定义)

#include <Rcpp.h>
#ifndef SUM1
#define SUM1

double sumC(Rcpp::NumericVector x);

#endif

sum.cpp (like before) sum.cpp(像之前一样)

#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double sumC(NumericVector x) {
  int n = x.size();
  double total = 0;

  for(int i = 0; i < n; ++i) {
    total += x[i];
  }
  return total;
}

mean.cpp file (you have to include sum header) #include "sum.h" mean.cpp文件(你必须包括sum头) #include“sum.h”

#include <Rcpp.h>
#include "sum.h"
using namespace Rcpp;

// [[Rcpp::export]]
double meanC(NumericVector x) {
  return sumC(x) / x.size();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM