[GIS] How to call external files in Python Geoprocessing Service

arcgis-servergeoprocessing-servicepythonr

I have created a Python script which calls an R script and then reads the output back into Python. I can run this tool in ArcGIS Desktop (10.2.1) and it works fine. When I publish as a geoprocessing service it fails once it reaches the R script. I have checked the file paths and they are fine. Is it even possible to call external scripts from a geoprocessing service? Here is my code.

import sys, os, arcpy
arcpy.AddMessage("Loaded Python Imports...")

try:
#User Input
lat = arcpy.GetParameterAsText(0)
long = arcpy.GetParameterAsText(1)

arcpy.AddMessage("LAT: " + "".join(lat))
arcpy.AddMessage("LONG: " + "".join(long))

#Create R Command
rScript = "test.R"
args = " ".join([lat, long])
RCMD = "R --slave --vanilla --args "
cmd = RCMD + args + " < " + rScript

#Execute
os.system(cmd)

#Render the Results
output = open("FILE PATH")
finalNumber = output.read()
print("The output is: " + " ".join(finalNumber))
arcpy.AddMessage("The output is: " + " ".join(finalNumber))
output.close()

#Delete Text File
os.remove("FILE PATH")

except Exception as e:
    print e.message
    arcpy.AddError(e.message)

Best Answer

I found this article: Integrating external programs within ModelBuilder, it is older and initially looks like it is off topic, but if you look at this: enter image description here, you can see that it explicitly sets the path to the R script.

When your geoprocessing script runs on the server, it runs in a scratch folder within the jobs directory. Depending on the publishing process, your R script may not be there. Whenever I refer to external script within my geoprocessing services, I always explicitly refer to them from a folder that has been registered as a datasource.