Building package with Rcpp, generated .h file missing header - r

I am currently building a package in RStudio that uses Rcpp. I have defined the following .cpp file, which works with 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;
}
I am following the directions from here and here. Next I call Rcpp::compileAttributes(). This produces the following files:
src/RcppExports.cpp
R/RcppExports.R
inst/include/mypackage.h
inst/include/mypackage_RcppExports.h
The generated mypackage_RcppExports.h file looks as follows:
// 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__
After attempting to build and reload the package, I receive the following errors (1):
../inst/include/gwassim_RcppExports.h:27:12: error: 'unordered_set' in
namespace 'std' does not name a type
And (2)
RcppExports.cpp:12:1: error: 'unordered_set' in namespace 'std' does
not name a type
I have limited C++ experience, but my sense is that these errors occur due to #include <unordered_set> being omitted. How do I get these automatically generated files to have the correct headers?
My sessionInfo is the following:
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)
And my version of g++ is 4.6.3, as included in the RTools package for Windows. I have enabled C++11 features with the following:
Sys.setenv("PKG_CXXFLAGS"="-std=c++0x").

That is a finicky one. I think you want Section 3.5.2 of the Rcpp Attributes vignette and this trick:
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).
In passing, I think I also convinced you to create a package rather than to rely just on sourceCpp() :)
Edit: Doh!! I overlooked the part that
std::unordered_set<int> traverse_cor(NumericMatrix x, float maxcor)
is probably not an automatically wrappable function. You may need to convert your set into a vector (or list or ...) to get one of the types that matches naturally into R.

Related

How to build a package with RcppArmadillo?

