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
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
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
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.
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.
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]
This might be what is discussed in this rpy2 issue on bitbucket.