首页 > 解决方案 > 使用 rpy2:找不到已安装的 R 包

问题描述

我正在尝试将 R 与带有 rpy2 的 Python 结合使用。更具体地说,我正在尝试使用其他人编写的一些使用 rpy2 的代码。我认为使用 rpy2 的代码是:

import rpy2.robjects as robjects

integrated_result = 0
r1=robjects.r
r1.source("integrate_result.R")
integrated_result = r1.integrate_result(filename_list = mask)

“integrate_result.R”以“library(BayesFactor)”开头,包含一个名为“integrate_result”的函数。当我尝试运行它时,我收到以下错误:

    R[write to console]: Error in library(BayesFactor) : there is no package called 'BayesFactor'

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "C:\Users\XYZ\AppData\Local\Programs\Python\Python37\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\XYZ\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\memorymanagement.py", line 51, in rmemory
    yield pt
  File "C:\Users\XYZ\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface.py", line 680, in __call__
    raise embedded.RRuntimeError(_rinterface._geterrmessage())
rpy2.rinterface_lib.embedded.RRuntimeError: Error in library(BayesFactor) : there is no package called 'BayesFactor'

当我使用 R 本身时,安装包。我还尝试使用 rpy2 通过以下方式安装它:

>>> import rpy2.robjects.packages as r
>>> utils = r.importr("utils")
>>> package_name = "BayesFactor"
>>> utils.install_packages(package_name)
R[write to console]: Installing package into 'C:/Users/XYZ/Documents/R/win-library/3.6'
(as 'lib' is unspecified)

--- Please select a CRAN mirror for use in this session ---
R[write to console]: trying URL 'https://mirror.dogado.de/cran/bin/windows/contrib/3.6/BayesFactor_0.9.12-4.2.zip'

R[write to console]: Content type 'application/zip'
R[write to console]:  length 5099855 bytes (4.9 MB)

R[write to console]: downloaded 4.9 MB

package 'BayesFactor' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\XYZ\AppData\Local\Temp\RtmpqiHnHK\downloaded_packages
<rpy2.rinterface_lib.sexp.NULLType object at 0x00000153A91A5F88> [RTYPES.NILSXP]

这是 rpy2.situation 的输出:

python -m rpy2.situation
rpy2 version:
3.4.3
Python version:
3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [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-3.6.3
    Environment variable R_USER: None
    Environment variable R_LIBS_USER: None
R version:
R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
Copyright (C) 2020 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/.

    In the PATH:
    Loading R library from rpy2: OK
Additional directories to load R packages from:
None
C extension compilation:
'sh' is not recognized as an internal or external command,
operable program or batch file.
    Warning: Unable to get R compilation flags.

有没有人有任何想法?

标签: pythonrrpy2

解决方案


好吧,这是一件非常愚蠢的事情:

R 在我的计算机上的两个位置安装我的包:在 Documents 中的 R 文件夹中和在 Program Files 中的实际程序文件夹中。您可以像这样检查您的位置:

> .libPaths()
[1] "C:/Users/XYZ/Documents/R/win-library/3.6"
[2] "C:/Program Files/R/R-3.6.3/library"   

rpy2 只能使用安装在位置 [2] 中的软件包。

我只是将所有内容从 [1] 移动到 [2]。现在它起作用了!


推荐阅读