[Rcpp-devel] Maximum Columns in Rcpp DataFrame (revisited)

Kevin Ushey kevinushey at gmail.com
Wed Jul 9 00:13:19 CEST 2014


A simple proof of concept that you might find useful:

#include <Rcpp.h>
using namespace Rcpp;

class ListBuilder {

public:

  ListBuilder() {};
  ~ListBuilder() {};

  inline ListBuilder& add(std::string name, SEXP x) {
    names.push_back(name);
    elements.push_back(x);
    return *this;
  }

  inline operator List() const {
    List result(elements.size());
    for (size_t i = 0; i < elements.size(); ++i) {
      result[i] = elements[i];
    }
    result.attr("names") = wrap(names);
    return result;
  }

  inline operator DataFrame() const {
    List result = static_cast<List>(*this);
    result.attr("class") = "data.frame";
    result.attr("row.names") = IntegerVector::create(NA_INTEGER,
XLENGTH(elements[0]));
    return result;
  }

private:

  std::vector<std::string> names;
  std::vector<SEXP> elements;

  ListBuilder(ListBuilder const&) {};

};

// [[Rcpp::export]]
DataFrame test_builder(SEXP x, SEXP y, SEXP z) {
  return ListBuilder()
    .add("foo", x)
    .add("bar", y)
    .add("baz", z);
}

/*** R
test_builder(1:5, letters[1:5], rnorm(5))
*/



On Tue, Jul 8, 2014 at 2:49 PM, Kevin Ushey <kevinushey at gmail.com> wrote:

> Maybe we need a DataFrameBuilder class that someone could use like so:
>
>     DataFrame df = DataFrameBuilder::create()
>         .add("column 1", col1)
>         .add("column 2", col2)
>         .add("column 3", col3);
>
> This could be made as a generic VectorBuilder<T> class as well.
>
> Or maybe I've been writing too much Java lately...
>
> Cheers,
> Kevin
>
>
> On Tue, Jul 8, 2014 at 2:43 PM, Steffen Neumann <sneumann at ipb-halle.de>
> wrote:
>
>> Hi,
>>
>> thanks for the quick answer. I didn't find how to *easily*
>> populate my list with named elements without fiddling
>> with the names attribute, so I ended up creating
>> two dataframes and then cbinding them from within C++
>>
>> Rcpp::Language("cbind", allScanHeaderInfo, allScanHeaderInfo2).eval() ;
>>
>> Yours,
>> Steffen
>>
>> On Di, 2014-07-08 at 11:17 -0500, Dirk Eddelbuettel wrote:
>> > On 8 July 2014 at 17:51, Steffen Neumann wrote:
>> > | Hi,
>> > |
>> > | I (think I) need to create a DataFrame with more than 20 entries,
>> > | because I want another slot in this file:
>> > | https://github.com/sneumann/mzR/blob/master/src/RcppRamp.cpp#L221
>> > |
>> > | I stumbled upon a 2year old answer here:
>> > |
>> > |
>> http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-March/005430.html
>> > |
>> > | Questions:
>> > |
>> > | 1) Should I avoid DataFrame here in first place ? I'd love to keep it
>> > | 2) Is there a more elegant solution for >20 entries by now ?
>> > | 3a) How do I create the mentioned "local header file with 21, 22, 23,
>> ... \infty arguments as you need it."
>> > | 3b) How and where do I #include that ? I need to fake a directory
>> structure
>> > |     so that <Rcpp/DataFrame.h> picks up my version of #include
>> <Rcpp/generated/DataFrame_generated.h> ?
>> > | 4) Is that future proof ?!
>> >
>> > No.
>> >
>> > Can you 'fudge it' and just create a List with N elements, each
>> containing a
>> > DataFrame of 10 columns?  You can then find them in R at the end ...
>> >
>> > Come to think of it, a DataFrame is _almost_ the same as a list, so I
>> would
>> > just create a List with your desired 20+ elements and then just call
>> > as.data.frame() on it.
>> >
>> > Or in case all entries are numeric (you didn't say..) just take a
>> matrix...
>> >
>> > Dirk
>> >
>>
>>
>> --
>> IPB Halle                    AG Massenspektrometrie & Bioinformatik
>> Dr. Steffen Neumann          http://www.IPB-Halle.DE
>> Weinberg 3                   http://msbi.bic-gh.de
>> 06120 Halle                  Tel. +49 (0) 345 5582 - 1470
>>                                   +49 (0) 345 5582 - 0
>> sneumann(at)IPB-Halle.DE     Fax. +49 (0) 345 5582 - 1409
>>
>> _______________________________________________
>> 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
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140708/294cac00/attachment-0001.html>


More information about the Rcpp-devel mailing list