1

我正在做一个小项目,它使用由 R 脚本生成的图表,这些图表从 PHP 脚本中获取它们的值。但是我在使用向量作为参数时遇到了问题。

从 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