I\'m brand new to Python (and programming in general) I come from a finance background, so please bear with me. I just started using Python (Enthought\'s Pylab/Scipy/Numpy)
I know I'm a little late to the party, but I had the same problem and got it working on Windows 7 by doing the install via conda
conda install --channel https://conda.binstar.org/joshadel rpy2
I've encountered a somewhat different but related installation problem and finally found a solution at http://www.mail-archive.com/rpy-list@lists.sourceforge.net/msg02817.html
After I installed rpy2 2.0.8 via rpy2-2.0.8.win32-py2.6.msi (windows 7 (64bit), python 2.6, R 2.14 (both 32bit and 64bit, via RAndFriendsSetup2140V3.2-1-1.exe)), and tried to import rpy2 within python console, I got the exception:
Unable to locate R.dll
Adding the following lines to rinterface/__init__.py made it work:
if os.path.exists(os.path.join(R_HOME, 'lib')): ## ADDED ##
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin')
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules')
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'lib')
else: ## ADDED ##
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin', 'i386') ## ADDED ##
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules', 'i386') ## ADDED ##
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'library') ## ADDED ##
# Load the R dll using the explicit path
# First try the bin dir:
Rlib = os.path.join(R_HOME, 'bin', 'R.dll')
# Try bin/i386 subdirectory seen in R 2.12.0 ## ADDED ##
if not os.path.exists(Rlib): ## ADDED ##
Rlib = os.path.join(R_HOME, 'bin', 'i386', 'R.dll') ## ADDED ##
# Then the lib dir:
if not os.path.exists(Rlib):
Rlib = os.path.join(R_HOME, 'lib', 'R.dll')
# Otherwise fail out!
if not os.path.exists(Rlib):
raise RuntimeError("Unable to locate R.dll within %s" % R_HOME)
It turns out that the R.dll is moved but the __init__.py is not updated accordingly. So simply editing the __init__.py file will get things right.
Then I tried to replicate Taj G's situation, and I did it. After adding "your_R_installation_dir\bin\i386" into the windows environment variable PATH, the old error disappeared but new one came:
ValueError: Invalid substring in string
It seems that some additional pieces need to be installed and a C/C++ compiler needs to be correctly configured. I gave up here. Using easy_install to build rpy2 from source seems really tricky on windows and not officially support at present.
Although rpy2 2.0.8 is not a full-fledged version compared with 2.2.4, it is the latest version with standard windows installer on sourceforge. For now, it is the easy choice.
Got rpy2 to work finally with Windows 7 from source code. If it doesn't work for you like it didn't for me. See rpy2 install on windows 7.
Hope this helps!!
I too had a problem with RPy2, and I never actually got it to work - after days and days of trying all different sorts of solutions. I encourage you to try all of the great ideas people are telling you, and I'm interested to see if any of them work!
If you fail as I did, you may be able to use a workaround in the following way depending on your purposes:
Write R code with all of the functions you would like to use which also calls the libraries you want to use. Put all of these functions and library calls into one file (temp.r). For example, maybe my file looks like
CurrentYear <- function(birth.year,age) {
year <- birth.year + age
return(year)
}
Use python to prompt for user input to do function calls. I did this with a GUI, you can probably just do it with script in the terminal.
Use python to create a string with the R function calls. For example, we might have
stuff = '\nCurrentYear("%(birth.year)d", "%(age)d")\n' %vars()
where birth.year
and age
have been input by the user in the python program. Add stuff
to the end of temp.r
using python:
# Creates a copy of temp.r, so as not to disturb its contents for future use.
tocall = copyfile("C:\My Documents\temp.r", "C:\My Documents\tocall.r")
# Open the copy with the intent to append it (hence the "a")
inp = open("C:\tocall.r", "a")
# Adds the function call to the R script
inp.write(stuff)
inp.close()
# Navigate to the correct directory, use "Rscript" to
# run the code in the shell
dostuff = call('cd C:\My Documents &Rscript temp.r', shell = True)
I'm not sure what the disadvantages of this method are, but it works for me. Hope this helps if you fail with the whole RPy2 thing!
I have encountered a similar problem trying to use rpy2 with R 2.12 and Python 2.6 (as recommended by the rpy2 documentation).
It seems that windows binaries from http://cran.r-project.org/bin/windows/base/R-2.12.1-win.exe install the required R.dll in a directory not expected by rpy2.
I have copied all the files from R\R-2.12.1\bin\i386 to the bin directory, set an environnement variable R_HOME pointing to R\R-2.12.1 and it then worked.
I had exactly the same problem running rpy2 in python under win32 "WINBLOW :)" XP ; but I got this to work , FINALLY !!.
Here's how I did it :
Same as Guillaume, I had to copy all the files from C:\Program Files\R\R-2.15.0\bin\i386 to R\R-2.15.0
I had to set an environment variable
Go to My Computer>properties>Advanced>Environment Variables>
Variable : "PATH" , adding ;C:\Program Files\R\R-2.15.0
at the end of the "Value"
python setup.py build install
under command prompt C:\Python27\rpy2>
Hope This help, and please let me know if this work for you.