On 2012-03-03 16:52, Fangyu He wrote:

Hi everyone,

I want to create a R function which can be used as a parameter in the function "outer", I tried the rternalize way which expose python function to R. However, it doesn't work, the following being the problems which I encountered.

    import rpy2.rinterface as ri
    seq = ri.globalenv.get("seq")

Here you'll get an error because R has not be initialized.

import rpy2.rinterface as ri
ri.initr()
seq = ri.globalenv.get("seq")


    outer = ri.globalenv.get("outer")

    #a1, a2, b1, b2 are a list which have only a element

Let's add them so the example is self-contained:
a1 = [1,]
a2 = [2,]
b1 = [3,]
b2 = [4,]

a = seq(ri.IntSexpVector(a1),ri.IntSexpVector(a2),length=ri.IntSexpVector([50,])) b = seq(ri.IntSexpVector(b1),ri.IntSexpVector(b2),length=ri.IntSexpVector([50,]))

    #create a python function with lambda and expose it to R
    funcp = lambda x, y: x+y

When at the rinterface level, one has to be explicit regarding a number of things. A lot of the magic guesswork at the robjects level is no longer there.

radd = ri.baseenv.get('+')
def funcp(x, y):
    return radd(x, y)


    funcp_f = ri.rternalize(funcp)
    z = outer(a, b , funcp_f)

Now it seems to be working.

>>> from rpy2.robjects.vectors import FloatVector
>>> FloatVector(z)
<FloatVector - Python:0x7fee6416aef0 / R:0x1ef7480>
[4.000000, 4.020408, 4.040816, ..., 5.959184, 5.979592, 6.000000]


Best,


L.




gives a Error in FUN(X, Y, ...):
unsupported oper and type(s) for +: 'rpy2.rinterface.SexpVector' and 'rpy2.rinterface.SexpVector'

The only example I can find of creating R function that I can find is creating a function having only one parameter, like this example:

    from  rpy2.robjects.vectors  import  FloatVector
    from  rpy2.robjects.packages  import  importr
    import  rpy2.rinterface  as  ri
    stats  =  importr('stats')

    # Rosenbrock Banana function as a cost function
    # (as in the R man page for optim())
    def  cost_f(x):
         x1  =  x[0]
         x2  =  x[1]
         return  100  *  (x2  -  x1  *  x1)**2  +  (1  -  x1)**2

    # wrap the function f so it can be exposed to R
    cost_fr  =  ri.rternalize(cost_f)

    # starting parameters
    start_params  =  FloatVector((-1.2,  1))

    # call R's optim() with our cost funtion
    res  =  stats.optim(start_params,  cost_fr)

So how can I get a function can be used in "outer"? Any words would be welcome.

Thanks!
Fangyu He









------------------------------------------------------------------------------
Virtualization&  Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/


_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to