繁体   English   中英

R包中的c ++文档

[英]c++ documentation in an R package

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

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

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

  1. 如果我将//'@export添加到cpp文件,则将export()条目添加到NAMESPACE 它不应该是export(filename)吗?

  2. RcppExports.R文件包含NULL值,并且在生成过程中收到警告“警告: RcppExports.R:18 :名称丢失” 为什么是这样? 我该如何纠正?

  3. 我如何阅读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.RRcppExports.R文件中。

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

roxygen注释必须在其适用的功能旁边。 这些注释的不同组合是可能的:

#include <RcppArmadillo.h>

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

//' @export
// [[Rcpp::export]]
arma::mat myotherfunc(int nSize, ... ,arma::cube suitability) { 
   do some other stuff
}

// C++ internal function, i.e. usable in the packages C++ code
arma::mat cppinternal(int nSize, ... ,arma::cube suitability) { 
   do some other stuff
}

// R internal function, i.e. usable in the packages R code 
// [[Rcpp::export]]
arma::mat Rinternal(int nSize, ... ,arma::cube suitability) { 
   do some other stuff
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM