使用rpy2时找不到已安装的R包

4

我想使用rpy2将R和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.

有人有什么想法吗?


1
你说的“我试图加载它”是什么意思?请发布实际代码。而使用 utils.install_packages(package_name) 的代码片段的结果是什么? - desertnaut
1
包括 python -m rpy2.situation 可能会帮助其他人回答你的问题 :) - krassowski
1个回答

6

好的,这是一件相当愚蠢的事情:

R会将我的软件包安装在电脑上的两个位置:一个在“文档”文件夹下的R文件夹中,另一个在Program Files文件夹下的实际程序文件夹中。你可以像这样检查你的位置:

> .libPaths()
[1] "C:/Users/XYZ/Documents/R/win-library/3.6"
[2] "C:/Program Files/R/R-3.6.3/library"   
<我只是把一切从[1]移动到[2]。现在它能正常工作了!

1
我有两个关于你的解决方案的问题:1. 你是简单地从一个地方复制代码到另一个地方,还是使用了R命令/指令?2. 你知道任何命令/指令可以告诉R只使用其中的一个地方吗?提前感谢(顺便说一句:我正在使用macOS,而不是Windows)。 - pdeli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接