前置要求
第一步 安装rpy2
1
| pip3.8 install rpy2 -i https://pypi.tuna.tsinghua.edu.cn/simple
|
1 2 3
| import os os.environ['R_HOME'] = '/home/rqzhang/zlliu/dev/R/R_s/lib64/R' # R的安装后含SVN-REVISION的目录 非bin目录 os.environ['R_USER'] = '/home/rqzhang/zlliu/dev/python38/lib/python3.8/site-packages/rpy2' # rpy2的安装目录
|
冷门编译好的python库
第二步 调用方法一(不推荐)
1 2 3 4 5 6
| from rpy2.robjects import r as Rcode from rpy2.robjects.packages import importr as Rrequire Rrequire('ggplot2') print(Rcode("pi"))
|
pandas.py: Comparison with R / R libraries
第三步 调用方法二(推荐)
1 2 3 4
| import pandas as pd data = pd.DataFrame({'x':[1,2,3,4,5,6],'y':[1,2,3,4,5,6]}) px = 1 py = 2
|
1 2 3 4
| %%R -i data -o colN require(ggplot2) colN = colnames(data) ggplot(data=data) + geom_point(aes(x=x,y=y))
|
1 2 3 4
| %R require(ggplot2) %R -i px,py z = %R px*py print(colN, z[0]
|
如何优雅地在Jupyter Notebook中同时运行R和IPython