from pyensae.languages import r2python
print(r2python(rscript, pep8=True))
我在将 filename.R
转换为 pythonfilename.py
时遇到问题,因为这些库对转换没有用,因为即使我安装了它也会生成错误“找不到模块”该模块使用:
pip install pyensae
最佳答案
以下步骤适用于 Python 版本 Python 3.7.6
。
- 升级你的
pip
python -m pip install --upgrade pip
- 安装
pyensae
模块
pip install pyensae
- 通过执行下面的 import 检查你的 python 控制台
from pyensae.languages import r2python
如果你面对
ModuleNotFoundError: No module named 'antlr4'
或面向
ModuleNotFoundError: No module named '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 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)
关于python - 将 R 代码转换为 Python 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61039549/