Problems with Converting R Code to Python

Viewed 2725
from pyensae.languages import r2python

print(r2python(rscript, pep8=True))

I have problems converting filename.R into pythonfilename.py because these libraries are not useful for converting as it generates the error 'module not found' even if I installed that module using:

pip install pyensae

3 Answers

The following steps worked for me with the Python version Python 3.7.6.

  1. Upgrade your pip
python -m pip install --upgrade pip
  1. Install pyensae module
pip install pyensae
  1. Check your python console by executing below import
from pyensae.languages import r2python

If you are facing

ModuleNotFoundError: No module named 'antlr4'

or facing

ModuleNotFoundError: No module named 'builtin'

then execute the below command

pip install antlr4-python3-runtime

After the above steps, I could able to convert the R script to the python language

rscript = """
nb=function(y=1930){
debut=1816
MatDFemale=matrix(D$Female,nrow=111)
colnames(MatDFemale)=(debut+0):198
cly=(y-debut+1):111
deces=diag(MatDFemale[:,cly[cly%in%1:199]])
return(c(B$Female[B$Year==y],deces))}
"""
from pyensae.languages import r2python
print(r2python(rscript, pep8=True))

Console output

ANTLR runtime and generated code versions disagree: 4.9.1!=4.8 ANTLR runtime and generated code versions disagree: 4.9.1!=4.8 from python2r_helper import make_tuple

def nb(y=1930):
    debut = 1816
    MatDFemale = matrix(D . Female, nrow=111)
    colnames(MatDFemale) .set(range((debut + 0), 198))
    cly = range((y - debut + 1), 111)
    deces = diag(MatDFemale[:, cly[set(cly) & set(range(1, 199))]])
    return make_tuple(B . Female[B . Year == y], deces)

pyensae is no longer supported with python 2.7

Try pip3 install pyensae

I fixed this issue by using:

  1. python 3.7.6
  2. pip 22.1.2
  3. antlr4-python3-runtime 4.9

This answer comes from here.

Related