[Rcpp-devel] Passing XPtr of Eigen Map object between functions

Charles Determan cdetermanjr at gmail.com
Fri Jun 26 16:11:58 CEST 2015


As per Dirk's suggestion about creating a trivial class, I managed to get
this to work.  Thank you for the suggestion Dirk, again sorry if I wasn't
initially clear.

class myEigen {
    public:
        myEigen(SEXP A_) : A(A_) {}

        MatrixXf matrix() {return as<MatrixXf>(A);}
    private:
        SEXP A;
};

// [[Rcpp::export]]
SEXP matrixIN(SEXP A)
{

    myEigen *C = new myEigen(A);
    XPtr<myEigen> pMat(C);
    return(pMat);
}

// [[Rcpp::export]]
void matrixOUT(XPtr<myEigen> ptrA, int nr, int nc)
{
    MatrixXf A = ptrA->matrix();
    std::cout << A << std::endl;
}

On Fri, Jun 26, 2015 at 8:54 AM, Charles Determan <cdetermanjr at gmail.com>
wrote:

> I apologize if I am not being clear.  I realize that R doesn't have float
> data types, however I am able to pass a matrix in as a MatrixXf object.  I
> would like to be able to refer to this MatrixXf object persistently.  Some
> general code to demonstrate my point.
>
> # pass R matrix in as MatrixXf object and return pointer to that object
> SEXP matrixIN(MatrixXf A)
> {
>     return XPtr<MatrixXf>(A);
> }
>
> # pass previously made pointer and return that matrix
> SEXP matrixOUT(XPtr<MatrixXfr> ptrA)
> {
>     MatrixXf A = *ptrA
>     return wrap(A)
> }
>
> On Fri, Jun 26, 2015 at 8:32 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
>>
>> On 26 June 2015 at 07:43, Charles Determan wrote:
>> | I have tried a few more feeble attempts (explicitly create pointer to
>> pass,
>> | passing just the MatrixXf object) at getting the Eigen object to persist
>> | between functions without the additional Armadillo wrapper but with no
>> | success.  Anyone have any additional thoughts?
>>
>> You are not making it very easy to help you as you do not clearly say what
>> you really want.  Some things you talk about don't exist (ie "float
>> matrix in
>> R"), others make no sense (XPtr of Eigen::Map when the latter already is a
>> proxy object).
>>
>> As for the question of persistence, the single simplest approach I often
>> use
>> it create a trivial class holding a member variable of the "thing" you
>> want
>> to persist (often a database or resource handle, could even be a honking
>> bigmemory object and shared handle) whichI create in the constructor, or
>> an
>> init() function, and release in the destructor.
>>
>> Dirk
>>
>> --
>> http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150626/c58cc50a/attachment.html>


More information about the Rcpp-devel mailing list