Issue
I newly installed Ubuntu and I want to call R codes through Flask API, everything works in the terminal and simple Python Script like this one:
import rpy2.robjects as robjects
if __name__ == '__main__':
print("Mahdi: test")
robjects.r('''
library(zoo)
print("Mahdi")
''')
But when I call it through Flask API like this one:
import os
from flask import Flask
from dotenv import load_dotenv
from flask_restful import Api
import rpy2.robjects as robjects
app = Flask(__name__)
load_dotenv()
app.config[
'SQLALCHEMY_DATABASE_URI'] = f"postgresql://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@{os.getenv('DB_HOST')}/{os.getenv('DB_NAME')}"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# TODO It allow to your packages to show their exceptions
app.config["PROPAGATE_EXCEPTIONS"] = True
api = Api(app)
@app.route('/test')
def test():
print("Mahdi: test")
robjects.r('''
library(zoo)
print("Mahdi")
''')
return {"body": "Nothing"}, 200
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=2233)
I got this error:
NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>'
And I used conda 4.13.0 and pip 22.0.4 to run the project
Python version: 3.9.12
R version: 4.1.2
Ubuntu 22.04 LTS
Solution
You are probably using the latest version of rpy2 as of [ 21st May 2022 ], changelog.
Due to the changes for the low-level C-API, it is not that stable across different platforms, I suggest you downgrade the rpy2 package in your virtual environment.
Try running:
pip3 install rpy2==3.5.1
Answered By - khalid obaidi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.