[Rpy] although different variable assigment, returns same value

2008-04-29 Thread volkan kepoglu
Dear list,

PythonWin 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32.
Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin' for
further copyright information.
>>>
>>> import rpy
>>>
>>> rpy.r.version # rpy-1.0.1-R-1.3.0-to-2.6.1-Numpy-win32-py2.5
{'status': '', 'major': '2', 'version.string': 'R version 2.6.1
(2007-11-26)', 'language': 'R',
'os': 'mingw32', 'svn rev': '43537', 'system': 'i386, mingw32', 'month':
'11', 'platform': 'i386-pc-mingw32',
'year': '2007', 'arch': 'i386', 'day': '26', 'minor': '6.1'}
>>>
>>> def a(b=0):
... f = open("c:/temp/easy.R", "w")
... f.write ("sigma <- " + str(b))
... f.close()
...
>>>
>>> a() #writes "sigma <- 0" in the file, easy.R
>>>
>>> def r():
... t = "c:/temp/easy.R"
... rpy.r.source(t)
... v = rpy.r.sigma
... return v
...
>>> r()
0.0
>>> a(5) # "sigma <- 5"
>>> r()
0.0
>>> # why? it should be 5.0, shouldn't be?
>>>

Is it a bug or,
Is there any explanation for this,
Or my mistake, any comment???
It is not a big deal. There are other several ways to return sigma,
but it becomes strange to me. That's why I am asking. Just curiosity.

Regards,

PHD Candidate
Volkan Kepoglu
-
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


[Rpy] SF.net SVN: rpy: [508] branches/rpy_nextgen/rpy

2008-04-29 Thread lgautier
Revision: 508
  http://rpy.svn.sourceforge.net/rpy/?rev=508&view=rev
Author:   lgautier
Date: 2008-04-29 02:03:05 -0700 (Tue, 29 Apr 2008)

Log Message:
---
robjects:
- classes for arrays, matrixes (and almost data.frame)
- more tests

rinterface:
- more tests

Modified Paths:
--
branches/rpy_nextgen/rpy/rinterface/tests/__init__.py
branches/rpy_nextgen/rpy/robjects/__init__.py
branches/rpy_nextgen/rpy/robjects/tests/__init__.py
branches/rpy_nextgen/rpy/robjects/tests/testRObject.py

Added Paths:
---
branches/rpy_nextgen/rpy/rinterface/tests/test_SexpClosure.py

Modified: branches/rpy_nextgen/rpy/rinterface/tests/__init__.py
===
--- branches/rpy_nextgen/rpy/rinterface/tests/__init__.py   2008-04-28 
19:58:14 UTC (rev 507)
+++ branches/rpy_nextgen/rpy/rinterface/tests/__init__.py   2008-04-29 
09:03:05 UTC (rev 508)
@@ -3,6 +3,7 @@
 import test_SexpVector
 import test_SexpEnvironment
 import test_Sexp
+import test_SexpClosure
 import test_SexpVectorNumeric
 
 
@@ -10,10 +11,12 @@
 suite_SexpVector = test_SexpVector.suite()
 suite_SexpEnvironment = test_SexpEnvironment.suite()
 suite_Sexp = test_Sexp.suite()
+suite_SexpClosure = test_SexpClosure.suite()
 suite_SexpVectorNumeric = test_SexpVectorNumeric.suite()
 alltests = unittest.TestSuite([suite_SexpVector, 
suite_SexpEnvironment, 
suite_Sexp, 
+   suite_SexpClosure,
suite_SexpVectorNumeric])
 return alltests
 

Added: branches/rpy_nextgen/rpy/rinterface/tests/test_SexpClosure.py
===
--- branches/rpy_nextgen/rpy/rinterface/tests/test_SexpClosure.py   
(rev 0)
+++ branches/rpy_nextgen/rpy/rinterface/tests/test_SexpClosure.py   
2008-04-29 09:03:05 UTC (rev 508)
@@ -0,0 +1,37 @@
+import unittest
+import rpy2.rinterface as rinterface
+
+try:
+#FIXME: can starting and stopping an embedded R be done several times ?
+rinterface.initEmbeddedR("foo", "--vanilla", "--no-save", "--quiet")
+except:
+pass
+
+
+class SexpClosureTestCase(unittest.TestCase):
+#def setUpt(self):
+#rinterface.initEmbeddedR("foo", "--no-save")
+
+#def tearDown(self):
+#rinterface.endEmbeddedR(1);
+
+def testNew(self):
+
+x = "a"
+self.assertRaises(ValueError, rinterface.SexpClosure, x)
+
+def testTypeof(self):
+sexp = rinterface.globalEnv.get("plot")
+self.assertEquals(sexp.typeof(), rinterface.CLOSXP)
+
+def testRError(self):
+sum = rinterface.baseNameSpaceEnv["sum"]
+letters = rinterface.baseNameSpaceEnv["letters"]
+self.assertRaises(RuntimeError, sum, letters)
+
+def suite():
+suite = unittest.TestLoader().loadTestsFromTestCase(SexpClosureTestCase)
+return suite
+
+if __name__ == '__main__':
+ unittest.main()


Property changes on: 
branches/rpy_nextgen/rpy/rinterface/tests/test_SexpClosure.py
___
Name: svn:eol-style
   + native

Modified: branches/rpy_nextgen/rpy/robjects/__init__.py
===
--- branches/rpy_nextgen/rpy/robjects/__init__.py   2008-04-28 19:58:14 UTC 
(rev 507)
+++ branches/rpy_nextgen/rpy/robjects/__init__.py   2008-04-29 09:03:05 UTC 
(rev 508)
@@ -106,6 +106,9 @@
 def do_slot(self, name):
 return self._sexp.do_slot(name)
 
+def rclass(self):
+return baseNameSpaceEnv["class"](self.getSexp())
+
 class Rvector(Robject):
 """ R vector-like object. Items in those instances can
be accessed with the method "__getitem__" ("[" operator),
@@ -176,8 +179,43 @@
 return res
 
 def __len__(self):
-return len(self._sexp)
+return len(self.getSexp())
 
+class RArray(Rvector):
+
+def __init__(self, o):
+super(RArray, self).__init__(o)
+if not r["is.array"](self.getSexp())[0]:
+raise(TypeError("The object must be reflecting an R array"))
+
+def __getattr__(self, name):
+if name == 'dim':
+res = r.dim(self.getSexp())
+res = mapperR2Py(res)
+return res
+
+def __setattr__(self, name, value):
+if name == 'dim':
+value = mapperPy2R
+res = r["dim<-"](value)
+
+class RMatrix(RArray):
+
+def nrow(self):
+""" Number of rows """
+return r.nrow(self.getSexp())
+
+def ncol(self):
+""" Number of columns """
+return r.nrow(self.getSexp())
+
+class DataFrame(Rvector):
+#FIXME: not implemented
+def __init__(self, o):
+raise(RuntimeError("Not implemented."))
+
+
+
 class Rfunction(Robject):
 """ An R function (aka "c

Re: [Rpy] making a data.frame with parameters in a fixed order

2008-04-29 Thread Gregory Warnes

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"] )

>>> df = makeDF( 1, 2, 3, names=["A","B","C"] )
>>> r.print_(df)
  A B C
1 1 2 3



-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="c1n1")

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='c1n1')")

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