Rcpp - Use multiple C++ functions in file referenced by sourceCpp?

前端 未结 1 1923

I hope this isn\'t too obvious, as I\'ve searched all day and can\'t find the answer.

Say I have the following R file:

library(Rcpp)
sourceCpp(\"cfi         


        
相关标签:
1条回答
  • 2021-01-02 03:50

    You are expected to use the export attribute in front of every function you wanted exported. So by correcting your file to

    #include <Rcpp.h>
    
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    NumericVector plusTwo(NumericVector x){
      NumericVector out = x + 2.0;
      return out;
    }
    
    // [[Rcpp::export]]
    NumericVector giveOutput(NumericVector a){
      NumericVector b = plusTwo(a);
      return b;
    }
    

    I get the desired behaviour:

    R> sourceCpp("/tmp/patrick.cpp")
    R> giveOutput(1:3)
    [1] 3 4 5
    R> plusTwo(1:3)
    [1] 3 4 5
    R> 
    

    Oh, and creating a package is as easy as calling Rcpp.package.skeleton() (but read its help page, particularly for the attributes argument). I know of at least one CRAN package that started how you started here and clearly went via Rcpp.package.skeleton()...

    0 讨论(0)
提交回复
热议问题