Error while using PHP to run R scripts using a sequence or vector as parameter -


i working on small project uses graphs generated r scripts, values php script. have problems using vectors parameters.

from php use simply

echo shell_exec("plot.roc.curves.r ".$d." ".$sigma." \"".$distribution."\"  \"".$f."\"  \"".$x."\"  \"".$y."\" ".$size); 

this r script

args <- commandargs(true)            # allow command line arguments  d            <- as.integer(args[1]) sigma        <- as.integer(args[2])  # sigma distribution <- args[3]              # distribution (cauchy, norm, exp, binom, ... ) f            <- args[4]              # filename of output file xaxis        <- args[5]              # text x axis yaxis        <- args[6]              # text y axis size         <- as.integer(args[7])  # size of generated png file (x , y)  plot.roc.curves <- function(d_f,sigma_f,distribution_f,xaxis_f,yaxis_f) { cdf <- get(paste("p",distribution_f,sep="")) # connects p (for distribution) given distribution                                              # , generates function cdf() (e.g. pcauchy()->cdf() qf <- get(paste("q",distribution_f,sep=""))  # connects q (for quantile) given distribution                                              # , generates function qf() (e.g. qnorm()->qf() di <- d_f[1]                                 # set di first element of list d  roc.curve.d <- function(x)     {     return(1-cdf((qf(1-x)-di)/(sigma_f)))   # main formula     } gcolours <- c(1)                            # set gcolours list element 1 curve(roc.curve.d,from=0,to=1,ylim=c(0,1),lwd=3,xlab=xaxis_f, ylab=yaxis_f) # draw line function roc.curve.d x=0 1 , y=0 1 thickness 3  curve(1*x,add=t,lty=2)                      # draw diagonal line  (i in 2:length(d_f))     {     gcolours <- c(gcolours,((i-1)%%8)+1)    # makes sure, gcolours 1:8     di <- d_f[i]                            # set di value of index     curve(roc.curve.d,lwd=3,col=gcolours[i],add=true)   #     }  legend("bottomright",pch=15,legend=d_f,col=gcolours,title="d")  # draws legend in bottom right corner }   png(filename = f, width = size, height = size)   # generate empty png-file paper sheet plot.roc.curves(d,sigma,distribution,xaxis,yaxis) # write curve on new generated sheet dev.off()                                        # close graphic file 

now heres problem:

this r script workes fine e.g.

plot.roc.curves.r 1 2 "norm" "file" "x" "y" 500 

but need use sequences or lists first parameter, e.g.

plot.roc.curves.r 1:5 2 "norm" "file" "x" "y" 500 

or

plot.roc.curves.r 1,2,3,5 2 "norm" "file" "x" "y" 500 

which produces na error while running command line , leads partially correct image without colored curves after diagonal line. removed as.integer "as.integer(args[1])" , thought of annother way. tried

plot.roc.curves.r "1:5" 2 "norm" "file" "x" "y" 500 

and

plot.roc.curves.r "1,2,3,5" 2 "norm" "file" "x" "y" 500 

but not work either treatened string , not sequence. after hours of probing have found solution problem - sure not right solution although works. inserted after beginning of function plot.roc.curves directly after curled brace:

if(regexpr(":", d_f)>0)          # if d_f contains @ least 1 colon (therefore it's integer sequence)     {     d_f <- unlist(lapply(as.list(d_f), function(x) eval(parse(text=x)))) # convert string integer sequence     } else if(regexpr(",", d_f)>0)     # if d_f contains @ least 1 comma (therefore it's list of numeric values)     {     d_f <- as.numeric(unlist(strsplit(d_f,",")))                         # convert string numeric     } else     {     d_f <- as.numeric(d_f)       # assume parameter numeric , integer     } 

i know, ugly. each time need use other types have write new converting sequence. can't right.

actually need give vector script:

plot.roc.curves.r c(1,2,3) 2 "norm" "file" "x" "y" 500 

there must simple way - haven't found it. can me understand how send mixed types (e.g. vectors or sequences) command line or php r script ? highly appreciated.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -