[R] return a list of vectors from C?

Jeff D. Hamann jeff.hamann at forestinformatics.com
Wed Feb 18 18:40:28 CET 2004


I've been working on a shared library that will be called from R. The
functions pass several vectors in and out (residuals, parameters, etc) and I
would like to be able to return a list of objects. I'm familiar with return
single objects (vectors, etc) from a C function, but need a little help for
returning a list of objects.

My code so far looks something like:

/* this function will perform the same tasks as the previous */
/* and return a list of the values rather than just the  */
/* optimal values. It should return the optimization info as */
/* well as the residual values from the flikam function  */
SEXP testfunc2(
   SEXP *pq,
   SEXP *obs )
{

   int  i;
   double *pq_vect;
   double *obs_vect;

   SEXP  ret_val;

   PROTECT( pq = coerceVector( pq, REALSXP ) );
   PROTECT( obs = coerceVector( obs, REALSXP ) );

   pq_vect = REAL( pq );
   obs_vect = REAL( obs );


   /* call my functions for the results */


   /* generate the output list */
   PROTECT( ret_val = allocVector( VECSXP, NPQ ) );
   for( i = 0; i < NPQ; i++ )
   {
      //REAL(ret_val)[i] = XMIN[i];
      //PROTECT( ret_val = allocVector( VECSXP, NPQ ) );
      //SET_VECTOR_ELT( ret_val, i, 3.14159 );

      //SET_VECTOR_ELT( ret_val, i, pq );


      /* generate the output vector */
/*       PROTECT( ret_val = allocVector( REALSXP, NPQ ) ); */
/*       for( i = 0; i < NPQ; i++ ) */
/*       { */
/*   REAL(ret_val)[i] = XMIN[i]; */
/*       } */

   }

   UNPROTECT( 3 );

   return ret_val;

}


What package would be a good example of how to do this?

Jeff.

---
Jeff D. Hamann
Forest Informatics, Inc.
PO Box 1421
Corvallis, Oregon USA 97339-1421
(office) 541-754-1428
(cell) 541-740-5988
jeff.hamann at forestinformatics.com
www.forestinformatics.com




More information about the R-help mailing list