Ah, I see the error. Please disregard this. I see how Rcpp::as works now.<div><br></div><div>Best,</div><div>Stephen<br><br><div class="gmail_quote">On Tue, Aug 7, 2012 at 2:08 PM, Stephen J. Barr <span dir="ltr"><<a href="mailto:stephenjbarr@gmail.com" target="_blank">stephenjbarr@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello again,<div><br></div><div>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?</div>

<div><br></div><div>
Thanks,</div><div>Stephen</div><div><br></div><div><div>//</div><div>// Stephen J. Barr</div><div>//   - trying to combine RInside and Eigen</div><div><br></div><div>#include <RInside.h></div><div>#include <Eigen/Dense></div>


<div><br></div><div><br></div><div>using namespace Eigen;</div><div>using namespace std;</div><div><br></div><div><br></div><div><br></div><div>int main(int argc, char *argv[]) {</div><div><br></div><div>    RInside R(argc, argv); </div>


<div>    int nrow = 3;</div><div>    int ncol = 4;</div><div>    R.parseEvalQ("set.seed(1)");</div><div>    MatrixXd mymat = MatrixXd(nrow, ncol);</div><div>    R.parseEvalQ("MYMAT = matrix(rnorm(3*12),nrow=3)");</div>


<div>    const Map<MatrixXd> mymat(as<Map<MatrixXd>>(MYMAT));</div><div><br></div><div><br></div><div>    exit(0);</div><div><div class="h5"><div><br></div><div>}</div><div><br></div><div><br><div class="gmail_quote">

On Tue, Aug 7, 2012 at 11:32 AM, Stephen J. Barr <span dir="ltr"><<a href="mailto:stephenjbarr@gmail.com" target="_blank">stephenjbarr@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thank you Dirk and also Douglas. This definitely helps. I think I can get this going. Thank you so much for the quick replies.<div>


<br>Best,<br>Stephen<div><div><br><br><div class="gmail_quote">On Tue, Aug 7, 2012 at 11:29 AM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Hi Stephen,<br>
<div><br>
On 7 August 2012 at 10:55, Stephen J. Barr wrote:<br>
| Greetings,<br>
|<br>
| I am new to Rcpp, RInside and the entire family of related packages. I am<br>
| experienced with R and also C++ using Eigen but I have never combined the two<br>
| before.<br>
<br>
</div>You are in a good starting position.<br>
<div><br>
| I would like to create a matrix using R and then play with it using<br>
| Eigen. From what I have read so far, it seems like I can use RInside to create<br>
| an Rcpp::NumericMatrix. <br>
<br>
</div>That works. Even one of the very oldest examples (rinside_sample1.cpp) does<br>
something related with a Rcpp::NumericMatrix (eg creates one and passes it around).<br>
<div><br>
| My question is this: to transform a matrix from a Rcpp::NumericMatrix to an<br>
| Eigen MatrixXd, do I use RcppEigen? Or is RcppEigen only for calling C++/Eigen<br>
| from R and not the other way around?<br>
<br>
</div>I think I have an idea as to what you are asking. I think it may help to step<br>
back for a second and try to imagine the big picture:<br>
<br>
a) Rcpp helps us with the interface defined by .Call:<br>
<br>
     SEXP .Call(string somefuncname, SEXP arg1, SEXP arg2, ...)<br>
<br>
   so everthing going in and out is a SEXP type.<br>
<br>
b) RInside lets you interchange with an embedded R process, again talking<br>
   SEXPs.<br>
<br>
c) Packages like RcppEigen provide wrappers to and from SEXPs.<br>
<br>
So if you don't need a Rcpp::NumericMatrix, don't create one.  Create an<br>
Eigen matrix, and use RcppEigen's wrappers to pass it throught the SEXP<br>
interfaces.  That should work just fine with RInside as well.<br>
<br>
You will have to expand the RInside Makefile to point to the RcppEigen<br>
headers as well, and then it should just work.<br>
<br>
Hope this helps,  Dirk<br>
<br>
<br>
| Thanks,<br>
| Stephen <br>
|<br>
| ----------------------------------------------------------------------<br>
<div><div>| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</div></div><span><font color="#888888">--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</font></span></blockquote></div><br></div></div></div>
</blockquote></div><br></div></div></div></div>
</blockquote></div><br></div>