I wish to build a package, but I write part of it using RcppArmadillo and now I am suffering the consequences. I am using roxygen2 and devtools to help me with DESCRIPTION and NAMESPACE. I am coding in R / Ubuntu. In the DESCRIPTION I include two lines to load the packages:
Depends: R (>= 3.4.4), MASS (>= 7.3-49), Rcpp (>= 1.0.5), RcppArmadillo (>= 0.9.900.2.0)
LinkingTo: Rcpp, RcppArmadillo
and in the folder /src I write a script name loss_function.cpp, inside it there is:
> // [[Rcpp::depends(RcppArmadillo)]]
>
> #include <RcppArmadillo.h>
>
> using namespace Rcpp;
>
> //' Check function.
> //'
> //' #param x vector
> //' #param tau percentile
> //' #return y new vector
> // [[Rcpp::export(rho_koenker)]]
> arma::vec rho_koenker(arma::vec x, double tau){
> int n = x.n_elem;
> arma::vec y(n);
> for(int i = 0; i < n; ++i){
> if(x(i)<0){
> y(i) = x(i)*(tau-1);
> } else {
> y(i) = x(i)*tau;
> }
> }
> return(y);
> }
>
> //' Quantile regression loss function
> //'
> //' #param beta parameter
> //' #param x matrix
> //' #param y vector
> //' #param tau percentile
> //' #param N total number of observations
> //' #param d beta's length
> //' #return eta numeric
> // [[Rcpp::export(loss_qr)]]
> double loss_qr(arma::vec beta, arma::mat x, arma::vec y, double tau, int N, int d){
> double eta = 0;
> arma::vec res(N);
> arma::vec rho(N);
> res = y - (x * beta);
> rho = rho_koenker(res,tau);
> eta = accu(rho);
> return(eta);
> }
When I check the package (build -> check package) there comes an error msg:
Error in .Call("_pqfe_loss_qr", PACKAGE = "pqfe", beta, x, y, tau, N, :
"_pqfe_loss_qr" not available for .Call() for package "pqfe"
Calls: qr ... optim_qr -> <Anonymous> -> <Anonymous> -> fn -> .Call
Execution halted
Warning message:
Can't find generic `sew` in package knitr to register S3 method.
This message is only shown to developers using devtools.
Do you need to update knitr to the latest version?
As alluded in the comment above, it is not clear where you error would lie here with RcppArmadillo. So I
invoked RcppArmadillo.package.skeleton() (via the helper command kitten.r from littler relying on pkgKitten but those are details)
added in src/loss_function.cpp based on what you had
added one missing #export tag to you doxygen/roxygen to also have the R documentation created
called Rcpp::compileAttributes() (via helper compAttr.r from `littler) to have the interal glue code generated / updated
called roxygenize() (via helper roxy.r from littler) to have help files created
called R CMD build (directly, or via helper build.r from littler)
ran R CMD check (directly, or via helper rcc.r from littler)
and it all works:
edd#rob:~/git/stackoverflow/73405262(master)$ R CMD build soDemo
* checking for file ‘soDemo/DESCRIPTION’ ... OK
* preparing ‘soDemo’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* installing the package to process help pages
* saving partial Rd database
* cleaning src
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘soDemo_1.0.tar.gz’
edd#rob:~/git/stackoverflow/73405262(master)$
and checks nicely too
edd#rob:~/git/stackoverflow/73405262(master)$ R CMD check soDemo_1.0.tar.gz
* using log directory ‘/home/edd/git/stackoverflow/73405262/soDemo.Rcheck’
* using R version 4.2.1 (2022-06-23)
* using platform: x86_64-pc-linux-gnu (64-bit)
* using session charset: UTF-8
* checking for file ‘soDemo/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘soDemo’ version ‘1.0’
* package encoding: UTF-8
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘soDemo’ can be installed ... OK
* checking installed package size ... OK
* checking package directory ... OK
* checking DESCRIPTION meta-information ... OK
* checking top-level files ... OK
* checking for left-over files ... OK
* checking index information ... OK
* checking package subdirectories ... OK
* checking R files for non-ASCII characters ... OK
* checking R files for syntax errors ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking loading without being on the library search path ... OK
* checking dependencies in R code ... OK
* checking S3 generic/method consistency ... OK
* checking replacement functions ... OK
* checking foreign function calls ... OK
* checking R code for possible problems ... OK
* checking Rd files ... OK
* checking Rd metadata ... OK
* checking Rd cross-references ... OK
* checking for missing documentation entries ... OK
* checking for code/documentation mismatches ... OK
* checking Rd \usage sections ... OK
* checking Rd contents ... OK
* checking for unstated dependencies in examples ... OK
* checking line endings in C/C++/Fortran sources/headers ... OK
* checking line endings in Makefiles ... OK
* checking compilation flags in Makevars ... OK
* checking for GNU extensions in Makefiles ... OK
* checking for portable use of $(BLAS_LIBS) and $(LAPACK_LIBS) ... OK
* checking use of PKG_*FLAGS in Makefiles ... OK
* checking compilation flags used ... OK
* checking compiled code ... OK
* checking examples ... OK
* checking for unstated dependencies in ‘tests’ ... OK
* checking tests ...
Running ‘tinytest.R’
OK
* checking PDF version of manual ... OK
* DONE
Status: OK
edd#rob:~/git/stackoverflow/73405262(master)$
I put the tarball here if you want to take it from there.

stop application if pushed thread had exception in Perl

I have aplication which runs in parallel mode. Jobs are runing using threads with implmented subroutine. Subroutine "worker3" have three argumens, one parameter and two path for files.
This subroutine execute R script using system commands.
if ($ENV{"model"} eq "alaef_cy40_5km"){
sub worker3 {
my $parameter2 = $_[0];
print $parameter2, "\n";
$ENV{"grb_path"} = $models{$model}{"grb"}{"path"}{$parameter2};
$ENV{"grb_file"} = $models{$model}{"grb"}{"file"}{$parameter2};
print"FROM sub:", "\n";
print $ENV{"grb_path"}, "\n";
print $ENV{"grb_file"}, "\n";
say "Job Started\n";
system("Rscript $root/read_ALAEF.R #_ ");
}
for my $param( 'T2m','RH2m'){
push #threads, my $thr1=threads ->create('worker3', $param ,$ENV{"grb_file"},$ENV{"grb_path"})
}
$_->join() for threads->list();
}
Problem is when R script finish with Execution halted, application final status is ok. What i need is when R script finished with error, the whole program needs to be stopped.
So, if one thread fails, application needs to stops, and display error.
This is output:
T2m
FROM sub:
/data/nwp/products/a-laef_stream
A-LAEF_mem_{MBR2}_{YYYY}{MM}{DD}{HH}_surface.grb
Job Started
RH2m
FROM sub:
/data/nwp/products/a-laef_stream
A-LAEF_mem_{MBR2}_{YYYY}{MM}{DD}{HH}_surface.grb
Job Started
-- Attaching packages --------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.5 v purrr 0.3.4
v tibble 3.1.6 v dplyr 1.0.7
v tidyr 1.1.4 v stringr 1.4.0
v readr 2.1.0 v forcats 0.5.1
-- Attaching packages --------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.5 v purrr 0.3.4
v tibble 3.1.6 v dplyr 1.0.7
v tidyr 1.1.4 v stringr 1.4.0
v readr 2.1.0 v forcats 0.5.1
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
here() starts at /work/users/p6095/2022-02-19_00/harp.119
Loading required package: harpIO
Attaching package: 'harpIO'
The following object is masked from 'package:purrr':
accumulate
Loading required package: harpPoint
Loading required package: harpVis
Loading required package: shiny
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
here() starts at /work/users/p6095/2022-02-19_00/harp.119
Loading required package: harpIO
Attaching package: 'harpIO'
The following object is masked from 'package:purrr':
accumulate
Loading required package: harpPoint
Loading required package: harpVis
Loading required package: shiny
Loading required package: harpSpatial
Error: unexpected string constant in:
"
'4'"
Execution halted
Loading required package: harpSpatial
Error: unexpected string constant in:
"
'4'"
Execution halted
----------------------------------------------------
Application finished OK at: 21-02-2022 15:46:11 UTC
----------------------------------------------------
As you see, it shows that application finished OK, no matter if is the error inside R code. I need this : if pushed thread had exception -> stop application perl
Here is an example of how you can stop the main program with a status message if one of the executables run by a given thread fails:
First, I constructed a dummy executable foo.pl like this:
use feature qw(say);
use strict;
use warnings;
my ($id, $force_fail) = #ARGV;
my $thread1_exit_value = $force_fail ? 2 : 0;
if ($id == 1) {
sleep 2;
exit $thread1_exit_value;
}
elsif ($id == 2) {
sleep 5;
exit 0;
}
Then the main program like this:
use v5.20; # experimental signatures requires perl >= 5.20
use feature qw(say);
use strict;
use warnings;
use experimental qw(signatures);
use threads;
{
my #threads;
my $force_fail = 1;
my $start_time = time;
for (1..2) {
my $thr = threads->create(
sub {
my $res = system "foo.pl", $_, $force_fail; $res
}
);
push #threads, $thr;
}
my $fail = 0;
for my $i (0..$#threads) {
my $thr = $threads[$i];
if ($fail) {
say "detaching thread $i..";
$thr->detach();
}
else {
say "joining thread $i..";
my $res = $thr->join();
if (command_failed($res, $i)) {
$fail = 1;
}
}
}
my $total_time = time - $start_time;
say "Program " . ($fail ? "failed" : "succeeded") . " after $total_time seconds";
}
sub command_failed( $res, $id ) {
if ($res == -1) {
say "thread $id failed to execute: $!";
return 1;
}
elsif ($res & 127) {
printf "thread $id died from signal %d\n", $res & 127;
return 1;
}
else {
my $rcode = $res >> 8;
if ($rcode != 0) {
say "thread $id exited with return value $rcode";
return 1;
}
return 0;
}
}
When setting $force_fail = 1 on line 9 of the main script, the output is:
joining thread 0..
thread 0 exited with return value 2
detaching thread 1..
Program failed after 2 seconds
On the other hand, when setting $force_fail = 0 the output is:
joining thread 0..
joining thread 1..
Program succeeded after 5 seconds

Returning a user-defined structure from Rcpp in an R package

Rcpp is powerful and has worked great in most all cases, but I cannot figure out how to wrap a C-function that returns a user-defined structure into an R package.
DESCRIPTION
Package: myPackage
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Depends: R (>= 4.0.0)
License: GPL-3
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
Imports:
Rcpp
Suggests:
knitr,
rmarkdown,
testthat
VignetteBuilder: knitr
LinkingTo:
Rcpp
NAMESPACE
# Generated by roxygen2: do not edit by hand
importFrom(Rcpp,evalCpp)
export(read_header)
useDynLib(myPackage, .registration = TRUE)
Header File ("inst/include/myPackage_types.h")
#include <Rcpp.h>
namespace Rcpp {
typedef struct {
int my_data;
} MY_HEADER_INFO;
template <> SEXP wrap(const MY_HEADER_INFO& x) {
std::vector<std::string> names;
std::vector<SEXP> elements(1);
// do something with the elements and names
names.push_back("my_data");
elements[0] = Rcpp::wrap( x.my_data );
return 0;
};
}
C-code
/*
read_header.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <Rcpp.h>
#include "inst/include/myPackage_types.h"
namespace Rcpp {
// [[Rcpp::export]]
Rcpp::MY_HEADER_INFO read_header() {
Rcpp::MY_HEADER_INFO *header;
header = (Rcpp::MY_HEADER_INFO*)malloc(sizeof(Rcpp::MY_HEADER_INFO));
memset(header, 0, sizeof(Rcpp::MY_HEADER_INFO));
return *header;
}
}
When I source (i.e., compile) the C-code, I get the following:
Error in dyn.load("/private/var/folders/gl/jvj9b0xn34lgq6_h9370p8q80000gn/T/RtmpYRaBGW/sourceCpp-x86_64-apple-darwin17.0-1.0.4.6/sourcecpp_25a13fe3d7da/sourceCpp_49.so") :
unable to load shared object ...
When I try to build the package (CMD + Shift + B), I get:
clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include' -I/usr/local/include -fPIC -Wall -g -O2 -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
RcppExports.cpp:9:7: error: no type named 'MY_HEADER_INFO' in namespace 'Rcpp'
Rcpp::MY_HEADER_INFO read_header();
~~~~~~^
This seems so simple, but I can't find a similar vignetter or example and none of the variations I've tried seem to work. What is the cause of the dynamical loading error for SourceCpp? Why can't the compiler find the code in the header file?
Thanks!
I got this to work and have posted my solution to GitHub: git#github.com:markrbower/myPackage.git
The key parts are:
inst/include/myPackage_types.h
#include <RcppCommon.h>
namespace Rcpp {
typedef struct {
int my_data;
} MY_HEADER_INFO;
template <> SEXP wrap(const MY_HEADER_INFO& x);
template<> MY_HEADER_INFO* as(SEXP x);
}
read_header.cpp
/*
read_header.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "../inst/include/myPackage_types.h"
#include <RcppCommon.h>
namespace Rcpp {
template <> SEXP wrap(const MY_HEADER_INFO& x);
}
#include <Rcpp.h>
namespace Rcpp {
template <> SEXP wrap(const MY_HEADER_INFO& x) {
std::vector<std::string> names;
std::vector<SEXP> elements(1);
// do something with the elements and names
names.push_back("my_data");
elements[0] = wrap( x.my_data );
Rcpp::List result(elements.size());
for (size_t i = 0; i < elements.size(); ++i) {
result[i] = elements[i];
}
result.attr("names") = Rcpp::wrap(names);
// result can be return to R as a list
return( result );
};
}
//' #importFrom Rcpp evalCpp
//' #useDynLib myPackage
//' #export
// [[Rcpp::export]]
Rcpp::MY_HEADER_INFO read_header() {
Rcpp::MY_HEADER_INFO *header = NULL;
printf( "%ld\n", sizeof(Rcpp::MY_HEADER_INFO) );
header = (Rcpp::MY_HEADER_INFO*)malloc(sizeof(Rcpp::MY_HEADER_INFO));
memset(header, 0, sizeof(Rcpp::MY_HEADER_INFO));
header->my_data = 10;
return *header;
}
There were two problems. First, I had template definitions under the wrong Rcpp header (put the initial calls after RcppCommon.h and the detailed calls after Rcpp.h). The tutorials and examples warned me not to do that, but I did it, anyway. Second, I found that if you "source" the code and then "load" the library, the sourced code will obscure the library code and you will get a "null pointer" error. Running "devtools::check()" showed me that along with noting that the fix is to "rm" the sourced function.
Let me also add that there are two Roxygen comments that I needed to add to my .cpp file to get the appropriate commands to appear in my NAMESPACE file:
//' #importFrom Rcpp evalCpp
//' #useDynLib myPackage
I found an old post where Dirk suggested using the Rcpp.package.skeleton function to build a "baby" project and then slowly add things until you can do what you want. That is so much better than the approach I have been using: Start with a complex C program and try to shoehorn my code into Rcpp.

RcppTN .cpp script works when sourced but not when compiled in library

I am trying to create a R package that uses random draws from a truncated normal in a .cpp script. I am using the rtn1 function from the pckage RcppTN. If I source the code, the function works fine. Once I build the package I get the error:
> library(testtruncnorm)
> testtruncnorm()
Error in testtruncnorm::testtruncnorm() :
function 'RcppTN_rtn1' not provided by package 'RcppTN'
Simplified .cpp code is here
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppTN.h>
// [[Rcpp::depends(RcppTN)]]
#include<armadillo>
using namespace Rcpp;
//' draw truncated normal
//'
//' testtruncnorm()
//' #return returns 2 draws from a truncated normal
// [[Rcpp::export]]
arma::vec testtruncnorm()
{
arma::vec result = arma::ones(2);
result[1] = RcppTN::rtn1(1, 1, 0,HUGE_VAL);
result[2] = RcppTN::rtn1(1, 1, 0,HUGE_VAL);
return result;
}
My NAMESPACE file is
useDynLib(testtruncnorm, .registration=TRUE)
importFrom(Rcpp, evalCpp)
exportPattern("^[[:alpha:]]+")
My DESCRIPTION file is
Package: testtruncnorm
Type: Package
Title: What the Package Does Using Title Case
Version: 1.0
Date: 2018-10-23
Author: Your Name
Maintainer: Your Name <your#email.com>
Description: More details about what the package does. See
<http://cran.r-project.org/doc/manuals/r-release/R-exts.html#The-
DESCRIPTION-file>
for details on how to write this part.
License: GPL (>= 2)
Imports: Rcpp (>= 0.12.19), RcppTN
LinkingTo: Rcpp, RcppArmadillo, RcppTN
I am using RStudio create "R Package with RcppArmadillo" to get started. RStudio version 1.1.456. R version 3.5.1. Windows 10.
You have to make sure that RcppTN gets attached. You can do this using
importFrom(RcppTN, rtn)
in NAMESPACE. In the documentation for RcppTN it says that one should add
Depends: RcppTN
which should have the same effect.

Automate #usage documentation in Rcpp functions

How can I automate the documentation of the #usage section for Rcpp functions? In an ordinary R function within a package documented using roxygen2, the Usage section is added automatically. This is convenient for automatically documenting default arguments.
#' #title Hello world
#' #return Prints 'Hello world'
#' #export
#' #useDynLib RcppSandBox
hello <- function(x = 1) {
print("Hello, world!")
}
Hello world
Usage
hello(x = 1)
Value
Prints 'Hello world'
Yet when I write an analogous script with Rcpp, the documentation is produced but no #usage section is written.
//' Hello, Rcpp!
//' #name rcpp_hello
//' #param CharVec Print x or not.
//' #export
#include <Rcpp.h>
using namespace Rcpp;
// This is a simple function using Rcpp that creates an R list
// containing a character vector and a numeric vector.
//
// Learn more about how to use Rcpp at:
//
// http://www.rcpp.org/
// http://adv-r.had.co.nz/Rcpp.html
//
// and browse examples of code using Rcpp at:
//
// http://gallery.rcpp.org/
//
// [[Rcpp::export]]
List rcpp_hello(bool CharVec = true) {
CharacterVector x = CharacterVector::create("foo", "bar");
NumericVector y = NumericVector::create(0.0, 1.0);
List z = List::create(x, y);
if (CharVec) {
} else {
List z = List::create(y);
}
return z;
}
Hello, Rcpp!
Description
Hello, Rcpp!
Arguments
CharVec Print x or not.
I looked at the source code and documentation for dplyr::between, but there doesn't seem to be any special #usage section, yet the #usage section is present in the documentation.
I also looked at http://r-pkgs.had.co.nz/man.html and http://dirk.eddelbuettel.com/code/rcpp/Rcpp-attributes.pdf and Ctrl-F'd for usage, but found nothing relevant.
I know I can add //' #usage rcpp_hello(CharVec = TRUE) in this particular case but the advantage of letting roxygen2 automate the process is that I can change the default arguments.
DESCRIPTION file:
Package: RcppSandBox
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself#somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Encoding: UTF-8
LazyData: true
Imports: Rcpp (>= 0.12.10)
LinkingTo: Rcpp
RoxygenNote: 6.0.1
Session info:
> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252 LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RcppSandBox_0.1.0 dplyr_0.5.0 RevoUtilsMath_10.0.0
loaded via a namespace (and not attached):
[1] compiler_3.4.0 magrittr_1.5 R6_2.2.2 assertthat_0.2.0 RevoUtils_10.0.4 DBI_0.6-1 tools_3.4.0
[8] tibble_1.3.0 Rcpp_0.12.10
You haven't needed to use #usage since Roxygen2 2.0 and 3.0.0 for R or Rcpp. The issue you are running into is your roxygen tags are not directly above the Rcpp attributes tag...
#include <Rcpp.h>
//' Hello, Rcpp!
//' #param CharVec Print x or not.
//' #export
// [[Rcpp::export]]
Rcpp::List rcpp_hello(bool CharVec = true) {
Rcpp::CharacterVector x = Rcpp::CharacterVector::create("foo", "bar");
Rcpp::NumericVector y = Rcpp::NumericVector::create(0.0, 1.0);
Rcpp::List z = Rcpp::List::create(x, y);
if (CharVec) {
} else {
Rcpp::List z = Rcpp::List::create(y);
}
return z;
}
Try the above. This also means you do not need the #name tag either...

Resources