我的R包使用Rcpp和RcppArmadillo。我在Rstudio中构建它。
我正在遵循H.Wickhams指南(R包装)。该软件包会生成(带有一个警告,请参阅下文),安装并正常运行。

cpp脚本已使用//'标头记录

我有几个与我相关的问题:


如果我将//'@export添加到cpp文件,则将export()条目添加到NAMESPACE。它不应该是export(filename)吗?
RcppExports.R文件包含一个NULL值,在构建过程中,我收到警告“警告:RcppExports.R:18:缺少名称”。为什么是这样?我该如何纠正?
我如何阅读cpp脚本的帮助文件。文件名似乎不像软件包中的R文件那样起作用?


编辑:.cpp中的代码段

//' @export
#include <RcppArmadillo.h>
// [[Rcpp::export]]
arma::mat myfunc(int nSize, ... ,arma::cube suitability) {
   do some stuff
}


来自RcppExports.R的代码(此NULL是上面提到的警告)

#' @export
NULL
myfunc <- function(nSize, ..., suitability) {
   .Call('_myfunc', PACKAGE = 'mypackage', nSize, ... , suitability)
}


和来自RccpExports.cpp的代码

// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
#include <RcppArmadillo.h>
#include <Rcpp.h>

using namespace Rcpp;

// myfunc
arma::mat myfunc(int nSize, ..., arma::cube suitability);
RcppExport SEXP _myfunc(SEXP nSizeSEXP, SEXP suitabilitySEXP) {
BEGIN_RCPP
  Rcpp::RObject rcpp_result_gen;
  Rcpp::RNGScope rcpp_rngScope_gen;
  Rcpp::traits::input_parameter< int >::type nSize(nSizeSEXP);
  Rcpp::traits::input_parameter< arma::cube >::type suitability(suitabilitySEXP);
  rcpp_result_gen = Rcpp::wrap(myfunc(nSize, ..., suitability));
  return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
    {"_myfunc", (DL_FUNC) &_myfunc, 9},
   {NULL, NULL, 0}
};

RcppExport void R_init_mypackage(DllInfo *dll) {
   R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
   R_useDynamicSymbols(dll, FALSE);
}


解决方案:感谢Anders和Ralf

#include <RcppArmadillo.h>
//' calculates stuff
//'
//' Calculates the stuff
//'
//'@param nSize number of some stuff
//'@param p2
//'@param p3
//'@param p4
//'@param p5
//'@param p6
//'@param p7
//'@param p8
//'@param suitability 3D array
//' @export
// [[Rcpp::export]]
arma::mat myfunc(int nSize, ... ,arma::cube suitability) {
  do some stuff
}

最佳答案

你需要有类似的东西

//' @export
// [[Rcpp::export]]


就在您的函数定义之前。第一个是Roxygen,说该功能将公开给用户界面。第二行告诉Rcpp导出到R端-即这些功能只需放入/导出到RcppExports.RcppRcppExports.R中即可。只有在提供了后者的情况下,前两种才有意义。

Roxygen注释\\'仅保留在RcppExports.R文件中。

编辑:
从您的评论中,我看到您提到了RcppArmadillo。您只需在#include <RcppArmadillo.h>文件顶部进行一次.cpp。不需要// [[Rcpp::depends(RcppArmadillo)]]

关于c++ - R包中的c++文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53525348/

10-12 17:36