Rcpp - 在 sourceCpp 引用的文件中使用多个 C++ 函数? [英] Rcpp - Use multiple C++ functions in file referenced by sourceCpp?
问题描述
我希望这不是太明显,因为我已经搜索了一整天,但找不到答案.
I hope this isn't too obvious, as I've searched all day and can't find the answer.
假设我有以下 R 文件:
Say I have the following R file:
library(Rcpp)
sourceCpp("cfile.cpp")
giveOutput(c(1,2,3))
它编译以下 C++ 文件:
And it compiles the following C++ file:
#include <Rcpp>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector plusTwo(NumericVector x){
NumericVector out = x + 2.0;
return out;
}
NumericVector giveOutput(NumericVector a){
NumericVector b = plusTwo(a);
return b;
}
无论我尝试什么,Rcpp 预处理器都会使 plusTwo()
可用,而 giveOutput()
根本不可用.我能找到的文档说这是应该创建一个包的点,但在阅读包小插图后,它似乎比我需要的要复杂一个数量级.
No matter what I try, the Rcpp preprocessor makes plusTwo()
available, and giveOutput()
not at all. The documentation I've been able to find says that this is the point at which one should create a package, but after reading the package vignette it seems an order of magnitude more complicated than what I need.
在 giveOutput()
中没有明确定义 plusTwo()
,我该怎么办?
Short of explicitly defining plusTwo()
inside giveOutput()
, what can I do?
推荐答案
您应该在要导出的每个函数前使用 export 属性.因此,通过将您的文件更正为
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;
}
我得到了想要的行为:
R> sourceCpp("/tmp/patrick.cpp")
R> giveOutput(1:3)
[1] 3 4 5
R> plusTwo(1:3)
[1] 3 4 5
R>
哦,创建一个包就像调用 Rcpp.package.skeleton()
一样简单(但请阅读它的帮助页面,特别是 attributes
参数).我知道至少有一个 CRAN 包开始了你从这里开始的方式,很明显通过 Rcpp.package.skeleton()
...
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()
...
这篇关于Rcpp - 在 sourceCpp 引用的文件中使用多个 C++ 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!