2

I have recently started experimenting with interfacing C++ with R and I am struggling with the following C++ function. Specifically I am trying to use sourceCpp with the following .cpp file

#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
#include <algorithm>

#include <Rcpp.h>
using namespace Rcpp;


// [[Rcpp::export]]


std::unordered_map<std::string,std::string> example_function(std::unordered_map<std::string,std::vector<std::string>> map_1, std::unordered_map<std::string,std::vector<std::string>> map_2)
{
  std::unordered_map<std::string, std::string> example_map;
 
  //Do some things to example_map and return it

  return example_map;
}

The error I am getting is on some line of Exporter.h and it states

no matching function for call to 'std::unordered_map<std::__cxx11::basic_string<char>, std::vector<std::__cxx11::basic_string<char> > >::unordered_map(SEXPREC*&)

invalid conversion from 'SEXP' {aka 'SEXPREC*'} to 'std::unordered_map<std::__cxx11::basic_string<char>, std::vector<std::__cxx11::basic_string<char> > >::size_type' {aka 'long unsigned int'} [-fpermissive]

My understanding of the issue is that C++ has no way to "translate" unordered_map<std::string, std::vector <std::string> > into R. I understand that I need to write to functions for this, one that turns C++ into R, and one that turns R into C++ but I do not know where to start. I would be grateful for your help.

Thanks in advance.

  • 1
    You’ve slightly misunderstood. Rather than writing *functions* to perform the conversion, you’d usually write a *wrapper type*. In fact, Rcpp incluges a generic pointer wrapper type which you can probably use for your purposes: [`Rcpp::XPtr`](https://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1XPtr.html). – Konrad Rudolph Mar 15 '22 at 16:26
  • Never tried myself, but in this [Rcpp-extending](https://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-extending.pdf) there is an example with custom class `Foo`, so probably what you need; – pptaszni Mar 15 '22 at 16:30
  • @KonradRudolph thanks for your comment, could you please expand your answer more? I am new to rcpp and I think I am missing some things. – John Duncan Mar 15 '22 at 16:51
  • @pptaszni thanks for your comment too. I had already found the pdf you attached, but I couldn't use what it says to my problem. It shouldn't be very hard but I am just not an experienced user with rcpp. – John Duncan Mar 15 '22 at 16:52

0 Answers0