2012-08-13 103 views
2

我偶然发现了一个名为openCPU.org的令人敬畏的开源项目,我对这个项目感到非常兴奋。作为一名研究科学家,我试图创建一个托管我的工作的网站,我只想在云上运行R,让我的脚本实时运行并显示在我的网页上。所以感谢Jeroen让这个项目发生了很长时间。与openCPU交互

有了这个,我的问题。

我该如何与openCPU交互?

我可以把一个例子功能为“运行一些代码”:

http://public.opencpu.org/userapps/opencpu/opencpu.demo/runcode/

和检索我的代码,这是伟大的一个PNG图像!

但我该如何在自己的网页或通过URL?

我可以从这个页面让我原来的代码上传的对象,是这样的:“x3ce3bf3e33”

如果是类似的功能:

myfun <-function(){ 

x = seq(1,6.28) 
y = cos(x) 
p = plot(x,y) 
print(p) 
# also tried return(p) 
} 

应该不是我能够通过调用它:

http://public.opencpu.org/R/tmp/x3ce3bf3e33/png

什么用的输入变量?例如:

myfun <-function(foo){ 

x = seq(1,foo) 
y = cos(x) 
p = plot(x,y) 
print(p) 
} 

我觉得可能是我缺少的东西。如何使用url指定“GET”或“POST”?

编辑

确定响应以下@Jeroen,我需要使用使用POST并与该API获取。现在我的问题扩展到让PHP正确地与它交互的问题。

说我有代码:

<?php 

$foo = 'bar'; 
$options = array(
    'method' => 'POST', 
    'foo' => $foo, 
    ); 
$url = "http://public.opencpu.org/R/tmp/x0188b9b9ce/save"; 
$result = drupal_http_request($url,$options); // drupal function 
?> 

我怎么那么访问哪些传递回来$结果?我正在寻找一个图表。它看起来像这样:

{ 
    "object" : null, 
    "graphs" : [ 
     "x2acba9501a" 
    ], 
    "files" : {} 
} 

下一个步骤将是获得图像,沿着线的东西:

$newurl = "http://public.opencpu.org/R/tmp/".$result["graph"]."/png"; 
$image = drupal_http_request($newurl); 
echo $image; 

但我不知道如何访问$结果中的单个元素?

编辑#2

玉家伙,我已经得到了这个工作,由于下方和多个其他帮助会话的答案,很多砸我的头监视器。

在这里,我们走了,带着卷曲

<?php 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://public.opencpu.org/R/tmp/x0188b9b9ce/save'); 
curl_setopt($ch, CURLOPT_POST, 1); // Method is "POST" 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Returns the curl_exec string, rather than just Logical value 
$result = curl_exec($ch); 
curl_close($ch); 
$new = json_decode($result,true); // $result is in 'json' format, decode it 
$get = $new['graphs'][0]; // the 'hashkey for the image, "x2acba9501a" above 

$img = 'http://public.opencpu.org/R/tmp/'.$get.'/png'; // link to the png image 

echo <<<END // use this to display an image from the url 
<a href="$img"> 
<img src="$img"> 
</a> 
END 

?> 
+1

你的问题有几个问题:你的第一个编辑可能应该是一个单独的问题,因为它非常独立的,第二,你的第二个编辑绝对应该是一个答案,而不是一个问题。请记住,stackoverflow不应该是一个论坛,而是一个问答网站,这里的问题的真正答案:如何让opencpu与php交互隐藏在问题中... – cmbarbu 2015-08-12 09:11:13

回答

3

OpenCPU完成使用HTTP POST来执行的功能,以及HTTP GET读/渲染对象和图表。您可以先将功能保存到临时商店,然后从那里调用它。在interactive manual的“/ R/tmp API”一章中给出了一个基本示例。如果您单击名为save a functionget the functioncall the function的红色演示按钮,它将帮助您完成这些步骤。

基本上,在第一步中,您对身份函数执行HTTP POST以将您的功能保存在商店中。这也是你发现的running code example page的第三种形式。所以我只是在那里复制你的代码,然后它返回对象x0188b9b9ce

要检查一切是否正常,可以使用HTTP GET读取此对象。例如,打开这个网址读取你的函数的源代码:

http://public.opencpu.org/R/tmp/x0188b9b9ce/ascii 

替代输出例如得到函数作为RDATA文件:

