Take the 2-minute tour ×

I've been playing around with setting up an R package which aims to make use of Rcpp in RStudio, but I'm struggling to get things to work properly with Rcpp Attributes.

My understanding of how this works is fairly tenuous, but my understanding is as follows:

  1. In the source C++ files, you can add Rcpp Attributes, for example the tag // [[Rcpp::export]] marks a C++ function for exporting, making it available for R.
  2. When you do Build the package, Rcpp then generates the appropriate C++ code in the file RcppExports.cpp, and wrapper functions in the R source file RcppExports.R.

This doesn't seem to be working properly (as I expect) when I build my package. Roxygen isn't playing nicely with this when generating the NAMESPACE file (so i've disabled that). The tag // [[Rcpp::export]] seems to just mark the function for exporting to R, rather than also marking a function for being exported to the package Namespace.

More importantly, the Rcpp Attribute tag // [[Rcpp::depends()]] isn't being handled correctly. If I copy the code here into a new source file, and rebuild my package, gcc throws errors on the RcppExports.cpp file saying that the BigMatrix identifier is undeclared indicating that the attribute tage // [[Rcpp::depends(bigmemory)]] isn't being handled correctly.

Since multiple things aren't working as I would expect, What am I missing in my understanding of Rcpp Attribute tags?

share|improve this question
You could just write your function interface by hand, rather than relying on Rcpp Attributes and its compileAttributes() helper. After all, you do have a special case here (== need for special header). –  Dirk Eddelbuettel Aug 26 '13 at 19:22
add comment

2 Answers

up vote 3 down vote accepted

This is an issue with the RcppExports.cpp file that is generated. At the moment, there is no way to teach it to include header files from somewhere else, so it just does not include bigmemory/MatrixAccessor.hpp.

A workaround is to do this:

#include <Rcpp.h>
#include <bigmemory/MatrixAccessor.hpp>

Rcpp::NumericVector BigColSums(Rcpp::XPtr<BigMatrix> pBigMat) {

    // Create the matrix accessor so we can get at the elements of the matrix.
    MatrixAccessor<double> ma(*pBigMat);

    // Create the vector we'll store the column sums in.
    Rcpp::NumericVector colSums(pBigMat->ncol());
    for (size_t i=0; i < pBigMat->ncol(); ++i)
        colSums[i] = std::accumulate(ma[i], ma[i]+pBigMat->nrow(), 0.0);
    return colSums;

// [[Rcpp::export]]
Rcpp::NumericVector BigColSums( SEXP pBigMat ){
    return  BigColSums( Rcpp::XPtr<BigMatrix>( pBigMat) ) ;   

So that you capture the type in your .cpp file and RcppExports.cpp only has to know about SEXP.

share|improve this answer
add comment

// [[Rcpp::export]] doesn't export functions to the package NAMESPACE, it just says "we should make this function available to R" -- it's still up to you on how you want to manage this function in your namespace.

Roxygen works to parse roxygen tags from source file comments; you'll have to include an //' @export tag in your .cpp source files, as partially described in 3.4 of the attributes vignette.

For your second problem, you still need to make sure bigmemory is in your Depends: and LinkingTo: sections of your DESCRIPTION file. I suppose one might prefer for // [[Rcpp::depends]] to automatically modify the package DESCRIPTION file, but it's easy enough to do yourself.

There was also a similar discussion here on the RStudio help boards.

share|improve this answer
Thanks, that's solved my problem with the NAMESPACE file, and explained some of my gaps in Roxygen knowledge! Adding @useDynLib bigmemory doesn't fix the issue however. I've also got bigmemory listed as both a dependency and a link in the DESCRIPTION file. –  Scott Ritchie Aug 26 '13 at 7:54
add comment

Your Answer


