19

I 'm unable to run rpy2 in python.

with this code

 import rpy2.robjects as robjects

Here's the full exceptions:


RuntimeError: R_USER not defined.

File "d:\py\r\r.python.py", line 1, in

  import rpy2.robjects as robjects

File "c:\Python27\Lib\site-packages\rpy2\robjects\__init__.py", line 17, in <module>
  from rpy2.robjects.robject import RObjectMixin, RObject

File "c:\Python27\Lib\site-packages\rpy2\robjects\robject.py", line 5, in <module>
  rpy2.rinterface.initr()

I'm using window xp win32 Here're my locations:

C:\Python27\Lib\site-packages\rpy2\robjects\robject.py

C:\Program Files\R\R-2.15.0\bin\i386\R.exe

C:\Python27\python.exe
Cœur
  • 32,421
  • 21
  • 173
  • 232
JPC
  • 4,525
  • 20
  • 63
  • 93

6 Answers6

20

Here is the way I fixed my R package version 3.0.2 python version 2.7 platform ipython notebook.

Change Path for R computer-> property -> advanced and system setting -> environment variables

in the user variable field add C:\Program Files\R\R-3.0.2\bin\x64 (my system is windows 64bit) to path

In the system variable field add two new variables

R_HOME c:\program files\r\r-3.0.2

R_USER C:\Users\"your user name"\Anaconda\Lib\site-packages\rpy2

Michael Piefel
  • 14,444
  • 5
  • 65
  • 95
user3758274
  • 201
  • 2
  • 2
  • 2
    For me I was having this problem and to fix it needed to set the R_USER variable for both the system and the user variables (using the same computer --> property --> advanced and system setting --> environment variables method). I simply set it to C:\Users\MyUsername – Brandon Aug 10 '15 at 17:27
  • 1
    Hi, I did this and worked. However, when I run Rstudio, i get path errors `The system cannot find the path specified` Any ideas how to fix this? thanks – Luis Candanedo Jan 29 '16 at 10:36
  • This worked for me! Thank you. Just wanted to add that, you may need to locate/browse the location of your 'rpy2' package in order to specify the correct path for R_USER. If you enter the path as shown above, and 'rpy2' is not located there, you won't see any error messages there under Advanced System Settings. And the R_USER RunTimeError would persist. For me, the installed 'rpy2' package was located here: C:\Users\{my_username}\Anaconda2\envs\py36\Lib\site-packages\rpy2 – Vishal Dec 08 '17 at 20:58
12

If you want to use Python with rpy2 but you also want to keep using your RStudio, don't forget to add RStudio to your path as well, or you'll get some path issues.

You can change your paths according to @user3758274:

Change Path for R computer-> property -> advanced and system setting -> environment variables in the user variable field add C:\Program Files\R\R-3.0.2\bin\x64 (my system is windows 64bit) to path

In the system variable field add two new variables

R_HOME    c:\program files\r\r-3.0.2

R_USER    C:\Users\"your user name"\Anaconda\Lib\site-packages\rpy2

But then add also RStudio to your R_USER system variable, so you'll get:

R_USER    C:\Program Files\RStudio\bin;C:\Users\"your user name"\Anaconda\Lib\site-packages\rpy2
Lenka Vraná
  • 1,546
  • 17
  • 28
12

Combining answers from @laven_qa and @user3758274, here is what worked for me :

# installing steps after downloading .whl from http://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2
import pip
pip.main(["install", "C:/Users/YOUR_USERNAME/Downloads/rpy2-2.8.6-cp36-cp36m-win_amd64.whl"]) # Path to the file that was downloaded from the website above

# setting temporary PATH variables
import os
os.environ['R_HOME'] = 'C:\Program Files\Microsoft\R Open\R-3.4.0' #path to your R installation
os.environ['R_USER'] = 'C:\ProgramData\Anaconda3\Lib\site-packages\rpy2' #path depends on where you installed Python. Mine is the Anaconda distribution

# importing rpy2
import rpy2.robjects as robjects

# test : evaluating R code
robjects.r('''
        # create a function `f`
        f <- function(r, verbose=FALSE) {
            if (verbose) {
                cat("I am calling f().\n")
            }
            2 * pi * r
        }
        # call the function `f` with argument value 3
        f(3)
        ''')

# returns : 
> R object with classes: ('numeric',) mapped to:
> <FloatVector - Python:0x000000000C260508 / R:0x000000000F2872E8>
> [18.849556]
François M.
  • 3,419
  • 7
  • 25
  • 64
  • temporarily setting variable didn't work for me. I had to completely reinstall https://thomas-cokelaer.info/blog/2012/01/installing-rpy2-with-different-r-version-already-installed/ – Gwang-Jin Kim Jun 18 '20 at 10:55
  • problem was that I had to put the right path to the right folder - I was too much up – Gwang-Jin Kim Jun 18 '20 at 11:14
9

For an instant and temporary solution, you can add the following code before importing rpy2:

import os
os.environ['R_HOME'] = 'C:/program files/R-3.3.1'

One thing worth noting is that you should use backslash instead of slash in the path.

laven_qa
  • 141
  • 1
  • 4
7

OH, nvm .. I fixed this .. here's how i did it , just incase anyone have the same issue. I have to specify PYTHONPATH to location rpy2.robjects stored

Here's in details : My Computer > System properties > Advanced > Environment Variables :

Under system variables create or edit your

Variable name : PYTHONPATH 

Variable value : C:\Python27\Lib\site-packages\rpy2;C:\Program Files\R\R-2.15.0\bin\i386;C:\Python27\Lib\site-packages\rpy2\robjects

This should work, enjoy.

Paul Hiemstra
  • 56,833
  • 11
  • 132
  • 142
JPC
  • 4,525
  • 20
  • 63
  • 93
  • Please, could you extent your answer ? I can not understand what ' to location rpy2.robjects stored' means – joaquin Jan 07 '13 at 11:47
  • This got me working in PyCharm CE. Adding R_USER variables didn't help so I took them back out and let R go to its defaults. – Nate Wanner Jan 29 '17 at 02:45
2

This might be what is discussed in this rpy2 issue on bitbucket.

lgautier
  • 10,758
  • 24
  • 41