[R] Hook into Coercion Framework for data.frame

Von Der Hirschheydt, Juergen juergen.vonderhirschheydt at credit-suisse.com
Tue Apr 26 18:20:11 CEST 2011


Hi,

I am looking into a way to hook into the R coercion framework to allow
me to convert table-like data stored within a COM object into a
data.frame.

Some of our COM objects have their own table-like data storage, and from
R's point of view it's an object (EXTPTRSXP) decoarated with a sepcial
symbol so we can easily identify it.

	COM_Table <- a_method_that_creates_the_object()
	class(COM_TABLE)
	> "QS.IpdgRelation"

In addition, we have a method that transforms this object into a
data.frame:

	relation.to.data.frame <- function(obj) { ... }

A user can easily transform the above object into a data.frame by
calling:

	myDataFrame <- relation.to.data.frame(COM_Table)

That all works nice and well, but we'd like to take it a step further
and be able to call

	data.frame(COM_Table)		and/or
	as.data.frame(COM_Table)

To achieve the same coercion. I've tried to hook into the coercion by
using setAs :

	setOldClass ( "QS.IpdgRelation" )
	setAs ( "QS.IpdgRelation", "data.frame", function(from)
relation.to.data.frame(from) )

Which allows me to do

	as ( COM_Table, "data.frame" )

But I can't seem to get the last step to hook this coercion into the
data.frame() or as.data.frame() call. Is there a way to achieve this, or
am I chasing a red hering here ?

Any help appreciated,
Thanks a lot,

Juergen

=============================================================================== 
Please access the attached hyperlink for an important el...{{dropped:4}}



More information about the R-help mailing list