【问题标题】:Python and R: how do you show a plot with Pyper in a Jupyter notebook?Python 和 R:如何在 Jupyter 笔记本中使用 Pyper 显示绘图?
【发布时间】:2016-12-04 16:30:25
【问题描述】:

我正在使用 Jupyter 创建报告。我的大部分代码都是用 Python 编写的,但我需要使用一些 R 功能。

我使用一个名为 Pyper 的包在 Python 中调用 R。它运作良好,但我无法弄清楚如何在 Jupiter 笔记本中显示用 R (通过 Pyper)制作的图。一切似乎都运行良好,但 Jupyter 并没有显示情节。

这是我的测试代码:

In [17]: from pyper import *
         r = R()
         r("library(TSA)")
         r("data(star)")
         r("periodogram(star)")

这是 Jupyter 的输出(没有周期图):

Out[17]: 'try({periodogram(star)})\n'

【问题讨论】:

  • pyperrpy2 是完全不同的项目。使用后者,在 jupyter notebook 中显示图形就像将 R 代码放在 %%R 块中一样简单。
  • 感谢@Igautier,我现在明白 pyper(在其当前版本中)可能无法像 rpy2 那样在 Jupyter 中显示绘图。因此,在尝试了不同的解决方案后,我找到了一种解决方法,我将其发布在下面。

标签: python r jupyter rpy2 pyper


【解决方案1】:

如果有人在使用 Pyper 并想向 Jupyter 添加绘图,我已经找到了一种解决方法:

from pyper import *
r = R()
r("library(TSA)")
r("data(star)") 

# Save the figure
r("png('rplot.png');periodogram(star);dev.off()")


# Upload the figure to Jupyter
from IPython.display import Image
Image("rplot.png",width=600,height=400)

【讨论】:

    猜你喜欢
    • 2020-07-22
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 2022-07-21
    • 2021-07-27
    • 1970-01-01
    相关资源
    最近更新 更多