rpy2:如何在 python flask restful 中调用带有参数的函数?
rpy2: how to call function with parameter in python flask restful?
在 flask-restplus 中,我尝试使用来自 Python 的参数调用 R 函数,其中一个参数来自 POST 请求 JSON 主体。为此,首先我在 python 中加载 R 函数并将 json 数据作为参数传递,但我最终遇到以下错误:
TypeError: 'ListVector' object is not callable
我不明白为什么,我没有从 rpy2
文档中找到很多关于函数调用的信息。有没有办法将参数传递给 R 函数并在 python 中调用该函数?谁能指出我任何可能的方法来做到这一点?有什么想法吗?
我目前尝试的代码最少 api
输入json数据:
{
"body": {
"sex": "M",
"feat_aa": {
"value": 12,
"machine": "AC"
},
"feat_bb": {
"value": 13,
"machine": "AB"
}
}
}
玩具R函数:
library(jsonlite)
my_func <- function(json_data){
qry=fromJSON(json_data)
data=qry$body
## do something
}
这里是最小烧瓶的主要代码api:
from flask import Flask, jsonify, request
from flask_restplus import Api, Namespace, Resource, fields
import rpy2
import rpy2.robjects as robjects
##
app = Flask(__name__)
api = Api(app)
ns = Namespace('hello')
features_attr = api.model('hello_world', {
'value': fields.Integer(required=True),
'machine': fields.String(required=True)
})
feat_root_objs = api.model('my machine', {
'sex': fields.String(required=True),
'features': fields.List(fields.Nested(features_attr, required=True))
})
@ns.route('/hello')
class helloWorld(Resource):
@ns.expect(feat_root_objs, validate=False)
def post(self):
myfunc = robjects.r.source("my_func.R")
param = request.get_json()
res = myfunc(['param'])
# res = myfunc(param)
return jsonify(res)
if __name__ == '__main__':
api.add_namespace(ns)
app.run(debug=True)
如何将 parameter/arguments 传递给 R 函数并从 python 进行函数调用?有没有想过要做到这一点?谢谢
更新
我也试过这个尝试:
def post(self):
myfunc = robjects.r.source("my_func.R")
param = request.get_json()
res = myfunc(param)
return jsonify({"output": res})
但我总是出现以下错误:
> [2020-04-29 12:47:02,104] ERROR in app: Exception on
> /Ed_features/match_ed [POST] Traceback (most recent call last): File
> "C:\Users\jyson\AppData\Local\Programs\Python\Python37\Lib\site-packages\flask\app.py",
> line 1832, in full_dispatch_request
> rv = self.dispatch_request() File "C:\Users\jyson\AppData\Local\Programs\Python\Python37\Lib\site-packages\flask\app.py",
> line 1818, in dispatch_request
> return self.view_functions[rule.endpoint](**req.view_args) File "C:\Users\jyson\AppData\Local\Programs\Python\Python37\Lib\site-packages\flask_restplus\api.py",
> line 309, in wrapper
> resp = resource(*args, **kwargs) File "C:\Users\jyson\AppData\Local\Programs\Python\Python37\Lib\site-packages\flask\views.py",
> line 88, in view
> return self.dispatch_request(*args, **kwargs) File "C:\Users\jyson\AppData\Local\Programs\Python\Python37\Lib\site-packages\flask_restplus\resource.py",
> line 44, in dispatch_request
> resp = meth(*args, **kwargs) File "C:\Users\jyson\match_api\imm_server\heylo_ed.py", line 58, in post
> res = myfunc(param) TypeError: 'ListVector' object is not callable
你成功了吗?
尝试:
r.source("my_func.R")
my_result=r('my_func')(param)
或
r.source("my_func.R")
functionR=r('my_func')
my_result=functionR(param)
在 flask-restplus 中,我尝试使用来自 Python 的参数调用 R 函数,其中一个参数来自 POST 请求 JSON 主体。为此,首先我在 python 中加载 R 函数并将 json 数据作为参数传递,但我最终遇到以下错误:
TypeError: 'ListVector' object is not callable
我不明白为什么,我没有从 rpy2
文档中找到很多关于函数调用的信息。有没有办法将参数传递给 R 函数并在 python 中调用该函数?谁能指出我任何可能的方法来做到这一点?有什么想法吗?
我目前尝试的代码最少 api
输入json数据:
{
"body": {
"sex": "M",
"feat_aa": {
"value": 12,
"machine": "AC"
},
"feat_bb": {
"value": 13,
"machine": "AB"
}
}
}
玩具R函数:
library(jsonlite)
my_func <- function(json_data){
qry=fromJSON(json_data)
data=qry$body
## do something
}
这里是最小烧瓶的主要代码api:
from flask import Flask, jsonify, request
from flask_restplus import Api, Namespace, Resource, fields
import rpy2
import rpy2.robjects as robjects
##
app = Flask(__name__)
api = Api(app)
ns = Namespace('hello')
features_attr = api.model('hello_world', {
'value': fields.Integer(required=True),
'machine': fields.String(required=True)
})
feat_root_objs = api.model('my machine', {
'sex': fields.String(required=True),
'features': fields.List(fields.Nested(features_attr, required=True))
})
@ns.route('/hello')
class helloWorld(Resource):
@ns.expect(feat_root_objs, validate=False)
def post(self):
myfunc = robjects.r.source("my_func.R")
param = request.get_json()
res = myfunc(['param'])
# res = myfunc(param)
return jsonify(res)
if __name__ == '__main__':
api.add_namespace(ns)
app.run(debug=True)
如何将 parameter/arguments 传递给 R 函数并从 python 进行函数调用?有没有想过要做到这一点?谢谢
更新
我也试过这个尝试:
def post(self):
myfunc = robjects.r.source("my_func.R")
param = request.get_json()
res = myfunc(param)
return jsonify({"output": res})
但我总是出现以下错误:
> [2020-04-29 12:47:02,104] ERROR in app: Exception on
> /Ed_features/match_ed [POST] Traceback (most recent call last): File
> "C:\Users\jyson\AppData\Local\Programs\Python\Python37\Lib\site-packages\flask\app.py",
> line 1832, in full_dispatch_request
> rv = self.dispatch_request() File "C:\Users\jyson\AppData\Local\Programs\Python\Python37\Lib\site-packages\flask\app.py",
> line 1818, in dispatch_request
> return self.view_functions[rule.endpoint](**req.view_args) File "C:\Users\jyson\AppData\Local\Programs\Python\Python37\Lib\site-packages\flask_restplus\api.py",
> line 309, in wrapper
> resp = resource(*args, **kwargs) File "C:\Users\jyson\AppData\Local\Programs\Python\Python37\Lib\site-packages\flask\views.py",
> line 88, in view
> return self.dispatch_request(*args, **kwargs) File "C:\Users\jyson\AppData\Local\Programs\Python\Python37\Lib\site-packages\flask_restplus\resource.py",
> line 44, in dispatch_request
> resp = meth(*args, **kwargs) File "C:\Users\jyson\match_api\imm_server\heylo_ed.py", line 58, in post
> res = myfunc(param) TypeError: 'ListVector' object is not callable
你成功了吗? 尝试:
r.source("my_func.R")
my_result=r('my_func')(param)
或
r.source("my_func.R")
functionR=r('my_func')
my_result=functionR(param)