Von Der Hirschheydt, Juergen
2011-Apr-26 16:20 UTC
[R] Hook into Coercion Framework for data.frame
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}}
statconnDCOM, availagble from rcom.univie.ac.at is a (D)COM server accessing R. rcom (available from CRAN) is an R package which can turn R into a COM server or into a COM client. These tools have their own mailing list which can be subscribed at rcom.univie.ac.at. On 4/26/2011 6:20 PM, Von Der Hirschheydt, Juergen wrote:> 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}} > > ______________________________________________ > R-help at r-project.org 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. >