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) and R for statistical analysis. I'm trying to install rpy2 into Python to integrate R, but I get the error:

Tried to guess R's HOME but no R
command in the PATH.

I'm not sure what this means. The path to my R.exe is "C:\Program Files\R\R-2.12.1\bin" if that's useful. Any help would be much appreciated!

@Taj - I had a similar issue of finding R in my path (not python related), I ended up having to go down one more directory into C:\Program Files\R\R-2.12.1\bin\x64 to get my script to find R. If you want the 32 bit version of R, change x64 with i386 and see if that makes it work.
–
ChaseFeb 7 '11 at 18:32

@Taj: It means the directory for R isn't in your PATH environment variable. See here for setting PATH on Windows: computerhope.com/issues/ch000549.htm (I should add that the main developer of rpy2 doesn't use Windows, so you may be working with an old version).
–
Thomas KFeb 7 '11 at 18:45

@Thomas: Thanks for the link. I found that info while searching for my answer earlier, but all of my attempts to modify the PATH variable yielded the same result. I tried the dir that R.exe is in, the dir that R.dll is in, but no luck. Would you happen to have any more specific info?
–
Taj GFeb 7 '11 at 18:56

@Taj: Where did you get rpy2 from, and what version did you download?
–
Thomas KFeb 7 '11 at 19:06

@Taj - see my comment regarding how I modified my environment path in order to work. You can isolate whether this is an rpy2 problem, or path issue by opening a command prompt (start --> run --> cmd) and typing R. You should see R fire up in the DOS prompt as a fully functional program. You can debug from there.
–
ChaseFeb 7 '11 at 19:08

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.

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

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)

Profit

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!