<div dir="ltr">A simple proof of concept that you might find useful:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><div>#include <Rcpp.h></div><div>using namespace Rcpp;</div>
<div><br></div><div>class ListBuilder {</div><div><br></div><div>public:</div><div><br></div><div> ListBuilder() {};</div><div> ~ListBuilder() {};</div><div><br></div><div> inline ListBuilder& add(std::string name, SEXP x) {</div>
<div> names.push_back(name);</div><div> elements.push_back(x);</div><div> return *this;</div><div> }</div><div><br></div><div> inline operator List() const {</div><div> List result(elements.size());</div><div>
for (size_t i = 0; i < elements.size(); ++i) {</div><div> result[i] = elements[i];</div><div> }</div><div> result.attr("names") = wrap(names);</div><div> return result;</div><div> }</div><div>
<br></div><div> inline operator DataFrame() const {</div><div> List result = static_cast<List>(*this);</div><div> result.attr("class") = "data.frame";</div><div> result.attr("row.names") = IntegerVector::create(NA_INTEGER, XLENGTH(elements[0]));</div>
<div> return result;</div><div> }</div><div><br></div><div>private:</div><div><br></div><div> std::vector<std::string> names;</div><div> std::vector<SEXP> elements;</div><div><br></div><div> ListBuilder(ListBuilder const&) {};</div>
<div><br></div><div>};</div><div><br></div><div>// [[Rcpp::export]]</div><div>DataFrame test_builder(SEXP x, SEXP y, SEXP z) {</div><div> return ListBuilder()</div><div> .add("foo", x)</div><div> .add("bar", y)</div>
<div> .add("baz", z);</div><div>}</div><div><br></div><div>/*** R</div><div>test_builder(1:5, letters[1:5], rnorm(5))</div><div>*/</div></div></div><div><br></div></blockquote></div><div class="gmail_extra"><br>
<br><div class="gmail_quote">On Tue, Jul 8, 2014 at 2:49 PM, Kevin Ushey <span dir="ltr"><<a href="mailto:kevinushey@gmail.com" target="_blank">kevinushey@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Maybe we need a DataFrameBuilder class that someone could use like so:<br><br> DataFrame df = DataFrameBuilder::create()<br> .add("column 1", col1)<br> .add("column 2", col2)<br>
.add("column 3", col3);<br> <br>This could be made as a generic VectorBuilder<T> class as well.<div><br></div><div>Or maybe I've been writing too much Java lately...<br><br>Cheers,<br>Kevin</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div class="h5">On Tue, Jul 8, 2014 at 2:43 PM, Steffen Neumann <span dir="ltr"><<a href="mailto:sneumann@ipb-halle.de" target="_blank">sneumann@ipb-halle.de</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">Hi,<br>
<br>
thanks for the quick answer. I didn't find how to *easily*<br>
populate my list with named elements without fiddling<br>
with the names attribute, so I ended up creating<br>
two dataframes and then cbinding them from within C++<br>
<br>
Rcpp::Language("cbind", allScanHeaderInfo, allScanHeaderInfo2).eval() ;<br>
<br>
Yours,<br>
Steffen<br>
<div><br>
On Di, 2014-07-08 at 11:17 -0500, Dirk Eddelbuettel wrote:<br>
> On 8 July 2014 at 17:51, Steffen Neumann wrote:<br>
> | Hi,<br>
> |<br>
> | I (think I) need to create a DataFrame with more than 20 entries,<br>
> | because I want another slot in this file:<br>
> | <a href="https://github.com/sneumann/mzR/blob/master/src/RcppRamp.cpp#L221" target="_blank">https://github.com/sneumann/mzR/blob/master/src/RcppRamp.cpp#L221</a><br>
> |<br>
> | I stumbled upon a 2year old answer here:<br>
> |<br>
> | <a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-March/005430.html" target="_blank">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-March/005430.html</a><br>
> |<br>
> | Questions:<br>
> |<br>
> | 1) Should I avoid DataFrame here in first place ? I'd love to keep it<br>
> | 2) Is there a more elegant solution for >20 entries by now ?<br>
> | 3a) How do I create the mentioned "local header file with 21, 22, 23, ... \infty arguments as you need it."<br>
> | 3b) How and where do I #include that ? I need to fake a directory structure<br>
> | so that <Rcpp/DataFrame.h> picks up my version of #include <Rcpp/generated/DataFrame_generated.h> ?<br>
> | 4) Is that future proof ?!<br>
><br>
> No.<br>
><br>
> Can you 'fudge it' and just create a List with N elements, each containing a<br>
> DataFrame of 10 columns? You can then find them in R at the end ...<br>
><br>
> Come to think of it, a DataFrame is _almost_ the same as a list, so I would<br>
> just create a List with your desired 20+ elements and then just call<br>
> as.data.frame() on it.<br>
><br>
> Or in case all entries are numeric (you didn't say..) just take a matrix...<br>
><br>
> Dirk<br>
><br>
<br>
<br>
--<br>
</div><div><div>IPB Halle AG Massenspektrometrie & Bioinformatik<br>
Dr. Steffen Neumann <a href="http://www.IPB-Halle.DE" target="_blank">http://www.IPB-Halle.DE</a><br>
Weinberg 3 <a href="http://msbi.bic-gh.de" target="_blank">http://msbi.bic-gh.de</a><br>
06120 Halle Tel. <a href="tel:%2B49%20%280%29%20345%205582%20-%201470" value="+4934555821470" target="_blank">+49 (0) 345 5582 - 1470</a><br>
<a href="tel:%2B49%20%280%29%20345%205582%20-%200" value="+4934555820" target="_blank">+49 (0) 345 5582 - 0</a><br>
sneumann(at)IPB-Halle.DE Fax. <a href="tel:%2B49%20%280%29%20345%205582%20-%201409" value="+4934555821409" target="_blank">+49 (0) 345 5582 - 1409</a><br>
</div></div><br></div></div><div class="">_______________________________________________<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></blockquote></div><br></div>
</blockquote></div><br></div>