[R] moving onto returning a data.frame?
Jeff D. Hamann
jeff_hamann at hamanndonald.com
Wed May 21 02:47:35 CEST 2003
I've been studying some of the code and I'm still a little shakey on the
proper method for returning a data.frame from a C function (which is my
ultimate goal here). I've started some code that I've "stolen" from the
archives and I'm running into crashes, etc. I've been trying to gleen some
insight from the src/main/scan.c file and didn't find many comments in the
code that would benifit a newbie.
Does anyone have a good example (from some of the packages?) for returning a
data.frame. I'm trying to start my function so that it takes a data.frame as
an argument and returns a data.frame (post-hocus-pocus).
Here's my function so far...
SEXP testfunction3(
SEXP m_in )
{
int *mdims, n, p, i;
double *mm;
SEXP m_out;
SEXP nms;
if( !isMatrix( m_in ) )
{
error("m_in must be a matrix");
}
mdims = INTEGER( GET_DIM( m_in ) );
n = mdims[0];
p = mdims[1];
PROTECT( m_out = NEW_NUMERIC( p ) );
PROTECT( m_in = AS_NUMERIC( m_in ) );
PROTECT( nms = GET_COLNAMES( GET_DIMNAMES( m_in ) ) );
/* here you'll disect the incoming data.frame into the vectors you'll
pass into your simulation code */
/* get the vectors based on the column names to make sure the sequence
isn't important */
/* crunch, crunch, crunch */
/* assign the results into the outgoing data.frame which will have the
same dimensions as the incoming frame */
if( !isNull( nms ) )
{
namesgets( m_out, nms );
}
UNPROTECT( 3 );
return m_out;
}
Thanks,
Jeff.
More information about the R-help
mailing list