[Rd] Manipulating R lists in C

Tom McCallum tom.mccallum at levelelimited.com
Fri Nov 17 15:29:33 CET 2006


Hi,

I have been studying the R manual on lists but cannot seem to create a  
simple R list in C - I keep on getting "'dimnames' applied to non-array"  
whenever I attempt to assign names to the list elements:

Wanted output a list structure something like
	[ type="Bid", price=2.0, volume=1000 ]

I can get up to a list of
	[ "Bid2, 2.0, 1000 ]

But for the life of me I can't assign the names to the list items - any  
suggestions?

Below is what I already have - any comments on things I don't really need  
to do as well in terms of creating extra string objects etc are also  
helpful.

Many thanks

Tom


======EXTRACT=========


   SEXP typeStr, volumeStr, priceStr, typeValStr;
   SEXP priceList;

   // create main list of vectors (unspecific items )
   PROTECT( priceList = allocVector( VECSXP, 3 ) );

   // for each item create a label and attach value
   PROTECT( typeValStr = allocVector( STRSXP, 1 ) );
   SET_STRING_ELT( typeValStr, 0,  
mkChar( getPriceDataTypeAsString( p.getData().getType() ).c_str() ) );
   SET_STRING_ELT( priceList, 0, typeValStr) ;

   SEXP priceSXP;
   PROTECT( priceSXP = allocVector( REALSXP, 1) );
   REAL(priceSXP)[0] = p.getData().getPrice();
   SET_VECTOR_ELT( priceList, 1, priceSXP);

   SEXP volumeSXP;
   PROTECT( volumeSXP = allocVector( REALSXP, 1 ) );
   REAL(volumeSXP)[0] = p.getData().getVolume();
   SET_VECTOR_ELT( priceList, 2, volumeSXP);

   ********WORKS UP TO HERE*************

   // add the names of the list items (type,price,volume)
   SEXP dimnames;
   PROTECT( dimnames = allocVector( VECSXP, 3 ) );

   // first create string objects
   PROTECT( typeStr = allocVector( STRSXP,1 ) );
   SET_STRING_ELT( typeStr, 0, mkChar("type") );

   PROTECT( priceStr = allocVector( STRSXP,1 ) );
   SET_STRING_ELT( priceStr, 0, mkChar("price") );

   PROTECT( volumeStr = allocVector( STRSXP,1 ) );
   SET_STRING_ELT( volumeStr, 0, mkChar("volume") );

   // assign string objects to vector
   SET_VECTOR_ELT( dimnames, 0, typeStr );
   SET_VECTOR_ELT( dimnames, 1, priceStr);
   SET_VECTOR_ELT( dimnames, 2, volumeStr );

   // assign vector to original list of data
   setAttrib( priceList, R_DimNamesSymbol, dimnames ); <----- I think this  
is wrong but I don;t know what it should be

===========END EXTRACT===============

-- 
Dr. Thomas McCallum
Systems Architect,
Level E Limited
ETTC, The King's Buildings
Mayfield Road,
Edinburgh EH9 3JL, UK
Work  +44 (0) 131 472 4813
Fax:  +44 (0) 131 472 4719
http://www.levelelimited.com
Email: tom at levelelimited.com

Level E is a limited company incorporated in Scotland. The c...{{dropped}}



More information about the R-devel mailing list