my R package uses Rcpp and RcppArmadillo. I built it in Rstudio. I am following H.Wickhams guide (R-packages). The package builds (with one warning, see below), installs and runs fine.
cpp scripts have been documented using the //' headers
I have several questions that i think are all related:
If i add //'@export
to a cpp file then an entry of export()
is added to NAMESPACE
. Shouldn't it be export(filename)?
The RcppExports.R
file contains a NULL value and during build i get a warning "Warning: RcppExports.R:18
: Missing name" . Why is this? How do i rectify it?
How do i read the help file for the cpp script. ?filename doesn't seem to work like it does for R files in the package?
EDIT: snippet of code from .cpp
//' @export
#include <RcppArmadillo.h>
// [[Rcpp::export]]
arma::mat myfunc(int nSize, ... ,arma::cube suitability) {
do some stuff
}
Code from RcppExports.R (This NULL is the warning mentioned above)
#' @export
NULL
myfunc <- function(nSize, ..., suitability) {
.Call('_myfunc', PACKAGE = 'mypackage', nSize, ... , suitability)
}
and code from 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);
}
SOLUTION : Thanks to Anders and 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
}
You need to have something like
//' @export
// [[Rcpp::export]]
right before your function definition. The first is Roxygen saying that the function is to be exposed to the user-interface. The second line is telling Rcpp to export to the R-side---ie the functions are simply put/exported into RcppExports.Rcpp
and RcppExports.R
. Two different things where the former only makes sense if the latter is provided too.
The Roxygen comments \\\\'
are just carried on to the RcppExports.R
file.
Edit: From your comments, I see you metion RcppArmadillo. You only need to #include <RcppArmadillo.h>
once at the top of your .cpp
file. The // [[Rcpp::depends(RcppArmadillo)]]
is not needed.
The roxygen
comment has to be next to the function(s) it applies to. Different combination of such comments are possible:
#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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.