8

I'm attempting to run RPY2 to utilize the TTR package in R, and running python 3.8.3 and R 4.0.2. However, when attempting to run the code

os.environ['R_HOME'] = "C:\\Program Files\\R\\R-4.0.2\\bin\\x64"
from rpy2.robjects.packages import importr'

this results in :

OSError: cannot load library 'C:\Program Files\R\R-4.0.2\bin\x64\bin\x64\R.dll': error 0x7e

I proactively ran python -m rpy2.situation, yielding

C:\Users\XXXXX>python -m rpy2.situation
rpy2 version:
3.3.4
Python version:
3.8.3rc1 (tags/v3.8.3rc1:802eb67, Apr 29 2020, 21:39:14) [MSC v.1924 64 bit (AMD64)]
Looking for R's HOME:
    Environment variable R_HOME: None
    InstallPath in the registry: C:\Program Files\R\R-4.0.2
    Environment variable R_USER: None
    Environment variable R_LIBS_USER: None
R version:
R version 4.0.2 (2020-06-22) -- "Taking Off Again"
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.

Any help on why RPY2 is causing this 0x7e error is greatly appreciated. I have also uninstalled and reinstalled both R, and RPY2 as I found that on a solution on some other posts.

Frank Drin
  • 1,613
  • 2
  • 13
  • 18
  • This [answer](https://stackoverflow.com/questions/63449770/oserror-cannot-load-library-gobject-2-0-error-0x7e) helped me. – PSK Jul 10 '22 at 03:19

8 Answers8

3

I had the same issue trying to import the rpy2 library. I got it sorted when i added a path for R in my environment variable.

***InstallPath in the registry: C:\Program Files\R\R-4.0.2

Try creating a path on system environment variables with the above and see if it works

Mayowa
  • 31
  • 1
3

I had the same error and for me the problem was that SciPy was imported before rpy2. Moving the SciPy import below rpy2 solved it.

3

I had the exact same problem. The reason was that python was running in an Anaconda environment. The environment has its own version of R installed. (Maybe search your computer for "Rcmd.exe" to see all the R copies on your machine.) The solution was to modify os.environ['R_HOME'] to the appropriate copy of R:

For me it worked by adding this to the top of my python script:

import os
os.environ["R_HOME"] = "C:\\Users\\<Name>\\anaconda3\\envs\\<enironment_name>\\Lib\\R\\"

But the exact path might be different for you depending on from where you are running rpy2.

And also note that just like Aidan mentioned you should not add \\bin\\x64 to your R_HOME path.

  • This is what I ran on a mac and it got it work: `import os` then `os.environ['R_HOME'] = "/Users/georgehayward/miniconda3/lib/R"` – George Hayward Mar 16 '22 at 22:40
2

The line Loading R library from rpy2: OK when running rpy2.situation suggests that the R dll is loading properly. There is likely something different between the environment in which you are running you Python script and the terminal where you are running C:\Users\XXXXX>python -m rpy2.situation.

Try running rpy2.situation from a Python script (for example take the content of the if __name__ == '__main__': block - https://github.com/rpy2/rpy2/blob/master/rpy2/situation.py#L358)

lgautier
  • 11,363
  • 29
  • 42
  • I'm still confused lgautier. Added R to path by doing the following Verify R is installed in the following path: C:\Program Files\R\R-3.3.2\bin\x86 Open the start menu and type in “View advanced system settings”, click on “Environment variables” Under “System variables”, select Path and click on edit. Click “New”, and add the folder address for R to there (C:\Program Files\R\R-3.3.2\bin\x86) Open power shell and type in Rterm or R.exe to launch R command line tool Appears correctly when running R.exe in powershell, so cant imagine why this is not working. Maybe has to do w/ anaconda ? – Frank Drin Jun 29 '20 at 12:58
1

note in your output:

OSError: cannot load library 'C:\Program Files\R\R-4.0.2\bin\x64\bin\x64\R.dll': error 0x7e

your R_Home just needs to be 'C:\Program Files\R\R-4.0.2'. In fact remove the changing of environment variables and it should just work.

Aidan
  • 11
  • 1
1

You need to do both 2 things together:

  1. set R_HOME
  2. set environ "path" include "R bin"

e.g.

#set R_HOME dynamically
import os
os.environ['R_HOME'] = r'YOUR R HOME PATH' #e.g. r'C:\Users\STEMLab\Miniconda2\envs\myenv\Lib\R' for my case
#set R bin
os.environ['path'] += r';YOUR R BIN;' #e.g. r';C:\Users\STEMLab\Miniconda2\envs\myenv\Lib\R\bin;' for my case again

cheers :)

SCKU
  • 783
  • 9
  • 14
1

I was in the exact same situation and changing the environment variable settings didn't solve the problem. I had installed 32-bit python by mistake; installing 64-bit python worked fine for me!

You can check it by

import platform; platform.architecture()
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
1

I managed to get rpy2 (and pymer4) working by loading r.dll first using ctypes

import ctypes
import os
os.environ["R_HOME"] = r"D:\anaconda3\envs\pymer4\Lib\R"
# Load DLL into memory.

hllDll = ctypes.WinDLL (r"D:\anaconda3\envs\pymer4\Lib\R\bin\x64\R.dll")

import rpy2.robjects as robjects
sam
  • 160
  • 6