Wednesday, 6 July 2022

Why R codes doesn't work through API in ubuntu?

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



from Why R codes doesn't work through API in ubuntu?

No comments:

Post a Comment