8

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)
  • I don't know what version of ArcGIS server you are using and I don't know the configuration of your system, but one thought is that, at least with the older ArcGIS Server config, v10.0 and older, any services run would be generally run through a dedicated user account (SOM & SOC users, etc....). I don't know how you're permissions are set up, but I would get with your server/web admin if not you and figure out what user is being used to run the service and if that user has all permissions needed to access all the programs & files involved. Also, did you test it in ArcDesktop from the server? – John Feb 28 '14 at 5:47
  • I am using ArcGIS Server 10.2.1 and I am the web admin. We have tested the permissions and that is not the issue. The tool works in ArcGIS Desktop on the server but not as a geoprocessing service. When the tool is run from the desktop an R console window pops up and shows the R processes. I do not see this when running the service. I know R is installed on the server since we can run the tool on ArcGIS desktop. Well I guess all there is to do is to continue to troubleshoot – Jeremy Hamm Feb 28 '14 at 15:24
  • I don't know anything about this particular problem, but have you tried using something like Rscript test.R arg1 arg2 arg3 instead? Just in case you weren't aware of that option, here's a simple example of what that looks like in practice. – Josh O'Brien Mar 6 '14 at 0:44
  • Yeah I don't think the issue is with the R script itself. When I run the geoprocessing service in ArcMap it fails before the R script is called or while trying to call the script. I know R is installed on the server. I am really stumped by this... – Jeremy Hamm Mar 6 '14 at 16:53
  • OK, good luck. If you figure this out, please do post the answer here. – Josh O'Brien Mar 6 '14 at 18:06
2

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.

  • I actually used a method similar to this but not the route of a geoprocessing service. The python script executes the R code and reads in the output text file from the R script. This was the most efficient way to run our model and return results – Jeremy Hamm Sep 18 '14 at 17:06
1

You will need to use the ESRI tool structure and include the files in your script folder if you would like to reference them directly by name like you are doing in your code. http://resources.esri.com/help/9.3/ArcGISDesktop/com/Gp_ToolRef/sharing_tools_and_toolboxes/a_structure_for_sharing_tools.htm

Otherwise, you will need to append the location of the script in your sys.path. The example below references file "\myFolder\myModules\myExtFile.py". After you have a valid reference to your file you can use your custom functions in the GP service as long as where you are publishing to has access to the folder path.

import sys, arcpy
sys.path.append(r'\\myFolder\myModules')

import myExtFile as extFile

if __name__ == '__main__':
    # Get values from ESRI
    param1 = arcpy.GetParameterAsText(0)

    # Use custom python library
    myVal = extFile.someFunc(param1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.