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
Installing rpy2 You must have Python >=3.7 and R >= 4.0 installed to use rpy2 3.5. 2. Once R is installed, install the rpy2 package by running pip install rpy2 . If you'd like to see where you installed rpy2 on your machine, you can run python -m rpy2.
rpy2 is running an embedded R, providing access to it from Python using R's own C-API through either: a high-level interface making R functions an objects just like Python functions and providing a seamless conversion to numpy and pandas data structures.
rpy2 will typically require an R version that is not much older than itself. This means that even if your system has R pre-installed, there is a chance that the version is too old to be compaible with rpy2. At the time of this writing, the latest rpy2 version is 2.8 and requires R 3.2 or higher.
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 pathIn 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
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]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With