Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempting to run RPY2 in Python and receiving error 0X7e

Tags:

python

r

rpy2

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.

like image 313
Frank Drin Avatar asked Jun 26 '20 14:06

Frank Drin


5 Answers

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

like image 179
Mayowa Avatar answered Nov 18 '22 15:11

Mayowa


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.

like image 3
Samuel Håkansson Avatar answered Nov 18 '22 16:11

Samuel Håkansson


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.

like image 3
Jakob Bartscher Avatar answered Nov 18 '22 17:11

Jakob Bartscher


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)

like image 2
lgautier Avatar answered Nov 18 '22 17:11

lgautier


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.

like image 1
Aidan Avatar answered Nov 18 '22 16:11

Aidan