RとWebサービス
元々はZopeで利用するR用のSOAPインターフェースあったが、SOAP をサポートするアプリであれば利用できる。。PythonとRとのインターフェースである Rpy と、PythonでSOAPサービスを提供するSOAPpy を併用することで、RをSOAPから利用できるようにしている。
以下は RSOAP 1.1.4 の場合
RSOAPは、「サーバマネージャ」サービスと「サーバプロセス」サービスの2種類からなり、まず「サーバマネージャ」サービスに接続し、そのnewServerメソッドを呼んで「サーバプロセス」サービスのURLを得る。その後「サーバプロセス」サービスに接続し、主にcall, evalメソッドを用いて解析を実行する。
-- サーバマネージャサービスのURLがlocalhost:9081のとき
tell application "http://localhost:9081/"
set returnValue to call soap {method name:"newServer", ¬
method namespace uri:"", parameters:"", SOAPAction:""}
end tell
-- 上の呼び出しで、たとえば"http://localhost:9087"が返ってきたとき
tell application "http://localhost:9087/"
set returnValue to call soap {method name:"call", ¬
method namespace uri:"", parameters:{|String|:"rnorm", |aNumber|:10}, SOAPAction:""}
end tell
eval, downloadFileだけが使えるサンプル(「サーバプロセス」サービス)
<?xml version="1.0"?>
<definitions name="RSOAP" targetNamespace="urn:RSOAP"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:rsoap="urn:RSOAP" xmlns="http://schemas.xmlsoap.org/wsdl/" >
<types>
<xsd:complexType name="downloadFileResponseType">
<xsd:all>
<xsd:element name="flag" type="xsd:int"/>
<xsd:element name="data" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
</types>
<message name="downloadFileRequest">
<part name="name" type="xsd:string"/>
</message>
<message name="downloadFileResponse">
<part name="response" type="rsoap:downloadFileResponseType"/>
</message>
<message name="evalRequest">
<part name="string" type="xsd:string"/>
</message>
<message name="evalResponse">
<part name="value" type="xsd:string"/>
</message>
<portType name="RSOAPProcess">
<operation name="eval">
<input message="rsoap:evalRequest"/> <output
message="rsoap:evalResponse"/>
</operation>
<operation name="downloadFile">
<input message="rsoap:downloadFileRequest"/> <output
message="rsoap:downloadFileResponse"/>
</operation>
<service name="RSOAPProcessService">
<documentation>RSOAP Process API Service</documentation>
<port name="RSOAPProcessPort" binding="rsoap:RSOAPProcessSoapBinding">
<soap:address
location="http://localhost:9083"/>
</port>
</service>
<binding name="RSOAPProcessSoapBinding" type="rsoap:RSOAPProcess">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="eval">
<soap:operation
soapAction="" style="rpc"/>
<input>
<soap:body use="literal"
/>
</input>
<output>
<soap:body use="literal"
/>
</output>
</operation>
<operation name="downloadFile">
<soap:operation
soapAction="" style="rpc"/>
<input>
<soap:body use="literal"
/>
</input>
<output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:RSOAP" use="encoded"/>
/>
</output>
</operation>
</binding>
</definitions>
(工事中)
RSOAPは比較的古いSOAP実装であるSOAPpyを使っているため、SOAPメッセージの出力時にXML schemaのバージョンが1999となっている。したがって配列型を返す場合にその型が2001バージョンのanyTypeではなく, ur-typeとなっているので、新しいSOAPクライアント(apache axis, .NETなど)から接続する場合にそのままでは配列型のデータを交換できない。
ただし、SOAPpyをちょっと改造してur-typeと出力しているところをanyTypeに単純置換するだけでも実用上はわりと問題なかったりする。
データマイニングソフトSpotfire*1でRのクラスタリング機能の呼び出しなどに利用されている。事例1。事例2。事例3。
$RPath = 'C:\\R\\R-2.9.1\\bin\\Rscript.exe'; // Your R path here
$dbfPathName = "/temp/test.dbf";
$cmd = $RPath." -e \"library(foreign);d=read.dbf('$dbfPathName');mean(d['POP'])\"";
$res = shell_exec($cmd);//R の実行
echo explode(" ", $res);//行番号を取る
$RPath = 'C:\\R\\R-2.9.1\\bin\\Rterm.exe'; // Your R path here
$dbfPathName = "/temp/test.dbf";
$cmd = "echo library(foreign);d=read.dbf('$dbfPathName');mean(d['POP'])| $RPath --slave";
$res = shell_exec($cmd);//R の実行。injectgion 対策が要る。
echo explode(" ", $res);//行番号を取る
またこのままだとクオーテーションがバックスラッシュつきなので
地理空間データ用JSON
=gooJSON
Twitter API を利用
RLastFM: R interface to last.fm API last.fm API インターフェース
現状はアメリカ議会情報のデータ