[Rcpp-devel] clarification of interaction between RInside and RcppEigen

Stephen J. Barr stephenjbarr at gmail.com
Tue Aug 7 23:08:25 CEST 2012


Hello again,

I am just trying to straighten this out. Say I want to have the exact same
random matrix in C++ as I do in R using rnorm. Is what I am doing below on
the right track?

Thanks,
Stephen

//
// Stephen J. Barr
//   - trying to combine RInside and Eigen

#include <RInside.h>
#include <Eigen/Dense>


using namespace Eigen;
using namespace std;



int main(int argc, char *argv[]) {

    RInside R(argc, argv);
    int nrow = 3;
    int ncol = 4;
    R.parseEvalQ("set.seed(1)");
    MatrixXd mymat = MatrixXd(nrow, ncol);
    R.parseEvalQ("MYMAT = matrix(rnorm(3*12),nrow=3)");
    const Map<MatrixXd> mymat(as<Map<MatrixXd>>(MYMAT));


    exit(0);

}


On Tue, Aug 7, 2012 at 11:32 AM, Stephen J. Barr <stephenjbarr at gmail.com>wrote:

> Thank you Dirk and also Douglas. This definitely helps. I think I can get
> this going. Thank you so much for the quick replies.
>
> Best,
> Stephen
>
>
> On Tue, Aug 7, 2012 at 11:29 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
>>
>> Hi Stephen,
>>
>> On 7 August 2012 at 10:55, Stephen J. Barr wrote:
>> | Greetings,
>> |
>> | I am new to Rcpp, RInside and the entire family of related packages. I
>> am
>> | experienced with R and also C++ using Eigen but I have never combined
>> the two
>> | before.
>>
>> You are in a good starting position.
>>
>> | I would like to create a matrix using R and then play with it using
>> | Eigen. From what I have read so far, it seems like I can use RInside to
>> create
>> | an Rcpp::NumericMatrix.
>>
>> That works. Even one of the very oldest examples (rinside_sample1.cpp)
>> does
>> something related with a Rcpp::NumericMatrix (eg creates one and passes
>> it around).
>>
>> | My question is this: to transform a matrix from a Rcpp::NumericMatrix
>> to an
>> | Eigen MatrixXd, do I use RcppEigen? Or is RcppEigen only for calling
>> C++/Eigen
>> | from R and not the other way around?
>>
>> I think I have an idea as to what you are asking. I think it may help to
>> step
>> back for a second and try to imagine the big picture:
>>
>> a) Rcpp helps us with the interface defined by .Call:
>>
>>      SEXP .Call(string somefuncname, SEXP arg1, SEXP arg2, ...)
>>
>>    so everthing going in and out is a SEXP type.
>>
>> b) RInside lets you interchange with an embedded R process, again talking
>>    SEXPs.
>>
>> c) Packages like RcppEigen provide wrappers to and from SEXPs.
>>
>> So if you don't need a Rcpp::NumericMatrix, don't create one.  Create an
>> Eigen matrix, and use RcppEigen's wrappers to pass it throught the SEXP
>> interfaces.  That should work just fine with RInside as well.
>>
>> You will have to expand the RInside Makefile to point to the RcppEigen
>> headers as well, and then it should just work.
>>
>> Hope this helps,  Dirk
>>
>>
>> | Thanks,
>> | Stephen
>> |
>> | ----------------------------------------------------------------------
>> | _______________________________________________
>> | 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
>> --
>> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120807/e98ae0fc/attachment.html>


More information about the Rcpp-devel mailing list