how to call R from python?

10622
1
02-28-2012 06:07 PM
juttabeher
New Contributor
I am trying to set up a loop for sampling different rasters with a bunch of pointfiles within a bigger python script. The python version of "extract-multivalues-to-points" does not work, and I couldn't figure out how to pass my loop arguments onto geospatial modeling environment (GME), I planned to use R (and I want to do some batch calculations afterwards with the results anyway), but I can't get it working. I try to call it via rpy2, but when running the tests, if rpy2 is correctly installed, I always get an error, that R is not in the Path. I guess the reason is, that rpy2 is written for an older R version, and the directory structure was different then, but I have no idea how I could try to adjust this, unfortunately I am not very experienced in general commandline-scripting what makes it harder to understand the error...I use python2.6, arcGIS10 and R.2.14.1 on a windows7 machine. If someone has any idea, or a workaround, it would be awesome!!
Cheers, Jutta

here is the test I try to run (from the rpy2 website):
import os
import sys

import rpy2.tests
import unittest
tr = unittest.TextTestRunner(verbosity = 1)
suite = rpy2.tests.suite()
tr.run(suite)


here is the script that throws the error:
import os, sys

try:
    R_HOME = os.environ[r"C:\Program Files\R\R-2.14.1\bin\i386"]
except KeyError:
    R_HOME = os.popen("Rgui.exe").readlines()

if len(R_HOME) == 0:
    if sys.platform == 'win32':
        try:
            import win32api
            import win32con
            hkey = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE,
                                         "Software\\R-core\\R",
                                         0, win32con.KEY_QUERY_VALUE )
            R_HOME = win32api.RegQueryValueEx(hkey, "InstallPath")[0]
            win32api.RegCloseKey( hkey )
        except:
            raise RuntimeError(
                "Unable to determine R version from the registery." +\
                "Calling the command 'R RHOME' does not return anything.\n" +\
                    "This might be because R.exe is nowhere in your Path.")
0 Kudos
1 Reply
PhilMorefield
Occasional Contributor III
Most likely you need to add the 'R_HOME' environment variable.


  • Go to: Control Panel-->System-->Advanced-->Environment Variables.

  • Under 'System Variables' click New.

  • Variable name: R_HOME

  • Variable value: C:\Program Files\R\R-2.14.1

  • Highlight the 'Path' variable and click Edit. Make sure that 'C:\Program Files\R\R-2.14.1\bin\i386' is somewhere in that long list of pathways.

The unittest still bombs out, but I was able to import robjects which lets me run R commands from Python. However, note that Rpy2 is not well supported on Windows and so you will probably run into other problems down the road, which is why I gave up. If you're really set on being in both an ArcGIS and R environment simultaneously, you're probably better off going the opposite direction: use RPyGeo (an R library) to run ArcGIS geoprocessing tools.

Good luck!
0 Kudos