<div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
| Hi,<br>
| I need to share some data between my Python code and my c++ code, C++ does<br>
| not really have a lot of nice ideas like DataFrames. But if you save a<br>
| dataframe from Python into csv, you can readily read it using R. Csv is not<br>
| the best way to go, but it is a simple case.<br>
<br>
CSVs are indeed a terrible format, yet annoyingly common. Try binary<br>
alternatives if you can.<br></blockquote><div><br></div><div>I disagree with Dirk that CSV is "a terrible format" - it excels (hah) at human readability, is decently machine-readable and easily compressible, but certainly is inappropriate for many tasks that require efficiency.<br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
| I have generally been noticing as I google around, that R has a healthy and<br>
| seemingly growing list of packages that can be accessed by c++ code. From<br>
| c++, R does not look so bad to me, and I would like to get access to this<br>
| large library of native routines in R.<br>
|<br>
| First on the list, is that I hope to read a dataframe or something like it<br>
| from data in a file, and then transform that dataframe or other tabular<br>
| object into something I can use in my c++ code for linear algebra, like an<br>
| Armadillo matrix.<br>
|<br>
| So is there any native code in the R world that I can use to read a<br>
| dataframe from a file?<br></blockquote><div><br></div><div>First off, only use a data.frame if what you really want is a data.frame. Otherwise, stick with a matrix (or convert to one as early as possible).<br><br></div><div>* Data.frame = ordered collection of like-sized vectors, possibly of heterogeneous type.<br></div><div>* Matrix = ordered collection values, of known / fixed dimension, by default represented internally as columns of vectors in both R and armadillo (as in LAPACK).<br><br></div><div>In R, for modest-sized objects, going between these two types is "relatively seamless". But in C++/Rcpp, the underlying differences are more apparent to the user.  Matrices "just work" (e.g. easy construction of an "identical" armadillo object), whereas data.frames require some care and attention, and possibly extra object creation destruction.  When possible, stick with matrices.<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
|<br>
| I think Rcpp is really cool,<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">it might make me<br>
| a backdoor R user.<br></blockquote><div><br></div><div>I became a backdoor C++ programmer through C++.  +1 really cool.<br></div><div><br></div>I found Google Protocol Buffers absurdly useful for moving between R and cpp in complex projects.  It's well-documented, fast, encourages separate/good metadata documentation, and works smoothly for R, C++, and Python.   I never did use protobufs for  vector data, though. I did write some test code using repeated fields, but didn't get to the point of comfort there. For arrays of fixed dimension, I can imagine using one field 
per dimension to code that dimension's length, and then a final repeated
 field with the payload.  See below for example.<br><div><br><div>Question for Dirk (et al):<br></div><br>Has anyone used protobuf messages for, e.g., passing arrays? Any obvious downsides?  When I last googled, I didn't find much re protobuf repeated fields or Rcpp + protobufs...<br><br><br>// File PbTest.proto<br>syntax = "proto2";<br>package Array;<br>// see <a href="https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#fields">https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#fields</a><br><br>message a2d {<br>    optional uint32 dim1 = 10;<br>    optional uint32 dim2 = 20;<br>    // add more dims here<br>    //<br>    // numeric vector<br>    repeated float  payload = 50;<br>}<br><br></div><div>## File pbarray.R<br>library(RProtoBuf)<br>aa <- matrix(1:30, ncol=3) <br>bb <- new(P("Array.a2d", file='PbTest.proto'))<br>bb$dim1 <- dim(aa)[1]<br>bb$dim2 <- dim(aa)[2]<br>bb$add(field='payload', values=aa)<br><br>cat(as.character(bb))<br><br></div><div>best,<br></div><div>Christian<br></div><div><a href="http://www.x14n.org">http://www.x14n.org</a><br></div></div></div></div>