以下步骤适用于 Python 版本 Python 3.7.6。
- 升级你的
pip
python -m pip install --upgrade pip
- 安装
pyensae模块
pip install pyensae
- 通过执行以下导入检查您的 python 控制台
from pyensae.languages import r2python
如果你面对
ModuleNotFoundError: 没有名为“antlr4”的模块
或面对
ModuleNotFoundError: 没有名为“builtin”的模块
然后执行下面的命令
pip install antlr4-python3-runtime
经过上述步骤,我可以将R脚本转换为python语言
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))
控制台输出
ANTLR 运行时和生成的代码版本不一致:4.9.1!=4.8 ANTLR
运行时和生成的代码版本不一致:4.9.1!=4.8 from
python2r_helper 导入 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)