Rpy2 Error in Django – Conversion 'py2rpy' not defined for objects of type '<class 'str'>

Question:

I have never used R before, and am trying to call an R function from python using rpy2.

It works on a standalone python terminal, but not in Django.

But rpy2 does not seem to be able to convert python strings to r objects.

I am using a custom library given by a coworker and would like to know if the problem is on his side. Because I have seen other people on SO using the exact same method to run R functions from python.

Here’s the code

from config import celery_app
from rpy2.robjects.packages import importr
from pathlib import Path

import math
base = importr("base")
my_lib = importr("my_lib")

@celery_app.task()
def calculate_r_bindings(file_path):
    """
    
    """

    g = my_lib.fit_bindingCurves(input = file_path,tass = 200,tdiss = 530,conc_M = 1.2*math.exp(-7),generate_output = False)
    return g

I am using rpy2 version 3.5.5

Here’s the error:

File "/Users/user.ali/Projects/ProjectDev/project_env/lib/python3.9/site-packages/rpy2/robjects/conversion.py", line 240, in _py2rpy
    raise NotImplementedError(
NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'str'>'
Asked By: Mazhar Ali

||

Answers:

To anyone coming here with same problem. I fixed it by downgrading to rpy2==3.5.1 from rpy2==3.5.5

Answered By: Mazhar Ali