Can we use Rcpp with several C ++ functions?

I create my own function in the .clanguage. Then I create a function R to call the function .cusing .cfor example

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

which is called the wreper R-function. In addition, my function is based on other functions .c. As I know, use Rcppmakes it more flexible and easy, so:

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

I also know what cppFunctionworks with the language C++. However, I see that between the .cfunction and .c++there is nothing else.

: cppFunction .c, requrie wraper 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;
    }')

, build cppFunction, ?

?

+4
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) cpp.

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. file.cpp, 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( ) #include "sum.h"

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

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

Source: https://habr.com/ru/post/1682187/


All Articles