4

I am trying to use R in combination with Python with rpy2. More specifically, I am trying to use some code written by someone else that uses rpy2. I think the code that uses rpy2 is:

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" starts with "library(BayesFactor)" and contains one function called "integrate_result". When I try to run it, I get the following error:

    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'

When I use R itself the package is installed. I have also tried to install it using rpy2 by using the following:

>>> 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]

Here is the output of 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.

Does anyone have any ideas?

Max
  • 405
  • 2
  • 11
  • 1
    What do you mean "*I try to load it*"? Please post the actual code. And what is the result of the code snippet with `utils.install_packages(package_name)`? – desertnaut Apr 09 '21 at 23:02
  • 1
    Including `python -m rpy2.situation` might help others answer your question :) – krassowski Apr 10 '21 at 09:59

1 Answers1

6

Okay, it was a pretty stupid thing:

R installs my packages at two locations on my computer: in an R folder in the Documents and in the actual program folder in Program Files. You can check your locations like this:

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

rpy2 was only able to use the packages that were installed in location [2].

I simply moved everything from [1] to [2]. Now it works!

Max
  • 405
  • 2
  • 11
  • 1
    I have two questions regarding your solution: 1. did you simply copy the code from one place to the other or did you use an R command/instruction? 2. Do you know of any command/instruction to tell R to use only one of the two places? Thanks in advance (FWIW: I'm using macOS, not Windows). – pdeli May 02 '22 at 18:17