如何在PHP中显示R中的表?

时间:2016-11-30 18:10:13

标签: php r

我正在为学校项目编写机器学习代码,我在R的帮助下使用php。 我按照下一个网页(https://www.r-bloggers.com/integrating-php-and-r/)成功执行了穷人.php示例。但我想进一步使用例如虹膜数据集。 Bellow the image temp.png,我想从库(插入符号)显示confusionMatrix()的结果但是我被困在triyin中以在php文件中编写用php显示带有选项echo()的confusionMatrix的方式。

    <?php
    // poorman.php

    echo "<form action='poorman.php' method='get'>";
    echo "Number values to generate: <input type='text' name='N' />";
    echo "<input type='submit' />";
    echo "</form>";

    if(isset($_GET['N']))
    {
      $N = $_GET['N'];

      // execute R script from shell
      // this will save a plot at temp.png to the filesystem
      exec("Rscript my_rscript.R $N");

      // return image tag
      $nocache = rand();
      echo("<img src='temp.png?$nocache' />");
    }
    ?>

    args <- commandArgs(TRUE)   
    N <- args[1] 
    data("iris")
    x = iris[,-5]
    y = iris$Species
    kc <- kmeans(x,N)
    png(filename="temp1.png", width=500, height=500)          
    plot(x[c("Sepal.Length", "Sepal.Width")], col=kc$cluster,main="cluster 2D IRIS por MySQL")
    points(kc$centers[,c("Sepal.Length", "Sepal.Width")], col=kc$cluster, pch=23, cex=3)
    dev.off()

## prediction
library(class)
normalizacion=function(x) {    
num=x - min(x)    
denom=max(x) - min(x)   
return (num/denom)
}
iris1_normalizado=as.data.frame(lapply(iris1[1:4], normalizacion))
set.seed(1234)
iris1x=sample(2, nrow(iris1), replace=TRUE, prob=c(0.67, 0.33))
iris1x.training=iris1[iris1x==1, 1:4]
iris1x.test=iris1[iris1x==2, 1:4]
iris1x.trainLabels=iris1[iris1x==1, 5]
iris1x.testLabels=iris1[iris1x==2, 5]
iris1x_pred=knn(train=iris1x.training, test=iris1x.test, cl=iris1x.trainLabels, k=3)
library(caret)
confusionMatrix(iris1x.testLabels,iris1x_pred)

任何帮助? 提前谢谢。

0 个答案:

没有答案