使用 Rcpp 构建包,生成的 .h 文件丢失 header
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;
}
我正在按照 here and here 的指示进行操作。接下来我调用 Rcpp::compileAttributes()
。这会生成以下文件:
- src/RcppExports.cpp
- R/RcppExports.R
- inst/include/mypackage.h
- inst/include/mypackage_RcppExports.h
生成的 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: error: 'unordered_set' in
namespace 'std' does not name a type
和 (2)
RcppExports.cpp:12:1: error: 'unordered_set' in namespace 'std' does
not name a type
我的 C++ 经验有限,但我的感觉是这些错误是由于 #include <unordered_set>
被省略所致。如何让这些自动生成的文件具有正确的 headers?
我的会话信息如下:
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 Attributes 小插图的第 3.5.2 节和这个技巧:
The Package.h
file does nothing other than include the
Package_RcppExports.h
header. This is done so that package
authors can replace the Package.h
header with a custom one
and still be able to include the automatically generated exports
(details on doing this are provided in the next section).
顺便说一句,我想我也说服了你创建一个包而不是仅仅依赖 sourceCpp()
:)
编辑: 哦!!我忽略了
的部分
std::unordered_set<int> traverse_cor(NumericMatrix x, float maxcor)
可能不是一个可自动包装的函数。您可能需要将您的集合转换为向量(或列表或...)以获得与 R 自然匹配的类型之一。
我目前正在 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;
}
我正在按照 here and here 的指示进行操作。接下来我调用 Rcpp::compileAttributes()
。这会生成以下文件:
- src/RcppExports.cpp
- R/RcppExports.R
- inst/include/mypackage.h
- inst/include/mypackage_RcppExports.h
生成的 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: error: 'unordered_set' in namespace 'std' does not name a type
和 (2)
RcppExports.cpp:12:1: error: 'unordered_set' in namespace 'std' does not name a type
我的 C++ 经验有限,但我的感觉是这些错误是由于 #include <unordered_set>
被省略所致。如何让这些自动生成的文件具有正确的 headers?
我的会话信息如下:
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 Attributes 小插图的第 3.5.2 节和这个技巧:
The
Package.h
file does nothing other than include thePackage_RcppExports.h
header. This is done so that package authors can replace thePackage.h
header with a custom one and still be able to include the automatically generated exports (details on doing this are provided in the next section).
顺便说一句,我想我也说服了你创建一个包而不是仅仅依赖 sourceCpp()
:)
编辑: 哦!!我忽略了
的部分std::unordered_set<int> traverse_cor(NumericMatrix x, float maxcor)
可能不是一个可自动包装的函数。您可能需要将您的集合转换为向量(或列表或...)以获得与 R 自然匹配的类型之一。