[R] return tree from .Call

From: Sender <exonintron_at_gmail.com>
Date: Tue 22 Aug 2006 - 08:15:31 EST


Hello:

I was hoping to get some advice about how to return a tree (basically a linked list -- with each node containing a parent, left, and right node pointers) from a C routine back into R. Each node itself contains several attributes (a double, a char *, an int, and a void * )

Initially I was thinking I could just return to R a SEXP containing a pointer to the Root Node, but then realized that the pointer would be useless since C probably frees the memory after I leave the routine.

Since trees vary in length (or height) I need to be able to loop thru the tree until all Nodes have been visited and saved in a SEXP. I'm really new to handling R objects from within C, and converting and returning a large tree structure is daunting. Here is some rough code I was thinking about using to do this. Any suggestions or help will be greatly appreciated!

SEXP list ;

PROTECT( list = allocVector(VECSXP, tree->size) ) ;

for( i = 0; i < tree->size; ++i ){

     SEXP node, tree_double, tree_char, tree_int, tree_voidPTR ;

     PROTECT( tree_double = NEW_NUMERIC( tree->weight ) ) ;
     PROTECT( tree_char = NEW_CHARACTER( tree->name ) ) ;
     PROTECT( tree_int = NEW_INTEGER( tree->exons ) ) ;
     PROTECT( tree_voidPTR = NEW_CHARACTER( tree->data ) ) ;   ??? not sure
about this...
     PROTECT( node = allocVector( VECSXP, 4 ) ) ;
     SET_VECTOR_ELT( node, 0, tree_double) ;
     SET_VECTOR_ELT( node, 1, tree_char) ;
     SET_VECTOR_ELT( node, 2, tree_int) ;
     SET_VECTOR_ELT( node, 3, tree_voidPTR ) ;

     SET_VECTOR_ELT( list, i, node ) ;

}

UNPROTECT( tree->size * 4 ) ; ??? Tricky.. return( list ) ;

Look reasonable ?

THANKS !         [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue Aug 22 08:22:28 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 22 Aug 2006 - 18:21:48 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.