Hello Andrew,
The basic problem you are encountering is that python uses a
dictionary to store named parameters to function calls. Python's
dictionaries don't preserve order, just name-value correspondence.
As a consequence calling an R function with named arguments loses the
ordering. AFAIK, nothing can be done about that.
There are a couple of solutions. One, as you describe, is to
construct an R expression in a string and have R evaluate the
string. Another solution is to create the data frame object without
names, then add the names in a separate step. You can also construct
an empty data frame and add one column at a time to it. Finally, you
can create an R utility function do to the work you need.
As an example of the last approach consider:
f>>> from rpy import *
set_default_mode(NO_CONVERSION)
>>> set_default_mode(NO_CONVERSION)
>>>
>>>
>>>
>>> makeDF = r("""
... function( ..., names)
... {
... df <- data.frame(...)
... names(df) <- names
... df
... }
... """)
>>>
>>>
>>> makeDF( 1, 2, 3, names=["A","B","C"] )
<Robj object at 0x388130>
>>> df = makeDF( 1, 2, 3, names=["A","B","C"] )
>>> r.print_(df)
A B C
1 1 2 3
<Robj object at 0x388170>
-Greg
On Apr 28, 2008, at 5:29PM , Andrew Dalke wrote:
I'm having problems using RPy to create a data.frame which is passed
to an R function.
The problem is that I need to initialize the data.frame with the
parameters in a specified order. The normal way, of doing
r.data_frame(MW=120.3, NUM_C=5, SMILES="c1ccccn1")
does not work because the parameter order is not a well defined
property, while in R is seems to be fixed. That is, "MW" in this
case is the first parameter, "NUM_C" is the second, and "SMILES" is
the third.
This is important because some of the code I have to deal with
turns the data.frame input into a matrix.
I got things working the hard and wrong way. I did
r("data.frame(MW=120.3, NUM_C=5, SMILES='c1ccccn1')")
along with asking RPy to not convert the returned data.frame into a
dictionary.
That works, but I'm worried about proper escape rules. Eg, what
happens if the string contains a "'" character?
The only other trick I could think of would be to assign the
inputs to a temporary variable, at the Python side of thing (letting
RPy handle the escapes) then refer to the R variables inside of the
data.frame() function call.
I looked in the archive and online but I couldn't find a better
way to do this.
Do any of you all have any ideas?
Andrew
[EMAIL PROTECTED]
----------------------------------------------------------------------
---
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save
$100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://
java.sun.com/javaone
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list