【问题标题】:Can we use Rcpp with multiple C++ functions?我们可以将 Rcpp 与多个 C++ 函数一起使用吗?
【发布时间】:2017-07-25 07:14:49
【问题描述】:

我用.c 语言构建自己的函数。然后,我构建了一个 R 函数来使用 .C 调用 .c 函数,例如,

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

称为包装 R 函数。此外,我的函数基于或需要其他 .c 函数。据我所知,使用Rcpp 使其更加灵活和简单,例如:

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

我也知道cppFunction 可以与C++ 语言一起使用。但是,我发现.c 函数和.c++ 之间没有太大区别。

我的问题是:我可以将 cppFunction 与我的 .c 函数一起使用,该函数需要包装器 R 函数吗?或者我需要先将我的.c 函数转换为.c++ 函数?我的功能所基于的其他功能呢?这会像任何常规的R 函数一样吗? 我的意思是:假设我有两个cppFunction 函数myfunc1 和myfunc2,其中myfunc2 基于myfunc1。那么假设我的第二个cppFunction如下:

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

这样会好吗?还是我需要这样写:

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;
    }')

一般如何使用包含多个功能的构建cppFunction?

有什么帮助吗?

【问题讨论】:

  • 我想如果你把所有东西都放在一个 Rcpp ".cpp" 文件中(从默认的 RStudio 开始),就可以了。
  • @F.Privé 非常感谢您的评论。你的意思是我可以将它作为包括多个函数的常规 R 函数来完成?
  • 我的意思是,如果两个Rcpp函数在同一个文件中,它们就相互认识。否则,您将不得不使用标题。
  • @F.Privé 再次感谢您。是的,我明白你知道。我正在使用我自己的包,我认为它也可以。

标签: c++ c r


【解决方案1】:

这是一个使用外部 cpp 文件的示例。 这些函数可以在同一个文件中交互,但就像其他人说的那样,您必须使用标题才能使用其他文件中的函数。 你必须使用// [[Rcpp::export]],然后才能在 R 中使用任何你想要的功能。

(感谢@F.Privé 改进代码)

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();
}

文件 R:

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

2)使用 cppfunction 的替代方法。您必须使用包含参数

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;
}')

无论如何我建议你使用 sourceCpp,使用独立的文件可以使代码更易于维护和干净

3)使用 sourceCPP 和多个 cpp 文件。您必须使用头文件并为要在其他 cpp 文件中使用的每个 file.cpp 做一个头文件。

sum.h 文件(ifndef 防止多重定义)

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

double sumC(Rcpp::NumericVector x);

#endif

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 文件 (你必须包含 sum header) #include "sum.h"

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

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

【讨论】:

  • 也许是为了让 Alice 知道函数是相互认识的,你只能在 meanC() 中使用 return sumC(x) / x.size();。
  • @F.Privé 是的,您的评论正是我所需要的。
  • @F.Privé 非常感谢您,也非常感谢 Federico Mangirasso 的帮助。然后我可以轻松使用cppFunction。再次感谢。
  • 您好,感谢您的帮助。我改编了一个我为你准备的例子,我还在编辑。请注意,要在meanC中使用sumC,您必须先定义sumC,否则代码无法编译
  • @FedericoManigrasso 您能否将cppFunction 添加到您的答案中,因为它是我问题的主要部分,因此对可能感兴趣的人非常有帮助。非常感谢,。
猜你喜欢
  • 1970-01-01
  • 2012-04-25
  • 2022-01-04
  • 2012-07-13
  • 1970-01-01
  • 2021-06-02
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多