简体   繁体   English

RCPP +内联-创建和调用其他功能

[英]Rcpp + inline - creating and calling additional functions

I would like to know if there is a way to create Rcpp functions using the inline packages within the main function. 我想知道是否有一种方法可以使用Rcpp函数中的inline包创建Rcpp函数。 This is an example of what I want to do: 这是我想做的一个例子:

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);
                           ")

which should result in: 这应该导致:

> cpp.fun(a)
[1]  1  4  9  16  25  36  49  64  81  100

however I know that the compiler will not accept the creation of your own function within the main method. 但是我知道编译器不会在main方法中接受您自己的函数的创建。 How will I go about creating and calling another Rcpp function with inline without having to pass it through to R? 我将如何使用inline创建和调用另一个Rcpp函数,而不必将其传递给R?

body is for the body of the function, you want to look at the includes argument of cxxfunction : body是函数的主体,您需要查看cxxfunctionincludes参数:

library(inline)
library(Rcpp)
a = 1:10
cpp.fun = cxxfunction(signature(data1="numeric"), 
                      plugin="Rcpp",
                      body='

IntegerVector fun_data  = data1;
int n = fun_data.size();
for(int i=0;i<n;i++){
    fun_data[i] = fun1(fun_data[i]);
}
return(fun_data);
', includes = '

int fun1( int a1){
    int b1 = a1;
    b1 = b1*b1;
    return(b1);
}

' )    
cpp.fun( a )

?cxxfunction has detailed documentation about its includes argument. ?cxxfunction有关于其includes参数的详细文档。

But note that this version will make in place modifications in your input vector, which is probably not what you want. 但是请注意,此版本将在您的输入向量中进行适当的修改,这可能不是您想要的。 Another version that also takes advantage of Rcpp version of sapply : 另一个版本,还利用Rcpp的版本sapply

library(inline)
library(Rcpp)
a = 1:10
cpp.fun = cxxfunction(signature(data1="numeric"), 
                      plugin="Rcpp",
                      body='

IntegerVector fun_data  = data1; 
IntegerVector out = sapply( fun_data, fun1 ) ;
return(out);
', includes = '

int fun1( int a1){
    int b1 = a1;
    b1 = b1*b1;
    return(b1);
}

' )    
cpp.fun( a )
a

Finally, you definitely should have a look at sourceCpp . 最后,您绝对应该看看sourceCpp With it you would write your code in a .cpp file, containing: 有了它,您将在.cpp文件中编写代码,其中包含:

#include <Rcpp.h>
using namespace Rcpp ;

int fun1( int a1){
    int b1 = a1;
    b1 = b1*b1;
    return(b1);
}

// [[Rcpp::export]]
IntegerVector fun(IntegerVector fun_data){ 
    IntegerVector out = sapply( fun_data, fun1 ) ;
    return(out);
}

And then, you can just sourceCpp your file and invoke the function : 然后,您可以仅sourceCpp您的文件并调用该函数:

sourceCpp( "file.cpp" )
fun( 1:10 )
#  [1]   1   4   9  16  25  36  49  64  81 100

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

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