Skip to content

Passing parameters

2 messages · aelmore, Scott Foster

#
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.
2 days later
#
Hi Annie,
while I don't use python, I've been in similar positions.

here's a good link for the arguments function. It's the best
example/tutorial I've seen on the topic. It will also help you perhaps to
print the values of the arguments to screen/file within R to see if things
are going okay.

http://quantitative-ecology.blogspot.com/2007/08/including-arguments-in-r-cmd-batch-mode.html
http://quantitative-ecology.blogspot.com/2007/08/including-arguments-in-r-cmd-batch-mode.html 

remember that args is a list not a vector which is why you need two of [[
and two of ]].

given your R script say things like
what do these parts of your code return? That would be the simplest way for
you (and us) to check what's going on. The example linked above should help.
The part where it loops through the expressions and parses them - that's a
good place to add a print(args[[i]])  statement onto the screen/output file
to see that things are being read in correctly.

so when you say the R script works correctly by itself, what is this command
you give? I suggest getting python to print the command to screen as well as
execute it, will help you. My guess is these two things differ and that
means it's probably more a python programming issue than an R one so you
might be better posting elsewhere in a python related forum.





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