http://public.opencpu.org/R/tmp/x0188b9b9ce/rda 

重要的是HTTP GET从不执行函数。它只是看起来不错,并以您请求的输出格式返回。所以现在你确信你的功能在我们想要运行的商店中。要做到这一点,你需要一个HTTP POST。例如,要获得一个PDF,你可以做

POST http://public.opencpu.org/R/tmp/x0188b9b9ce/pdf 
POST http://public.opencpu.org/R/tmp/x0188b9b9ce/svg 
POST http://public.opencpu.org/R/tmp/x0188b9b9ce/png 

如果你调用该函数接受参数,你把它们作为参数传递给HTTP POST请求。如果要将输出包含在网页中,通常只需要将HTTP POST与/save输出类型结合使用。所以,你可以使用jQuery或任何做:

POST http://public.opencpu.org/R/tmp/x0188b9b9ce/save 

可能返回是这样的:(!耶)

{ 
    "object" : null, 
    "graphs" : [ 
     "x2acba9501a" 
    ], 
    "files" : {} 
} 

这表示您的功能已成功执行,它创造了一个阴谋。该图形已保存到tmp商店。所以,你现在可以得到的图形,并使用它嵌入在页面的HTTP GET:

http://public.opencpu.org/R/tmp/x2acba9501a/png 
http://public.opencpu.org/R/tmp/x2acba9501a/png?!width=900&!height=500 
http://public.opencpu.org/R/tmp/x2acba9501a/pdf 
http://public.opencpu.org/R/tmp/x2acba9501a/svg 
+0

感谢您的澄清。我现在看到我误解的根源。那么,你如何在访问这些信息{ “对象”:空, “图形”: “x2acba9501a” ], “文件”:{} } – 2012-08-14 01:57:37

+0

看到上面的编辑,并感谢您的帮助! – 2012-08-14 02:11:57

+0

感谢您的帮助,我已经得到它的工作。我肯定会回复更多的问题,但结果将成为其他人学习的更多示例。 – 2012-08-15 07:46:14

1

下面是一个完整的例子网页,我嘲笑为使用OpenCPU使用A R包我写了演示特定分析(MARSS) 。我一直在提供简单的特定分析 - 现场用户指南可以这么说。警告,我的例子严重依赖于JavaScript,除了这个例子,我没有JavaScript经验。所以编码是笨重的,但它适用于我的目的。将html复制并粘贴到一个文件中并在浏览器中打开,它应该可以工作。


<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<style> 
html, body 
{ 
background: #6699FF; 
text-align: center; 
font-family : "Lucida Sans Unicode", "Lucida Grande", sans-serif; 
font-size:14px; 
} 
#container 
{ 
background: #FFF; 
border: 0px #222 solid; 
margin: 0 auto; 
text-align: left; 
width: 10.25in; 
padding: 5px; 
overflow: auto; 
} 
#container form 
{ 
margin: 0 auto; 
} 
label 
{ 
float: left; 
width: 50px; 
} 
.leftCol 
{ 
float: left; 
width: 3in; 
} 
.rightCol 
{ 
float: left; 
width: 7in; 
} 
.references 
{ 
font-size: x-small; 
text-indent: 20px; 
} 
</style> 
<base target="_blank" /> 
<title>Count-based population viability analysis (PVA) using corrupted data</title> 
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js">  </script> 
<script> 
baseurl="http://public.opencpu.org"   
// This is done on load since we need the function on the server to deal with the file upload 
function saveRFunction(){ 
//Run this bit of code and store the R function to run on OpenCPU 
var baseurl = "http://public.opencpu.org"; 
var url = "http://public.opencpu.org/R/pub/base/identity/save"; 
// #txtRCommands is the id of the text box in the javascript 
var rFunction = $("#RFunction").val(); 
// this bit sends the the RFunction in the hidden text area 
$.post(url,{ x: rFunction }, 
function (data) { 
var funloc = $.parseJSON(data).object; 
funloc = "http://public.opencpu.org/R/tmp/"+funloc; 
//set the action for the form 
document.getElementById('FunctionLocation').value = funloc;     
});    
setTimeout(function() { 
// Wait 2 seconds because of Chrome before loading default form 
var frm = document.getElementById("inputForm2"); 
subm2(frm,'/svg'); 
}, 1000); 
}; 
//function for first submit button; subm and subm2 same except the id 
function subm(f,ftype){ 
document.getElementById("inputForm").action=document.getElementById('FunctionLocation').value+ftype; 
f.submit(); 
} 
function subm2(f,ftype){ 
document.getElementById("inputForm2").action=document.getElementById('FunctionLocation').value+ftype; 
f.submit(); 
} 
</script> 

