Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我想将变量发送到R,然后R将图像发送到浏览器_Php_Ajax_Image_R_Rapache - Fatal编程技术网

Php 我想将变量发送到R,然后R将图像发送到浏览器

Php 我想将变量发送到R,然后R将图像发送到浏览器,php,ajax,image,r,rapache,Php,Ajax,Image,R,Rapache,我正在开发一个使用R环境和RApache的web应用程序。我已经使用了AJAX.updater函数向一个R脚本发送了几个变量,然后返回给浏览器一个ResponseText来显示。这没有问题,但现在我希望向绘制图形的R脚本发送一个变量,然后将图像返回到浏览器 我可以在浏览器中通过该脚本显示R绘制的图像,例如: <% setContentType("image/png") t <- tempfile() load(file="/var/www/oraculo/brew/ICER

我正在开发一个使用R环境和RApache的web应用程序。我已经使用了AJAX.updater函数向一个R脚本发送了几个变量,然后返回给浏览器一个ResponseText来显示。这没有问题,但现在我希望向绘制图形的R脚本发送一个变量,然后将图像返回到浏览器

我可以在浏览器中通过该脚本显示R绘制的图像,例如:

    <% setContentType("image/png")
t <- tempfile()

load(file="/var/www/oraculo/brew/ICER")

png(t, width=3.25, height=3.25, units="in", res=1200, pointsize=4)
plot(G,vertex.size=1,vertex.label=NA)
dev.off()
sendBin(**readBin**(t,'raw',n=file.info(t)$size))
unlink(t)


DONE
%>
使用RApache的GET变量,我可以在R脚本中使用'autini'

一个可行的解决方案是将图像保存到文件中,但我不太喜欢它。 有什么方法可以将在“readbin”中读取的比特流放入“responseText”中,然后用php构建图像?我应该使用AJAX的哪个功能


谢谢你的时间

我用安装在服务器上的FastRWeb和Rserve解决了类似的问题 使用jquery.js在html页面中传递类似这样的json格式的数据。使用此解决方案的一个重要细节是,如果尝试使用$.ajax检索二进制数据,则需要复制jquery.js并修复两行,如中所述

在服务器端,您将需要一个名为myboxplot.R的R程序,其中包含(例如)以下代码:

 run <- function(MyTable) {
 # read json into data frame
 mytmp <- fromJSON(rawToChar(.GlobalEnv$request.body))
 axes <- sapply(strsplit(json_data[['entries']][['Labels']], ",") , as.character)
 x <- sapply(strsplit(json_data[['entries']][['Class']], ",") , as.character)
 y <- sapply(strsplit(json_data[['entries']][['Hwy']], ",") , as.numeric )
 z <- sapply(strsplit(json_data[['entries']][['Order']], ",") , as.numeric ) 
 mydata <-  data.frame(x,y,z)
 mydata$count <- ave(as.numeric(mydata$x), mydata$x, FUN = length)
 #add the count to the x label
 mydata$x <- factor(do.call(paste, c(mydata[c("count", "x" )], sep = "]-")))
 mydata$x <- paste( "[",mydata$x, sep = "" ) 
 #reorder by z which is the order number
 mydata$reord <- reorder(mydata$x, mydata$z)
 p <- WebPlot(1191, 842)  # A3
 print( ggplot(mydata, aes(reord,y) ) + geom_boxplot (aes(fill=reord), alpha=.25, width=1, outlier.colour = "green") + labs(x = axes[1], y = axes[2]) + scale_fill_discrete(name=axes[3]) + stat_summary(aes(label=sprintf("%.02f",..y..)), fun.y=mean, geom="text", size=3) + theme_bw() + theme(axis.title.x = element_text(face="bold", colour="#990000", size=14, vjust = -1), axis.text.x  = element_text(angle=-90, hjust=1, size=12), plot.margin=unit(c(1,1,1.5,1),"lines"))  )
 p 
 } 

运行也许你应该看看
闪亮的
  var myjsondata = '{ "entries": { "Labels": "MyTitle","Order": "1,2,3", "Class": "AM,PM,XF","Hwy": "29,29,31,20,29,26,29,29,24,44,29,26,29,32,49,29,23,24,44,41,29,26,28,29,39,29,28,29,26,26" } }';

   var request = $.ajax({         
        url: "http://Rserverip/cgi-bin/R/myboxplot",
        type: "POST",             
        xhrFields: {
              responseType : "arraybuffer"
        },
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        dataType:  "binary",
        processData: false,
        crossDomain : true,
        data: myjsondata 
      });

    request.done(function(msg,ret_status,xhr) {
        var uInt8Array = new Uint8Array(msg);
            var i = uInt8Array.length;
            var binaryString = new Array(i);
            while (i--)
            {
              binaryString[i] = String.fromCharCode(uInt8Array[i]);
            }
            var data = binaryString.join('');
            var base64 = window.btoa(data);
            $(img).attr('src', "data:image/png;base64,"+base64 );
       });
 run <- function(MyTable) {
 # read json into data frame
 mytmp <- fromJSON(rawToChar(.GlobalEnv$request.body))
 axes <- sapply(strsplit(json_data[['entries']][['Labels']], ",") , as.character)
 x <- sapply(strsplit(json_data[['entries']][['Class']], ",") , as.character)
 y <- sapply(strsplit(json_data[['entries']][['Hwy']], ",") , as.numeric )
 z <- sapply(strsplit(json_data[['entries']][['Order']], ",") , as.numeric ) 
 mydata <-  data.frame(x,y,z)
 mydata$count <- ave(as.numeric(mydata$x), mydata$x, FUN = length)
 #add the count to the x label
 mydata$x <- factor(do.call(paste, c(mydata[c("count", "x" )], sep = "]-")))
 mydata$x <- paste( "[",mydata$x, sep = "" ) 
 #reorder by z which is the order number
 mydata$reord <- reorder(mydata$x, mydata$z)
 p <- WebPlot(1191, 842)  # A3
 print( ggplot(mydata, aes(reord,y) ) + geom_boxplot (aes(fill=reord), alpha=.25, width=1, outlier.colour = "green") + labs(x = axes[1], y = axes[2]) + scale_fill_discrete(name=axes[3]) + stat_summary(aes(label=sprintf("%.02f",..y..)), fun.y=mean, geom="text", size=3) + theme_bw() + theme(axis.title.x = element_text(face="bold", colour="#990000", size=14, vjust = -1), axis.text.x  = element_text(angle=-90, hjust=1, size=12), plot.margin=unit(c(1,1,1.5,1),"lines"))  )
 p 
 }