1.安装[windows]
conda install rpy2
2.把R的路径添加在环境变量中
如果用pip install rpy2 报错了Tried to guess R's HOME but no R command in the PATH
2.具体调用可见教程
from rpy2.robjects import r
r('''f <- function(r){pi * r}''')
r.f(3)
如果要导入R中的包
from rpy2.robjects.packages import importr
r('''install.packages('hwriter')''') #安装包
r('''
hwriter:::hwrite.table(matrix(1:10, 2))
''')
3.备注
把Rdata.frame转化为pd.DataFrame https://stackoverflow.com/questions/20630121/pandas-how-to-convert-r-dataframe-back-to-pandas
from rpy2.robjects import pandas2ri
corr_df_in_R = pandas2ri.py2ri_pandasdataframe(pd.DataFrame(corr_matrix)) #py_df --> r_df