</head> 
<body onload="saveRFunction()"> 
<div id="container"> 
<h2>MARSS Case Study 1: Count-based PVA for data with observation error</h2> 
This web tool fits a count-based PVA model (sensu Dennis et al. 1991) from a univariate (one site) time series of abundance data with observation error (Holmes 2001, 2004). The result is an estimated long-term rate of population growth (&lambda;), process variance estimate (&sigma;<sup>2</sup>) and non-process or observation error variance. Extinction risk metrics sensu Dennis et al. (1991) along with an uncertainty plot sensu Ellner and Holmes (2008) are shown. 
</br></br> 
<div class="leftCol"> 
<form enctype="multipart/form-data" action="" method="POST" target="test" id="inputForm"> 
<fieldset> 
<legend><b>Upload a Dataset</b></legend> 
<i>Comma-delimited. 1st col time, 2nd col counts. Missing counts NA.</i></br> 
File: <input name="!file:file" type="file" /> <!-- param name has to be file --> 
Header: <select name="header"><option value=TRUE> TRUE </option> <option value=FALSE> FALSE </option> </select> </br /> 
<input name="!width" type="hidden" value=7 /> <!-- in inches --> 
<input name="!height" type="hidden" value=6 /> <!-- in inches --> 
<INPUT type="button" name="Submit" value="Run Analysis" onclick="subm(this.form,'/svg');"> <!-- svg so Firefox doesn't cache --> 
<INPUT type="button" name="Submit" value="Get PDF of Plot" onclick="subm(this.form,'/pdf');">   </fieldset> 
</form> 
<form action="" method="POST" target="test" id="inputForm2"> 
<fieldset> 
<legend><b>Select an Example Dataset</b></legend> 
Dataset: 
<select name="dataname"> 
<option value='"wilddogs"' selected >African Wilddogs</option> 
<option value='"prairiechicken"'>Prairie Chickens</option> 
<option value='"grouse"'>Sage Grouse</option> 
<option value='"graywhales"'>Gray Whales</option> 
</select></br /> 
<input name="!width" type="hidden" value=7 /> <!-- in inches --> 
<input name="!height" type="hidden" value=6 /> <!-- in inches --> 
<INPUT type="button" name="Submit" value="Run Analysis" onclick="subm2(this.form,'/svg');"> 
<INPUT type="button" name="Submit" value="Get PDF of Plot" onclick="subm2(this.form,'/pdf');"> 
</fieldset> 
</form> 
<fieldset class="references"> 
<legend><b>References</b></legend> 
<p>Dennis, Brian, Patricia L. Munholland, and J. Michael Scott. "Estimation of growth and extinction parameters for endangered species." Ecological monographs 61.2 (1991): 115-143.</p> 
<p>Holmes, E. E. 2001. Estimating risks in declining populations with poor data. Proceedings of the National Academy of Science 98: 5072-5077.</p> 
</fieldset> 
<fieldset class="references"> 
<legend><b>R Code</b></legend> 
<!-- if you do not want to see the R code, use this <textarea hidden="hidden" id="RFunction" style="display:none;"> --> 
<textarea id="RFunction" readonly="readonly" cols="27" rows="18" style="border:0px;margin:0px"> 
function(file=NULL, header=TRUE, dataname="wilddogs"){ 

library(MARSS) 

if(is.null(file)){ 
dat=get(dataname) 
}else{ 
dat=read.csv(file, header=header) 
dat=as.matrix(dat) 
} 

CSEGriskfigure(dat, silent=TRUE) 
} 
</textarea> 
</fieldset> 
<input type="hidden" value="not set" id="FunctionLocation" /> <!-- this is where the function is stored --> 
<br /> 
</div> 
<!-- This iframe holds the output image. --> 
<iframe style='width: 7in; height: 6in; border: 0px solid #000000; padding: 10px;' name='test' id="image_iframe" class="rightCol"></iframe>  
</div> 
<!-- This is an onload script, but Chrome is not loading the whole page before running the onload script. Time out used above for this problem. --> 
</body> 
</html>