Skip to content

moving onto returning a data.frame?

3 messages · Jeff D. Hamann, Joe Conway, Thomas Lumley

#
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.
#
Jeff D. Hamann wrote:
I found this to be quite the challenge also. See pg_tuple_get_r_frame() 
in the file pg_conversion.c from PL/R. You can grab a copy here:
http://www.joeconway.com/

If you have any questions after looking through that, contact me off 
list and I'll try to help. That said, there's no guarantee my code is 
correct, but at least it seems to work in all of my use-cases so far ;-)

HTH,

Joe
#
On Tue, 20 May 2003, Jeff D. Hamann wrote:
read.dta in the foreign package springs to mind since I updated it
yesterday.  There are probably many others.
You are creating a vector of p reals here, not a data frame. If you know
the input and output have the same sizes and types it might be easiest to
use
    m_out = duplicate(m_in)
though this is not maximally efficient.
-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle