fatal error: RcppArmadilloExtensions/sample.h: No such file or directory

fatal error: RcppArmadilloExtensions/sample.h: No such file or directory

我会提前为缺少可重现的示例而道歉(目前)- 如果没有它这不是一个简单的答案,我明天会做一个(尽管由于问题的性质,我我不确定那会有多容易)。

首先,我的第一个 Rcpp 代码项目开始运行了!它的速度惊人地快,并且完全满足了我的需要!感谢所有在这里帮助过我的人,感激不尽。

我的下一个任务是将其添加到包中。我一直在使用 Hadley Wickham 的 R packages 来整理我的包,所以我使用 roxygen2devtools::document() 进行文档和一般检查,然后书。我翻阅了有关编译代码的章节(http://r-pkgs.had.co.nz/src.html,以便于参考),并实现了这些步骤。具体来说:

然后我尝试更新文档(devtools::document()),我得到了以下错误:

simulate_mean.cpp:2:44: fatal error: RcppArmadilloExtensions/sample.h: No such file or directory
 #include <RcppArmadilloExtensions/sample.h>
                                            ^

我一直在搜索 stackexchange 以寻找类似的东西,并看到一个旧的 post,其中一条评论是顶部需要 // [[Rcpp::depends(RcppArmadillo)]] (Rcpp R sample equivalent from a NumericVector)。这对我来说不是问题 - 该行位于正确的位置,没有多余的空格(我在 stackexchange 上发现的另一个问题)。

文件的前几行如下所示:

#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;

(我看不出有什么问题,而且它在包外编译得很好)。

我的描述文件专门导入了 RcppRcppArmadillo。这是其中的相关部分:

Imports:
    dplyr(>= 0.7.4),
    purrr (>= 0.2.4),
    Rcpp (>= 0.12.17),
    RcppArmadillo (>= 0.8.600.0.0)
Suggests: mvtnorm (>= 1.0-6),
    testthat
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
License: MIT + file LICENSE
LinkingTo: Rcpp

作为故障排除的一部分,我已经更新了 R 和所有包,所以一切都使用最新版本。 (我也尝试了 devtools 的 github 版本,结果相同)。 关于为什么无法使用 document() 找到 sample.h 扩展名的任何想法?

提前致谢!

在你的 C++ 文件中你有

 // [[Rcpp::depends(RcppArmadillo)]]

对于Rcpp::sourceCpp(),这会为要找到的RcppArmadillo 的头文件设置必要的编译器标志。这在包中没有相同的效果。为了将其打包,您需要在 DESCRIPTION:

LinkingTo 中包含 RcppArmadillo
LinkingTo: Rcpp, RcppArmadillo

顺便说一句,获得正确结构的简单方法是使用

RcppArmadillo::RcppArmadillo.package.skeleton()

用于设置包框架。