Question

I'm having some problem converting test in a data frame to something that rcpp can print to the screen. The relevant part of my data looks like this:

ID          MATURITY    CRNCY
ZZ001 Corp  1/1/2001    USD
ZZ002 Corp  1/1/2004    USD
ZZ003 Corp  1/1/2001    USD
ZZ004 Corp  1/1/2001    USD

In an error-checking function I want to be able to print out the ID column, so that I can record it and let the calculation proceed. Here's a minimal version of the code I've written.

void print_ID( DataFrame df ){
  Rcout << "ID" << as<CharacterVector>( df["ID"] ) << std::endl ;
      // Returns address
  Rcout << "ID" << as<std::string>( df["ID"] ) << std::endl ;
  Rcout << "ID" << as<char>( df["ID"] ) << std::endl ;
      // Error: expecting a string
}

Compilation goes fine, but the first line just returns the address of the df pointer, and the second and third yield an error message. Dereferencing the df object doesn't help, as it just gives a compilation error. Any suggestions?

Thanks!

Was it helpful?

Solution

For something like this, I would just call back to the R functions for summarizing an object. Rcpp makes this easy.

#include <Rcpp.h>
using namespace Rcpp;

Function structure("str");

// [[Rcpp::export]]
void RcppStr(SEXP x) {
  structure(x);
}

/*** R
RcppStr(data.frame(x=1:5, y=letters[1:5]))
*/

If you want more fine-grained control, you're going to have to write your own pretty printers (as Dirk said). But for something like this it would be worth leveraging what already exists in R -- print, head, tail, and so on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top