1
votes

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").

1

1 Answers

4
votes

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.