1

PHPスクリプトから値を取得するRスクリプトによって生成されたグラフを使用する小さなプロジェクトに取り組んでいます。しかし、ベクトルをパラメーターとして使用すると問題が発生します。

PHPから私は単純に使用します

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

これは私のRスクリプトです

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]              # the filename of the output file
xaxis        <- args[5]              # the text for the x axis
yaxis        <- args[6]              # the text for the y axis
size         <- as.integer(args[7])  # the size of the generated png file (x and 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) to the given distribution
                                             # and generates a function cdf() (e.g. pcauchy()->cdf()
qf <- get(paste("q",distribution_f,sep=""))  # connects q (for quantile) to the given distribution
                                             # and generates a function qf() (e.g. qnorm()->qf()
di <- d_f[1]                                 # set di to the first element of the list d

roc.curve.d <- function(x)
    {
    return(1-cdf((qf(1-x)-di)/(sigma_f)))   # the main formula
    }
gcolours <- c(1)                            # set gcolours to list element 1
curve(roc.curve.d,from=0,to=1,ylim=c(0,1),lwd=3,xlab=xaxis_f, ylab=yaxis_f)
# draw a line with the function roc.curve.d from x=0 to 1 and y=0 to 1 with thickness 3

curve(1*x,add=T,lty=2)                      # draw a diagonal line

for (i in 2:length(d_f))
    {
    gcolours <- c(gcolours,((i-1)%%8)+1)    # makes sure, that gcolours is always 1:8
    di <- d_f[i]                            # set di to the value of index i
    curve(roc.curve.d,lwd=3,col=gcolours[i],add=TRUE)   #
    }

legend("bottomright",pch=15,legend=d_f,col=gcolours,title="d")  # draws a legend in the bottom right corner
}


png(filename = f, width = size, height = size)   # generate the empty png-file like a paper sheet
plot.roc.curves(d,sigma,distribution,xaxis,yaxis) # write the curve on the new generated sheet
dev.off()                                        # close the graphic file

問題は次のとおりです。

このRスクリプトは、例えば

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

しかし、最初のパラメーターにはシーケンスまたはリストを使用する必要があります。

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

また

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

これにより、コマンド ラインからの実行中に NA エラーが発生し、対角線の後に色付きの曲線がなく、部分的にのみ正しい画像になります。「as.integer(args[1])」から as.integer を削除し、別の方法を考えました。私は試した

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

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

ただし、これはシーケンスではなく文字列として扱われるため、これも機能しません。数時間の調査の後、問題の解決策を見つけましたが、これは機能しますが、正しい解決策ではないと確信しています。関数 plot.roc.curves の開始直後に中括弧の直後に挿入しました。

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

私は知っています、これは醜いです。しかし、他の型を使用する必要があるたびに、新しい変換シーケンスを作成する必要があります。それは正しくありません。

実際には、スクリプトにベクトルを与える必要があります。

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

簡単な方法があるはずですが、見つけられませんでした。コマンド ラインまたは PHP から R スクリプトに混合型 (ベクトルやシーケンスなど) を送信する方法を理解できる人はいますか? どんな助けでも大歓迎です。

4

0 に答える 0