1

Initially, I was importing some data from R directly into a C++ function like so:

#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::depends(RcppArmadillo)]]

List gibbs(List dat) {
  int N = dat["N"];
  arma::vec y = dat["y"];
  ...
}

Eventually, I needed more than just N and y (the input_data list has many separate vectors and matrices I need to import). So, I thought it would be a good idea to create a class Data to store all the input variables and automatically do some pre-processing in the constructor. Something like

#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::depends(RcppArmadillo)]]

class Data {  
public:
  int N;  
  arma::vec y;
  // other data

  Data(List input_data) {
    N = input_data["N"];
    y = input_data["y"];
    // pre-processing steps
  }
};

However, this doesn't work. It gives the following error:

object_LocLev.cpp:12:7: error: use of overloaded operator '=' is ambiguous (with operand types 'arma::vec' (aka 'Col<double>') and 'Rcpp::Vector<19>::NameProxy' (aka 'generic_name_proxy<19>'))
y = input_data["y"];
~ ^ ~~~~~~~~~~~~~~~

My question is why does this error appear? Is there a better way of assigning y?

Cat
  • 111
  • 4
  • Hi, and welcome to StackOverflow and Rcpp(Armadillo). Your question is a little sprawling and short of focus -- when I find that a certain design doesn't work I usually try something else. Maybe have a look at some the over 800 CRAN packages using RcppArmadillo and see if you find a good idea somewhere? – Dirk Eddelbuettel Jul 16 '21 at 21:20
  • @DirkEddelbuettel I have removed the full context of the question to make it "more focused." I also quickly examined five RcppArmadillo-reliant packages that I'm familiar with, as you suggested. I noticed they tend to organize their data in a different way than this --- but I'm not sure that's a good thing. Granted, that's a small sample, and I'll keep looking, but I would like to make this structure work if possible. – Cat Jul 16 '21 at 21:37
  • 1
    The compiler and the underlying template programming for the conversion can be finicky. We know the list contains `SEXP` objects you you try `SEXP s = input_data["y"];` and then an explicit `arma::vec v = Rcpp::as(s);`. And so on. We wrote the helper functions to work on function interfaces and signatures, you now take them into the bodies of C++ classes so there may be friction. – Dirk Eddelbuettel Jul 16 '21 at 22:50

0 Answers0