[Rcpp-devel] Initiate NumericMatrix in class constructor from S4 slot

Simon Zehnder szehnder at uni-bonn.de
Wed Mar 6 08:32:18 CET 2013


Hi Romain,

first let me thank you for the very fast and informative answer!

I suppose, as SEXP is a pointer, that these attempts reuse the memory allocated in R, right? Further, I presume, that I proceed in the same way for lists from R to Rcpp, don't I?

In regard to the initialisation list:

I have in my class over 10 different objects, so up to now I refused to use the initialising list. But you are right, that in regard to performance (copying etc.) the
initialising list is indisputable. So I gonna use it. 

Thanks again Romain!

Best

Simon

On Mar 6, 2013, at 8:23 AM, Romain Francois <romain at r-enthusiasts.com> wrote:

> There is no constructor of Matrix tht takes a SlotProxy. You can do it in two steps perhaps. 
> 
> SEXP tmp = classS4.slot("M_R") ;
> M = tmp ; 
> 
> Or use a specific cast: 
> 
>> M((SEXP)classS4.slot("M_R"));
> 
> In any case, you should use an initialisation list in your constructor anyway, something like: 
> 
>> FirstClass::FirstClass(Rcpp::S4& classS4) : M( (SEXP)classS4.slot("M_R")) {}
> 
> Hiwever, i will add the missing constructor if this does not break anything else. 
> 
> Romain
> 
> Le 6 mars 2013 à 08:14, Simon Zehnder <szehnder at uni-bonn.de> a écrit :
> 
>> Dear Rcpp-Devels,
>> 
>> I am a little confused right now with the following setting:
>> 
>> I have a C++ class with for example the following header
>> 
>> #include <RcppArmadillo.h>
>> 
>> class FirstClass {
>> 
>>   public:
>>       Rcpp::NumericMatrix M;
>> 
>>       FirstClass(Rcpp::S4& classS4);
>>       ~FirstClass();
>> }
>> 
>> Then, in the .cc file I have:
>> 
>> #include <RcppArmadillo.h>
>> #include "FirstClass.h"
>> 
>> FirstClass::FirstClass(Rcpp::S4& classS4) {
>> 
>>   M(classS4.slot("M_R"));     // array M_R in R
>> };
>> 
>> FirstClass::~FirstClass(){};
>> 
>> Now, my compiler always gives me an error: 
>> 
>> error: no match for call to ‘(Rcpp::NumericMatrix {aka Rcpp::Matrix<14>}) (Rcpp::RObject::SlotProxy)’
>> 
>> 
>> Do I have to create the Matrix in the constructor differently? I want to reuse memory allocated in R. Further, could the error be triggered by M_R being an array in R? 
>> 
>> Best 
>> 
>> Simon
>> 
>> 
>> _______________________________________________
>> Rcpp-devel mailing list
>> Rcpp-devel at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel



More information about the Rcpp-devel mailing list