2
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

TylerH
  • 20,799
  • 66
  • 75
  • 101
Abdul Rehman
  • 167
  • 2
  • 14

5 Answers5

6

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)
Prasanth Rajendran
  • 4,570
  • 2
  • 42
  • 59
  • Suffering with this error !!!! NameError: name 'unicode' is not defined. Was getting this issue, It has been resolved through restart IDE. – Syed Hamza Hassan Dec 31 '21 at 11:57
1

The below code is working.

from pyensae.languages import rconverter
print(rconverter.r2python(rscript, pep8=True))
andy156
  • 11
  • 2
0

pyensae is no longer supported with python 2.7

Try pip3 install pyensae

Derek O
  • 16,770
  • 4
  • 24
  • 43
  • Now this error pops up ` from __builtin__ import unicode ModuleNotFoundError: No module named "__builtin__'' ` – Abdul Rehman Apr 05 '20 at 08:10
  • I believe the "builtin" module was renamed to "builtins" in Python 3. Find the file where the code "from builtin import unicode" is run, and change it to: "from builtins import unicode" Source: https://github.com/catboost/catboost/issues/953 – Derek O Apr 05 '20 at 08:20
  • `from builtins import unicode ImportError: cannot import name 'unicode' from 'builtins' (unknown location)` – Abdul Rehman Apr 05 '20 at 08:25
0

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.

M.Vu
  • 397
  • 2
  • 9
0

simple you have to decrease pyensae version,

first install below version pyensae

pip install pyensae==1.3.825

# you will get autopep8 error, so install autope8 below version

pip install autopep8==1.7.0

Then Run below code it will work

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.rconverter import r2python
print(r2python(rscript, pep8 = True))

Output:

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)