是的,这是可能的!使用 rpy2。
您可以使用以下方式安装 rpy2:pip install rpy2
然后在您的一个单元格中运行%load_ext rpy2.ipython。 (您只需运行一次。)
现在您可以执行以下操作:
Python 单元格:
# enables the %%R magic, not necessary if you've already done this
%load_ext rpy2.ipython
import pandas as pd
df = pd.DataFrame({
'cups_of_coffee': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
'productivity': [2, 5, 6, 8, 9, 8, 0, 1, 0, -1]
})
R 细胞:
%%R -i df -w 5 -h 5 --units in -r 200
# import df from global environment
# make default figure size 5 by 5 inches with 200 dpi resolution
install.packages("ggplot2", repos='http://cran.us.r-project.org', quiet=TRUE)
library(ggplot2)
ggplot(df, aes(x=cups_of_coffee, y=productivity)) + geom_line()
您将从 python Pandas DataFrame 中获得漂亮的图形数据。