Jupyter 中的基本 rpy 失败

Basic rpy in Jupyter failing

提问人:EngrStudent 提问时间:8/9/2023 更新时间:9/18/2023 访问量:52

问:

我的想法
我不知道这是conda问题还是rpy问题。

背景
我正在尝试让 rpy 在 jupyter/python 中运行。

这是我正在使用的:

  • MS Windows 11 专业版
  • 蟒蛇 22.9.0
  • Python 3.7.16 中文文档
  • R-基地 3.6.1

我还安装了几个帮助程序包:

  • numpy 版本:1.23.5
  • Scipy 版本:1.10.0
  • OpenCV 版本:4.6.0
  • matplotlib 版本:3.6.2

我安装了 rpy2 版本 2.9.4

当我执行以下代码时:

import rpy2
import rpy2.situation
for row in rpy2.situation.iter_info():
    print(row)

我得到以下输出:

rpy2 version:
3.5.11
Python version:
3.9.16 (main, Jan 11 2023, 16:16:36) [MSC v.1916 64 bit (AMD64)]
Looking for R's HOME:
    Environment variable R_HOME: None
    InstallPath in the registry: C:\Program Files\R\R-4.2.2
    Environment variable R_USER: None
    Environment variable R_LIBS_USER: None
R version:
    In the PATH: R version 4.1.3 (2022-03-10) -- "One Push-Up"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.
    Loading R library from rpy2: OK
Additional directories to load R packages from:
None
C extension compilation:
    Warning: Unable to get R compilation flags.
Directory for the R shared library:

在这一点上,它给了我一长串错误,开头是这样的:

---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
Cell In[14], line 5
      2 import rpy2
      4 import rpy2.situation
----> 5 for row in rpy2.situation.iter_info():
      6     print(row)

并以此结束:

File c:\Users\username\.conda\envs\this_env_name\lib\subprocess.py:528, in run(input, capture_output, timeout, check, *popenargs, **kwargs)
    526     retcode = process.poll()
    527     if check and retcode:
--> 528         raise CalledProcessError(retcode, process.args,
    529                                  output=stdout, stderr=stderr)
    530 return CompletedProcess(process.args, retcode, stdout, stderr)

CalledProcessError: Command '('C:\\Program Files\\R\\R-4.2.2\\bin\\x64\\R', 'CMD', 'config', 'LIBnn')' returned non-zero exit status 1.

当我尝试运行这个时:

import rpy2.robjects

以下是产生错误的第一位和最后一位。

R[write to console]: Error in gettext(fmt, domain = domain, trim = trim) : 
  3 arguments passed to .Internal(gettext) which requires 2

---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
Cell In[16], line 4
      1 #
      2 import rpy2
----> 4 import rpy2.robjects


File c:\Users\username\.conda\envs\this_env_name\lib\site-packages\rpy2\rinterface.py:817, in SexpClosure.__call__(self, *args, **kwargs)
    810     res = rmemory.protect(
    811         openrlib.rlib.R_tryEval(
    812             call_r,
    813             call_context.__sexp__._cdata,
    814             error_occured)
    815     )
    816     if error_occured[0]:
--> 817         raise embedded.RRuntimeError(_rinterface._geterrmessage())
    818 return res

RRuntimeError: Error in gettext(fmt, domain = domain, trim = trim) : 
  3 arguments passed to .Internal(gettext) which requires 2

这些没有前进的道路,尽管最后一个似乎是相关的:

这个说使用 pip 而不是 conda,但这是毒害 conda 环境的好方法。它们可以一起被黑客入侵,但它们也可以相互破坏。
如何在python中正确安装rpy2?

观察结果
它应该使用的R应该是R-3.6左右,而不是R-4.2.2。


如何在不破坏 conda 的情况下让 rpy 在 jupyter 中运行?我只想让 python 调用一个 r-script,并让它在 python 工作区中的对象上运行,然后将一个新对象返回到 python 工作区。

python r jupyter-notebook jupyter rpy2

评论


答:

1赞 user11513145 9/16/2023 #1

在 rpy2 的文档中,它实际上说“目前不支持在 Windows 上运行 rpy2,尽管最近报告了 3.3.x 系列中最新的相对成功”。看起来您使用的是 Windows 机器,所以这就是问题所在。

文档链接以供参考