[英]Building package with Rcpp, generated .h file missing header
我目前正在RStudio中使用Rcpp構建一個程序包。 我定義了以下.cpp文件,該文件可與Rcpp::sourceCpp
。
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::interfaces(r, cpp)]]
#include <Rcpp.h>
#include <unordered_set>
using namespace Rcpp;
// [[Rcpp::export]]
std::unordered_set<int> traverse_cor(NumericMatrix x, float maxcor) {
std::unordered_set<int> elements;
int ncol = x.ncol();
for(int i = 0; i < ncol; i++) {
for(int j = 0; j < ncol; j++) {
if(i < j) {
if(x(i, j) > maxcor && x(i, j) < 1){
elements.insert(i + 1);
}
}
}
}
return elements;
}
我正在從這里和這里遵循指示。 接下來,我調用Rcpp::compileAttributes()
。 這將產生以下文件:
生成的mypackage_RcppExports.h文件如下所示:
// This file was generated by Rcpp::compileAttributes
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
#ifndef __gwassim_RcppExports_h__
#define __gwassim_RcppExports_h__
#include <Rcpp.h>
namespace gwassim {
using namespace Rcpp;
namespace {
void validateSignature(const char* sig) {
Rcpp::Function require = Rcpp::Environment::base_env()["require"];
require("gwassim", Rcpp::Named("quietly") = true);
typedef int(*Ptr_validate)(const char*);
static Ptr_validate p_validate = (Ptr_validate)
R_GetCCallable("gwassim", "gwassim_RcppExport_validate");
if (!p_validate(sig)) {
throw Rcpp::function_not_exported(
"C++ function with signature '" + std::string(sig) + "' not found in gwassim");
}
}
}
inline std::unordered_set<int> traverse_cor(NumericMatrix x, float maxcor) {
typedef SEXP(*Ptr_traverse_cor)(SEXP,SEXP);
static Ptr_traverse_cor p_traverse_cor = NULL;
if (p_traverse_cor == NULL) {
validateSignature("std::unordered_set<int>(*traverse_cor)(NumericMatrix,float)");
p_traverse_cor = (Ptr_traverse_cor)R_GetCCallable("gwassim", "gwassim_traverse_cor");
}
RObject __result;
{
RNGScope __rngScope;
__result = p_traverse_cor(Rcpp::wrap(x), Rcpp::wrap(maxcor));
}
if (__result.inherits("interrupted-error"))
throw Rcpp::internal::InterruptedException();
if (__result.inherits("try-error"))
throw Rcpp::exception(as<std::string>(__result).c_str());
return Rcpp::as<std::unordered_set<int> >(__result);
}
}
#endif // __gwassim_RcppExports_h__
嘗試構建並重新加載程序包后,我收到以下錯誤(1):
../inst/include/gwassim_RcppExports.h:27:12:錯誤:名稱空間“ std”中的“ unordered_set”未命名類型
和(2)
RcppExports.cpp:12:1:錯誤:名稱空間'std'中的'unordered_set'未命名類型
我的C ++經驗有限,但是我的感覺是這些錯誤是由於省略了#include <unordered_set>
而發生的。 如何使這些自動生成的文件具有正確的標題?
我的sessionInfo如下:
Session info ----------------------------------------------------------------------
setting value
version R version 3.1.0 (2014-04-10)
system x86_64, mingw32
ui RStudio (0.99.235)
language (EN)
collate English_United States.1252
tz America/New_York
Packages --------------------------------------------------------------------------
package * version date source
devtools 1.7.0.9000 2015-02-11 Github (hadley/devtools@9415a8a)
digest * 0.6.4 2013-12-03 CRAN (R 3.1.0)
memoise * 0.2.1 2014-04-22 CRAN (R 3.1.0)
mvtnorm 1.0-2 2014-12-18 CRAN (R 3.1.2)
Rcpp 0.11.4 2015-01-24 CRAN (R 3.1.2)
roxygen2 * 4.1.0 2014-12-13 CRAN (R 3.1.2)
rstudioapi * 0.2 2014-12-31 CRAN (R 3.1.2)
stringr * 0.6.2 2012-12-06 CRAN (R 3.0.0)
我的g ++版本是4.6.3,包含在Windows的RTools軟件包中。 我已經啟用了具有以下功能的C ++ 11功能: Sys.setenv("PKG_CXXFLAGS"="-std=c++0x")
。
那是一個挑剔的人。 我認為您想要Rcpp屬性小插圖的3.5.2節和這個技巧:
Package.h
文件除了包含Package_RcppExports.h
標頭外,什么也不做。 這樣做是為了使程序包作者可以用自定義程序包替換Package.h
標頭,並且仍然可以包括自動生成的導出文件(下一節將提供有關此操作的詳細信息)。
順便說一句,我想我也說服了您創建一個包,而不是僅僅依賴sourceCpp()
:)
編輯: Do! 我忽略了
std::unordered_set<int> traverse_cor(NumericMatrix x, float maxcor)
可能不是自動可包裝的函數。 您可能需要將您的集合轉換為向量(或列表或...),以獲取與R自然匹配的類型之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.