[R] Passing parameters

aelmore aelmore at usgs.gov
Tue Aug 23 13:33:41 CEST 2011


Hi,

I've got an R script that I'm trying to turn into a ArcGis script tool so
that I can run it from ModelBuilder in Arc. Arc isn't giving me any errors
when I run the model holding the current tool, but the run time for the R
script is 0 seconds.  I don't know if the parameters aren't being passed
properly or what.  I'm a programming newbie, and I can't even figure out how
to see what's happening at the R end.

Here is the Python script that is retrieving parameters from the model and
supposedly passing to R:

import arcpy as ARCPY
import arcpy.management as DM
import os as OS
import sys as SYS

def setupMyRsemivar():
    ########## Get User Provided Inputs ##########
    currentWS = arcpy.GetParameterAsText(0) # base working directory
    currentWS_RCMD = '"' + wd_base + '"' # add the string quotes
    indata = arcpy.GetParameterAsText(1) # polygon input file
    indata_RCMD = '"' + data_input + '"' # add the string quotes
    R_output = arcpy.GetParameterAsText(2) # polygon input file
    R_output_RCMD = '"' + data_input + '"' # add the string quotes
    
    ########## Get R Tool Directory and Tool Name ##########
    pyScript = SYS.argv[0] # Get the path for the Python Script
    toolDir = OS.path.dirname(pyScript) # Get the directory tree for the
Python Script
    rScript = OS.path.join(toolDir, "MyRsemivar.r") # Concatenate the
directory with the R script name
    rScript = '"' + rScript + '"' # add the string quotes

    ########## Create and execute the command to call the R script
##########
    ARCPY.SetProgressor("default", "Executing R Script...")
    args = " ".join([currentWS_RCMD, indata_RCMD, R_output_RCMD])
    RCMD = "R --slave --vanilla --args "
    cmd = RCMD + args + " < " + rScript
    
    #### Uncomment Next Line to Print Command ####
    ARCPY.AddWarning(cmd)

    #### Execute Command ####
    OS.system(cmd)
####################################################
And here is the opening of my R script:

##################### load all of the required packages
print("Loading Libraries……")
library(intamap) # also loads sp, gstat, rgdal, akima, automap, mvtnorm,
MASS, evd, lattice
library(extremevalues)
library(fBasics)
library(geoR)
library(graphics)
library(plotrix)

###################  get arguments 
Args = commandArgs(trailingOnly=TRUE)
print(Args)

currentWS_RCMD = Args[1] 
indata_RCMD = sub(".shp","",Args[2], ignore.case = TRUE) # strip extension
from the file name
R_output_RCMD = Args[3]

###################################################

In the Arc Script tool, the parameters are:
currentWS:  Data Type=Workspace, Type=Required, Direction=Input
indata: Data Type=Feature Layer, Type=Required, Direction=Input
R_output: Data Type = text file, Type=Derived, Direction=Output

###################################################

The R script works fine on it's own, so I'm assuming it isn't picking up the
arguments I thought I was passing to it.  Any advice or reference materials
you can provide would be most appreciated.  I'm finding both the R and the
ESRI documentation a bit sketchy, or perhaps I'm just missing stuff because
it's still over my head.

Thanks,
Annie

--
View this message in context: http://r.789695.n4.nabble.com/Passing-parameters-tp3762480p3762480.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list