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")
outer = ri.globalenv.get("outer")
#a1, a2, b1, b2 are a list which have only a element
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
funcp_f = ri.rternalize(funcp)
z = outer(a, b , funcp_f)
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