【问题标题】:rpy2: how to call function with parameter in python flask restful?rpy2:如何在 python 烧瓶中使用参数调用函数?
【发布时间】:2020-04-29 17:24:44
【问题描述】:

在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)

如何将参数/参数传递给 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

【问题讨论】:

  • 调用myfunc(['param']) 传递一个包含字符串param 的列表。你试过myfunc(param)。此外,由于您只发布了回溯的最后一行,因此其他人很难判断您的代码的哪一部分产生了异常。
  • @v25 是的,我试过了,但我得到了这个错误:TypeError: 'ListVector' object is not callable。我对这个问题感到绝望。您的帮助将不胜感激。
  • @v25 我粘贴了 python 抛出的异常列表。如何解决这个问题?你有想法吗?谢谢
  • 很抱歉,我帮不上忙。不过,我设法在more minimal example 中重新创建了它。我怀疑这个问题与 R 函数有关,而不是与 python 代码有关。随意使用它来重新发布/编辑。把烧瓶相关的东西拿出来可能会更好。
  • @v25 如何将参数传递给 r 函数?这样做的正确方法是什么?请问有什么想法吗?

标签: python r flask rpy2


【解决方案1】:

你成功了吗? 试试看:

r.source("my_func.R")
my_result=r('my_func')(param)

r.source("my_func.R")
functionR=r('my_func')
my_result=functionR(param)

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 2018-01-22
    • 2021-04-16
    • 2020-10-26
    • 2023-03-31
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    相关资源
    最近更新 更多