简体   繁体   English

使用 rpy2 的控制台输出在 Jupyter Notebook 中不起作用

[英]Console output using rpy2 does not work in Jupyter Notebook

Using Jupyter Notebook, when I execute a function from an R package (in my case Climatol) from a notebook that uses the R kernel, messages are displayed as output that report the procedures that are being done.使用 Jupyter Notebook,当我从使用 R 内核的笔记本执行 R 包(在我的情况下为 Climatol)中的函数时,消息显示为输出,报告正在执行的过程。 Nothing new.没什么新鲜的。

在此处输入图片说明

The code block used is this:使用的代码块是这样的:

library(maps)
library(mapdata)
library(climatol)

# Apply function (from R kernel)
homogen('Vel',2011,2012,tinc='6 hour',expl=TRUE)

Now, using the Python kernel from another notebook, when I call the same function through rpy2 applying the same parameters, I don't get the same messages that appear in the previous image.现在,使用另一个笔记本中的 Python 内核,当我通过rpy2应用相同的参数调用相同的函数时,我没有得到与rpy2显示的相同的消息。 Instead I get this:相反,我得到了这个: 在此处输入图片说明

This time, the code block used is this:这次使用的代码块是这样的:

from rpy2.robjects import r
from rpy2.robjects.packages import importr

importr('maps')
importr('mapdata')
importr('climatol')

# Apply function ( from Python kernel)
r["homogen"]("Vel",2011,2012,tinc="6 hour",expl=r['as.logical']("T"))

I ran the mentioned Python code from Sublime Text and in this case the messages are displayed:我从 Sublime Text 运行了上面提到的 Python 代码,在这种情况下显示了消息: 在此处输入图片说明

The messages are also displayed when running the code from the Windows console, which leads me to think that the downside is Jupyter.从 Windows 控制台运行代码时也会显示这些消息,这让我认为缺点是 Jupyter。 That being said, how can I get those messages using Jupyter?话虽如此,我如何使用 Jupyter 获取这些消息?

I'm using Python 3.7 and the version of rpy2 is 2.9.4我使用的是 Python 3.7,rpy2 的版本是 2.9.4

Thanks for the help.谢谢您的帮助。

rpy2 it not fully supported on Windows. rpy2 它在 Windows 上不完全支持。 Callbacks used to set how R output is handled are likely not working, which results in the issue you are observing.用于设置如何处理 R 输出的回调可能不起作用,这会导致您观察到的问题。

If this improves in will be in more recent rpy2 versions.如果这在更新的 rpy2 版本中有所改进。 The latest rpy2 release is 3.3.6 for example.例如,最新的 rpy2 版本是 3.3.6。

Otherwise, for better compatibility on Windows consider running rpy2/jupyter in Docker, or if it works in a WSL (Windows Subsystem for Linux).否则,为了在 Windows 上获得更好的兼容性,请考虑在 Docker 中运行 rpy2/jupyter,或者如果它在 WSL(Linux 的 Windows 子系统)中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM