babbelabab

how to call R from python?

Discussion created by babbelabab on Feb 28, 2012
Latest reply on Mar 13, 2012 by philmorefield
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.")

Outcomes